-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (190 loc) · 6 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// npm install -g localtunnel
// lt --port 3000 --subdomain conoro
// FB Callback then available on https://conoro.localtunnel.me
require("dotenv").config();
var express = require("express"),
graph = require("fbgraph");
var app = express();
var server = require("http").createServer(app);
const axios = require("axios");
var postIDs = [];
var conf = {
client_id: process.env.APP_PUBLIC_ID,
client_secret: process.env.APP_SECRET_ID,
scope: "email, user_birthday, user_location, publish_actions, user_likes, user_photos, user_posts, user_tagged_places, user_videos",
// Add the URL below on your Facebook App using Settings -> Add platform -> Website
redirect_uri: "https://conoro.localtunnel.me/auth"
};
// Configuration
var methodOverride = require("method-override");
var bodyParser = require("body-parser");
var errorHandler = require("errorhandler");
app.set("views", __dirname + "/views");
// Jade was renamed to pug
app.set("view engine", "pug");
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(methodOverride());
var path = require("path");
app.use(express.static(path.join(__dirname, "/public")));
var env = process.env.NODE_ENV || "development";
if ("development" == env) {
app.use(
errorHandler({
dumpExceptions: true,
showStack: true
})
);
}
// Routes
app.get("/", function (req, res) {
res.render("index", {
title: "click link to connect"
});
});
app.get("/auth", function (req, res) {
// we don't have a code yet
// so we'll redirect to the oauth dialog
if (!req.query.code) {
console.log("Performing oauth for some user right now.");
var authUrl = graph.getOauthUrl({
client_id: conf.client_id,
redirect_uri: conf.redirect_uri,
scope: conf.scope
});
if (!req.query.error) {
//checks whether a user denied the app facebook login/permissions
res.redirect(authUrl);
} else {
//req.query.error == 'access_denied'
res.send("access denied");
}
} else {
// If this branch executes user is already being redirected back with
// code (whatever that is)
console.log("Got Oauth code");
// code is set
// we'll send that and get the access token
graph.authorize({
client_id: conf.client_id,
redirect_uri: conf.redirect_uri,
client_secret: conf.client_secret,
code: req.query.code
},
function (err, facebookRes) {
var options = {
timeout: 3000,
pool: {
maxSockets: Infinity
},
headers: {
connection: "keep-alive"
}
};
graph.setOptions(options).get("me/posts", function (err, resg) {
for (var i = 0; i < resg.data.length; i++) {
postIDs.push(resg.data[i].id);
console.log(resg.data[i].id);
}
// Just do one page whilst testing
//if (resg.paging && resg.paging.next) getAllPages(resg);
getAllAttachments();
// Send list of IDs to the user for reference
res.setHeader("Content-type", "application/octet-stream");
res.setHeader(
"Content-disposition",
"attachment; filename=my_facebook_post_ids.csv"
);
res.send(postIDs);
});
}
);
}
});
async function getAllPages(res) {
var keep_grabbing = res;
while (keep_grabbing != false) {
keep_grabbing = await getPage(keep_grabbing);
}
}
const getPage = async res => {
try {
if (res && res.paging && res.paging.next) {
const response = await axios.get(res.paging.next);
const fbresp = response.data;
for (var i = 0; i < fbresp.data.length; i++) {
console.log(fbresp.data[i].id);
}
return fbresp;
} else {
console.log("All IDs collected.");
return false;
}
} catch (error) {
console.log(error);
return false;
}
};
async function getAllAttachments() {
var options = {
timeout: 3000,
pool: {
maxSockets: Infinity
},
headers: {
connection: "keep-alive"
}
};
/* Temporarily disable
for (var i = 0; i < postIDs.length; i++) {
await graph
.setOptions(options)
.get(postIDs[i] + "/attachments", function(err, result) {
if (result.data && result.data[0] && result.data[0].title)
console.log(result.data[0].title);
if (result.data && result.data[0] && result.data[0].description)
console.log(result.data[0].description);
if (result.data && result.data[0] && result.data[0].media)
console.log(result.data[0].media);
if (result.data && result.data[0] && result.data[0].target)
console.log(result.data[0].target);
var url;
if (result.data && result.data[0] && result.data[0].url) {
if (result.data[0].url.startsWith("http://l.facebook.com/l.php?u=")) {
url = result.data[0].url.replace(
"http://l.facebook.com/l.php?u=",
""
);
} else if (
result.data[0].url.startsWith("https://l.facebook.com/l.php?u=")
) {
url = result.data[0].url.replace(
"https://l.facebook.com/l.php?u=",
""
);
} else {
url = result.data[0].url;
}
}
console.log(decodeURIComponent(url));
});
}
*/
// Raw Post Data
for (var i = 0; i < postIDs.length; i++) {
graph.setVersion('2.12');
var params = {
fields: "id, caption, created_time, description, full_picture, link, message, message_tags, permalink_url, picture, story, story_tags"
};
await graph.setOptions(options).get(postIDs[i], params, function (err, result) {
console.log(result);
});
}
}
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Express server listening on port %d", port);
});