-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (75 loc) · 2.84 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
const express = require("express");
const { Client } = require("twitter-api-sdk");
const { Parser } = require('json2csv');
const zip = require('adm-zip');
const fs = require('fs');
const path = require("path");
const port = 3000;
const client = new Client(process.env.BEARER_TOKEN);
const server = express();
server.use(express.urlencoded({ extended: true }))
server.set('views', path.join(__dirname, 'views'));
server.set('view engine', 'ejs');
server.use(express.static(path.join(__dirname, 'public')))
server.get("/", (req, res) => {
res.render('pages/index')
});
server.post("/social_graph", async (req, res) => {
var handle = req.body.fhandle;
var result = await handleGraph(handle.toLowerCase());
if (typeof result === "undefined" || typeof result["followers"] === "undefined" || typeof result["followings"] === "undefined") {
res.status(404).send("Wrong Twitter Handle or Protected Account!")
};
fs.appendFile('graph_check_count.txt', handle + "\n" , function (err) {
if (err) throw err;
console.log('graph check!');
});
res.render('pages/social_graph', {
profile: result["profile"],
followers: result["followers"],
followings: result["followings"]
});
})
server.post("/download", async (req, res) => {
var handle = req.body.handle;
var result = await handleGraph(handle.toLowerCase());
fs.appendFile('download_count.txt', handle + "\n", function (err) {
if (err) throw err;
console.log('data downloaded!');
});
var follower_csv = createCSV(result["followers"]);
var following_csv = createCSV(result["followings"]);
var zipper = new zip();
zipper.addFile("follower.csv", Buffer.from(follower_csv));
zipper.addFile("following.csv", Buffer.from(following_csv));
const downloadName = 'twitter_social_graph.zip';
const data = zipper.toBuffer();
res.set('Content-Type','application/octet-stream');
res.set('Content-Disposition',`attachment; filename=${downloadName}`);
res.set('Content-Length',data.length);
res.send(data);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(
`> Ready on ${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`
);
});
async function handleGraph(handle) {
var user = await(client.users.findUserByUsername(handle));
var user_id = user["data"]["id"];
var profile = await(client.users.findUserById(
user_id,
{
"user.fields": ["description", "public_metrics"]
}));
const followers = await(client.users.usersIdFollowers(user_id, {max_results: 1000}));
const followings = await(client.users.usersIdFollowing(user_id, {max_results: 1000}));
return {"profile": profile["data"], "followers": followers["data"], "followings": followings["data"]}
}
function createCSV(data) {
const fields = ['name', 'username'];
const json2csvParser = new Parser({ fields });
const csv = json2csvParser.parse(data);
return csv
}