-
Notifications
You must be signed in to change notification settings - Fork 0
/
gistbot.js
155 lines (138 loc) · 3.52 KB
/
gistbot.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
const RegexDigger = require('./regexDigger');
const https = require('https');
const fs = require('fs');
var date;
var config;
try{
config = require('./config');
}
catch(e){
config = {
clientId: 'xxx',
clientSecret: 'yyy'
};
}
/** .gistbotrc variables */
var loglevel, interval, lineWidth, languages, regularExpressions, storageType;
var cache = [];
var options = {
hostname: 'api.github.com',
port: 443,
path: `/gists/public?client_id=${config.clientId}&client_secret=${config.clientSecret}`,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36',
'Accept': 'application/json'
}
};
function isGistNew(gist, done){
if(storageType === 'filesystem'){
fs.readFile(`data/gistbot-${date}.tsv`, 'utf8', function(err, res){
if(err || res.indexOf(gist.id) === -1){
fs.appendFileSync(`data/gistbot-${date}.tsv`, `${gist.id}\t${gist.owner ? gist.owner.login : 'anonymous'}\t${gist.description}\n`);
return done(1);
}
return done(0);
});
}
else{
if(cache.indexOf(gist.id) === -1){
cache.push(gist.id);
return done(1);
}
return done(0);
}
}
function logRotate(){
date = `${(new Date()).getDate()}-${(new Date()).getMonth()}-${(new Date()).getFullYear()}`;
}
function fetchData() {
logRotate();
var req = https.request(options, (res) => {
var data = '';
res.on('data', (d) => {
data+=d;
});
res.on('end', () => {
try{
data = JSON.parse(data);
}
catch(e){
return;
}
if(data.message) return;
var filtered = data.filter((el)=>{
if(!languages.length) return true;
var flag = false;
Object.keys(el.files).forEach((file)=>{
if(languages.indexOf(el.files[file].language) !== -1){
flag = true;
}
});
return flag;
});
filtered.forEach((gist)=>{
isGistNew(gist, (itsNew)=>{
if(!itsNew) return;
gist.description = (gist.description || 'No Description Available').replace(/\n/g, ' ');
if(gist.description.length > lineWidth){
gist.description = gist.description.slice(0, lineWidth);
loglevel && console.log(`${gist.description} (${gist.html_url})`);
}
else{
loglevel && console.log(`${gist.description}${' '.repeat(lineWidth - gist.description.length)} (${gist.html_url})`);
}
if(regularExpressions.length){
RegexDigger.start({
files: Object.keys(gist.files).map((el)=>{
return gist.files[el].raw_url;
}),
regularExpressions: regularExpressions
});
}
});
});
});
});
req.end();
req.on('error', (e) => {
console.error(e);
});
}
function version(){
fs.readFile('./package.json', 'utf8', (err, res)=>{
console.log(JSON.parse(res).version);
process.exit();
});
}
function main(){
fs.readFile('.gistbotrc', 'utf8', function(err, res){
if(err){
console.log('Error: Can\'t find .gistbotrc file..');
process.exit();
}
var rc = JSON.parse(res); // Temporary solution until es6 array desctructing comes
loglevel = rc.loglevel;
interval = rc.interval;
lineWidth = rc.lineWidth;
languages = rc.languages;
storageType = rc.storageType;
if(rc.regexPath){
try{
regularExpressions = fs.readFileSync(rc.regexPath, 'utf8');
regularExpressions = regularExpressions.split('\n');
}
catch(e){
console.log('Error: Can\'t open file');
process.exit();
}
}
else{
regularExpressions = [];
}
if(process.argv.indexOf('-v') !== -1) return version();
fetchData();
setInterval(fetchData, interval);
});
}
main();