This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (76 loc) · 3.37 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
const knex = require('knex');
const schedule = require('node-schedule');
const util = require('./lib/util');
const RepositoriesContainer = require('./lib/RepositoriesContainter');
const gitHubApi = require('./lib/githubApi');
const pubsub = require('./lib/pubsub');
const config = require('./config');
const repositoriesContainer = new RepositoriesContainer(knex(config.database.sqlite));
repositoriesContainer
.open()
.then(() => {
const publisher = new pubsub.Publisher();
Object.keys(config.subscribers).forEach(name => {
const subscriber = require('./subscribers/' + name);
if (!(subscriber instanceof pubsub.Subscriber)) {
return util.log(name, 'is a invalid subscriber');
}
publisher.subscribe(subscriber);
subscriber.emit(
pubsub.eventSubscribe,
config.subscribers[name].config
);
});
const jobPublishing = schedule.scheduleJob(config.scheduler.publishing, () => {
repositoriesContainer
.getNextUnpublished()
.then(id => {
if (!id) {
util.log('Repositories container has no unpublished records. Publishing stopped');
return jobPublishing.cancel();
}
return gitHubApi
.getRepoById(id)
.then(repo => {
let repoData = {
lang: util.normalizeLang(repo.language),
description: util.normalizeDescription(repo.description),
url: repo.html_url,
};
repoData.topics = repo.topics.filter(topic => topic !== repoData.lang);
publisher.publish(repoData);
return repositoriesContainer.markAsPublished(repo.id);
});
})
.catch(err => util.log(err));
});
const jobUpdatingContainer = schedule.scheduleJob(config.scheduler.updatingContainer, () => {
jobPublishing.cancel();
jobUpdatingContainer.cancel();
util.log('Getting current trending...');
gitHubApi
.getCurrentTrending()
.then(repos => {
util.log('Got. Saving new repositories in container...');
return repos.reduce((chain, repo) => {
return chain
.then(() => {
return repositoriesContainer
.has(repo.id);
})
.then(has => {
if (has) {
return;
}
return repositoriesContainer
.add(repo.id)
.then(() => util.log(repo.html_url));
})
.catch(err => util.log(err));
}, Promise.resolve());
})
.then(() => util.log('Saved'))
.catch(err => util.log(err));
});
})
.catch(err => util.log(err));