-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
127 lines (113 loc) · 3.1 KB
/
data.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
const fs = require("fs"),
url = require("url"),
request = require("request"),
cheerio = require("cheerio"),
NodeCache = require("node-cache"),
cacheOptions = {
stdTTL: 3600,
},
cacheTwitterData = new NodeCache(cacheOptions),
cacheSiteData = new NodeCache(cacheOptions);
const tokenFile = "./.data/token";
const tweetsDirectory = "./.data/tweets";
const existsToken = fs.existsSync(tokenFile);
const existsTweetsDirectory = fs.existsSync(tweetsDirectory);
if (!existsTweetsDirectory) {
fs.mkdirSync(tweetsDirectory);
}
const getPageMetadataFn = (pageUrl) => {
if (pageUrl.length > 0) {
return new Promise((resolve, reject) => {
let domain = url.parse(pageUrl).hostname;
if (domain.indexOf("www.") === 0) {
/* Remove leading www */
domain = domain.substr(4);
}
const r = request.get(pageUrl, (err, res, body) => {
if (r !== undefined) {
const $ = cheerio.load(body);
const pageTitle =
$('meta[property="og:title"]').attr("content") ||
$('meta[property="twitter:text:title"]').attr("content") ||
$("title").text() ||
"";
const pageDescription =
$('meta[name="description"]').attr("content") ||
$('meta[name="twitter:title"]').attr("content") ||
$('meta[property="og:title"]').attr("content") ||
"";
const image =
$('meta[name="twitter:image"]').attr("content") ||
$('meta[property="og:image"]').attr("content") ||
"";
const siteData = {
title: pageTitle.trim(),
description: pageDescription.trim(),
image: image,
"original-url": pageUrl,
domain: domain,
};
cacheSiteData.set(pageUrl, siteData);
return resolve(siteData);
}
});
});
} else {
return false;
}
};
const dataHelper = {
storeTweetData: (tweet) => {
let valuesSQL = [];
fs.writeFileSync(
`${tweetsDirectory}/${tweet.id}.json`,
JSON.stringify(tweet),
{
/* flag: 'wx' */
}
);
},
getTweetData: (tweetIds, cb) => {
let tweetData = [];
tweetIds.forEach((id) => {
const filePath = `${tweetsDirectory}/${id}.json`;
tweetData.push(JSON.parse(fs.readFileSync(filePath)));
});
if (cb) {
cb(tweetData);
}
},
storeTwitterToken: (token, cb) => {
fs.writeFile(tokenFile, token, (err) => {
if (cb) {
cb(err, token);
}
});
},
getTwitterToken: (cb) => {
fs.readFile(tokenFile, (err, token) => {
if (cb) {
cb(err, token);
}
});
},
getSiteData: (pageUrl, cb) => {
const urls = [pageUrl],
siteInfo = cacheSiteData.get(pageUrl);
if (!siteInfo) {
const urls = [pageUrl];
let actions = urls.map(getPageMetadataFn);
let results = Promise.all(actions);
results.then((results) => {
if (cb) {
cb(null, results[0]);
}
});
} else {
if (cb) {
cb(null, siteInfo);
}
}
},
};
module.exports = dataHelper;