-
Notifications
You must be signed in to change notification settings - Fork 1
/
npmau.js
75 lines (64 loc) · 2.06 KB
/
npmau.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
const NTwitter = require('ntwitter')
, npmMaintainers = require('npm-maintainers-au')
, NpmPublishStream = require('npm-publish-stream')
, NpmMaintainerFilterStream = require('./npm-maintainer-filter-stream')
, maintainerFilterStream = new NpmMaintainerFilterStream()
, secrets = require('./secrets')
, twit = new NTwitter(secrets.ntwitter)
var started = false
function tweet (status, cb) {
console.log(status.length, status)
// twit.updateStatus() uses the wrong URL, needs trailling slash, so do it manually
twit.post(
'/statuses/update.json/'
, { status: status, include_entities: 1 }
, null
, cb || function (err) {
if (err)
console.error(err)
}
)
}
function handleNpmData (data) {
var pkg = data.id + '@' + data.doc['dist-tags'].latest
, url = 'http://npm.im/' + data.id
, maint = '('
+ data.doc.versions[data.doc['dist-tags'].latest].maintainers
.map(function (m) { return m.name }).join(', ')
+ ')'
, len = pkg.length + 1 + 22 /* t.co */ + 1 + maint.length + 1
, desc = (data.doc.description || '')
if (len + desc.length > 140)
desc = desc.substring(0, 140 - len - 1) + '…'
tweet(pkg + ' ' + url + ' ' + desc + ' ' + maint)
}
function updateMaintainers () {
npmMaintainers(function (err, data) {
if (err)
return console.log(err)
if (data.length) {
maintainerFilterStream.setMaintainers(
data.map(function (u) { return u.npmLogin })
)
start()
}
})
}
twit.verifyCredentials(function (err, data) {
if (err) {
console.error('COULD NOT VERIFY TWITTER CREDENTIALS', err)
return process.exit(-1)
}
console.log('Verified Twitter credentials:', JSON.stringify(data))
})
function start () {
if (started)
return
started = true
NpmPublishStream({ startTime: new Date(Date.now() - 1000 * 60 * 60 * 6)})
.on('error', console.error)
.pipe(maintainerFilterStream)
.on('data', handleNpmData)
}
setInterval(updateMaintainers, 1000 * 60 * 60 * 12)
updateMaintainers()