-
Notifications
You must be signed in to change notification settings - Fork 0
/
redditbot.js
150 lines (126 loc) · 4.01 KB
/
redditbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const request = require('request-promise');
const download = require('image-downloader')
const uuidv4 = require('uuid/v4');
var options = {
uri: 'https://www.reddit.com/r/memes.json?t=day',
headers: {
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
// Download to a directory and save with an another filename
var admin = require("firebase-admin");
var serviceAccount = require(");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "",
});
var bucket = admin.storage().bucket();
const db = admin.firestore();
const filenames = []
request(options)
.then(function (body) {
const memes = body.data.children
var promises = []
memes.forEach( meme => {
const url = meme.data.url
if (url.includes('.jpg') || url.includes('.png')) {
console.log(url)
// urls.push(url)
// upload each url to firebase storage with uuid as name
// store uuid in database with user details
const random = uuidv4()
const imageOptions = {
url: url,
dest: `./images/${random}.jpg` ,
extractFilename: false
}
filenames.push(random)
promises.push(
download.image(imageOptions)
.then(({ filename, image }) => {
console.log('Saved to', filename) // Saved to /path/to/dest/photo.jpg
})
)
}
})
return Promise.all(promises)
})
.then(function () {
console.log("FILENAME", filenames)
// upload images to firebase storage
var promises = []
// Create a reference
filenames.forEach( name => {
// var imageRef = bucket.child();
promises.push(
bucket.upload(`./images/${name}.jpg`)
.then(function(snapshot) {
console.log('Uploaded a blob or file!');
})
)
})
return Promise.all(promises)
// save to memeDb and accountDB
// "uid": userID,
// "username": userID,
// "timestamp": Timestamp(date: Date()),
// "votes": 1,
// "earnings": 0,
})
.then( function () {
var uid = "redditbot"
var promises = []
filenames.forEach( name => {
// var imageRef = bucket.child();
var meme = {
"uid": uid,
"username": uid,
// "timestamp": admin.firestore.FieldValue.serverTimestamp(),
"votes": 1,
"earnings": 0
}
const memeData = {
[name] : {
"uid": uid,
"username": uid,
// "timestamp": admin.firestore.FieldValue.serverTimestamp(),
"votes": 1,
"earnings": 0
}
}
promises.push(
db.collection('accounts').doc(uid).update({
[`memes.${name}`] : {
"uid": uid,
"username": uid,
// "timestamp": admin.firestore.FieldValue.serverTimestamp(),
"votes": 1,
"earnings": 0
}
})
)
})
return Promise.all(promises)
})
.then( function () {
var uid = "redditbot"
var promises = []
filenames.forEach( name => {
// var imageRef = bucket.child();
promises.push(
db.collection('memes').doc(name).set({
"uid": uid,
"username": uid,
"timestamp": admin.firestore.FieldValue.serverTimestamp(),
"votes": 1,
"earnings": 0,
})
)
})
return Promise.all(promises)
})
.catch(function (err) {
console.log(err)
// API call failed...
});