-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (123 loc) · 3.5 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
const express = require('express')
const app = express()
const fetchUrl = require("fetch").fetchUrl;
var schedule = require('node-schedule');
var path = require('path');
// sequelize
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
// SQLite only
storage: 'reddit.sqlite'
});
const Links = sequelize.define('Links', {
kind :Sequelize.STRING,
url :Sequelize.TEXT,
domain : Sequelize.TEXT,
id : {
type:Sequelize.INTEGER,
primaryKey:true,
autoIncrement:true
},
pid:Sequelize.TEXT,
title : Sequelize.TEXT,
clicked : Sequelize.BOOLEAN,
link_flair_text : Sequelize.TEXT,
pinned : Sequelize.BOOLEAN,
score : Sequelize.INTEGER,
gilded : Sequelize.INTEGER,
downs : Sequelize.INTEGER,
brand_safe : Sequelize.BOOLEAN,
created_utc : Sequelize.DATE,
ups : Sequelize.INTEGER,
num_comments : Sequelize.INTEGER,
author :Sequelize.STRING
});
var recur = (count,objArr,cb)=>{
// source file is iso-8859-15 but it is converted to utf-8 automatically
var url = "http://www.reddit.com/r/worldnews/new.json";
if(objArr.length > 0 ){
// console.log("slow ",objArr.length);
// console.log("slow ",JSON. (objArr[objArr.length - 1]) );
var url = "http://www.reddit.com/r/worldnews/new.json?count="+objArr.length+"&after="+objArr[objArr.length-1].kind+"_"+objArr[objArr.length-1].pid;
}
console.log("------------------");
console.log(url);
fetchUrl(url, function(error, meta, body){
var resObj= JSON.parse(body.toString());
// console.log("slow 2", body.toString());
resObj.data.children.forEach((child,index)=>{
var Obj={
kind : child.kind,
url : child.data.url,
domain : child.data.domain,
pid : child.data.id,
title : child.data.title,
clicked : child.data.clicked,
link_flair_text : child.data.link_flair_text,
pinned : child.data.pinned,
score : child.data.score,
gilded : child.data.gilded,
downs : child.data.downs,
brand_safe : child.data.brand_safe,
created_utc : child.data.created_utc,
ups : child.data.ups,
num_comments : child.data.num_comments,
author : child.data.author
}
objArr.push(Obj);
Links.create(Obj).then(link=>{
}).catch(err=>console.log("ERr" , err));;
})
if(count==objArr.length){
cb(objArr);
}else{
recur(count,objArr,cb);
}
});
}
app.get('/sync', (req, res) =>{
recur(100,[],(obj)=>{
console.log(obj.length);
Links.findAll({
where:{}
}).then(links=>{
res.send(links)
}).catch(err=>{
console.log(err);
})
})
});
var port = process.env.PORT || 5000;
app.get('/',(req,res)=>{
Links.findAll({
where:{}
}).then(links=>{
res.send({count:links.length,links:links})
}).catch(err=>{
console.log(err);
})
});
app.get('/download',(req,res)=>{
// res.send({count:links.length,links:links})
res.sendFile(path.join(__dirname, 'reddit.sqlite'));
});
app.listen(port, () => {
Links.sync().then(() => {
// Table created
var j = schedule.scheduleJob('*/5 * * * *', function(){
console.log("-------------------------------------------");
fetchUrl("http://localhost:"+port+"/sync", function(error, meta, body){
console.log(body.toString());
});
});
});
})