-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (93 loc) · 2.64 KB
/
server.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
// server.js
// where your node app starts
// init project
const express = require("express");
const bodyParser = require("body-parser");
const Instagram = require("instagram-web-api");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
response.sendFile(`${__dirname}/views/index.html`);
});
app.get("/process", async (request, response) => {
try {
let username = request.query.username;
let posts = await getPosts(username);
let data = [];
for (let c = 0, i = posts.length; c < i; c++) {
data.push({
likes: posts[c].node.edge_media_preview_like.count,
thumbnail: posts[c].node.thumbnail_src,
caption: posts[c].node.edge_media_to_caption.edges.length
? posts[c].node.edge_media_to_caption.edges[0].node.text
: "",
shortcode: posts[c].node.shortcode
});
}
data.sort((a, b) => {
if (a.likes < b.likes) return 1;
if (b.likes < a.likes) return -1;
return 0;
});
response.send({
message: true,
posts: data
});
} catch (e) {
console.error(e);
response.send({
message: false,
posts: []
});
}
});
async function getPosts(username) {
let client = new Instagram(username);
let posts = [];
let page = false;
do {
let pagePosts = await getPage(username, client, page);
page = pagePosts.page;
posts.push(...pagePosts.data);
} while (page !== false);
return posts;
}
async function getPage(username, client, page) {
let result = {
data: [],
page: false
};
let params = {
username: username,
first: 50
};
if (page) {
params.after = page;
}
let data = await client.getPhotosByUsername(params);
if (data.user.edge_owner_to_timeline_media.page_info.has_next_page) {
result.page = data.user.edge_owner_to_timeline_media.page_info.end_cursor;
} else {
result.page = false;
}
if (data.user.edge_owner_to_timeline_media.edges.length) {
for (
let l = data.user.edge_owner_to_timeline_media.edges.length, c = 0;
c < l;
c++
) {
result.data.push(data.user.edge_owner_to_timeline_media.edges[c]);
}
}
return result;
}
// listen for requests :)
var listener = app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});