-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (70 loc) · 2.81 KB
/
index.js
File metadata and controls
85 lines (70 loc) · 2.81 KB
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
'use strict'
const RTM = require("satori-sdk-js")
const octonode = require('octonode')
const path = require('path')
const examples = require('node-examples')
const Rx = require('node-keyboard-rx')()
function subscribeToGithubStars() {
const endpoint = "wss://open-data.api.satori.com"
const appKey = process.env.SATORI_ACCESS_TOKEN
const rtm = new RTM(endpoint, appKey)
const subscription = rtm.subscribe('gh-watch-events', RTM.SubscriptionMode.SIMPLE, {
filter: 'select * from `github-events` where type=\'WatchEvent\''
})
rtm.start()
return Rx.Observable.fromEvent(subscription, 'rtm/subscription/data').finally(() => rtm.stop())
}
const github = module.exports = {
_starred() {
const starred = subscribeToGithubStars()
const foundStars = []
return starred
.flatMap(pdu => pdu.body.messages)
.map(msg => { return { user: msg.actor.login, repo: msg.repo.name } })
.filter(({ user, repo }) => {
if (foundStars.indexOf(user+repo) > -1 ) return false
foundStars.push(user+repo)
return true
})
},
starredRepos() {
const client = octonode.client(process.env.GITHUB_ACCESS_TOKEN)
return github._starred()
.concatMap(({ user, repo }) => {
const context = client.repo(repo)
return Rx.Observable.forkJoin(
Rx.Observable.of({ user, repo }),
Rx.Observable.bindNodeCallback(context.languages.bind(context))()
)
})
.map(([{ user, repo }, [ languages ]]) => {
const sum = Object.keys(languages).reduce((acc, key) => {
return acc + languages[key]
}, 0)
languages = Object.keys(languages).reduce((acc, key) => {
acc[key] = languages[key] / sum
return acc
}, {})
return { user, repo, languages }
})
},
sourceSearch(query) {
const client = octonode.client(process.env.GITHUB_ACCESS_TOKEN)
return github._starred()
.concatMap(({ user, repo }) => {
const context = client.search()
return Rx.Observable.forkJoin(
Rx.Observable.of({ user, repo }),
Rx.Observable.bindNodeCallback(context.code.bind(context))({
q: `${query}+repo:${repo}`,
sort: 'created',
order: 'asc'
})
)
})
.map(([{ user, repo }, [ { items } ]]) => {
return { user, repo, items }
})
}
}
examples({ path: path.join(__dirname, 'examples'), prefix: 'github_example_', cache: false })