-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (138 loc) · 5.35 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const fs = require( 'fs' );
const path = require( 'path' );
const chalk = require( 'chalk' );
const got = require( 'got' );
const Queue = require( 'bull' );
const now = require( 'performance-now' );
const indexers = require( './indexers/' );
const API_TOKEN = process.env.API_TOKEN;
if ( !API_TOKEN ) {
throw new Error( 'Unable to load API token' );
}
if ( !process.env.REDIS_URL ) {
throw new Error( 'Got no queue string, exiting' );
}
const MAX_INDEX_TIME = ( 8 * 60 * 1000 ); // 8 minutes
const MIN_REQUEST_TIMING = 1000;
let requestTiming = 1000;
const requestOptions = {
headers: {
Authorization: `Bearer ${ API_TOKEN }`,
},
json: true,
};
const redditQueue = new Queue(
'reddit-posts',
process.env.REDIS_URL,
);
const addJobToQueue = function addJobToQueue ( accountId, gameIdentifier, jobData ) {
// console.log( `Adding job ${ jobData.data.id }` );
return redditQueue.add( {
accountId: accountId,
game: gameIdentifier,
post: jobData,
}, {
removeOnComplete: true,
jobId: jobData.data.id,
} )
};
const sleep = function sleep( ms ) {
// console.log( `Sleeping for ${ ms }ms` );
return new Promise( ( resolve ) => {
setTimeout( resolve, ms );
} );
};
console.time( 'Indexer' );
process.on( 'exit', () => {
console.timeEnd( 'Indexer' );
} );
const indexGame = function indexGame ( gameData ) {
// console.log( `Indexing ${ gameData.identifier }` );
return new Promise( async ( resolve, reject ) => {
// console.log( `Checking ${ gameData.accounts.length } devs for ${ gameData.identifier }` );
let indexPromises = [];
for ( let accountIndex = 0; accountIndex < gameData.accounts.length; accountIndex = accountIndex + 1 ) {
// console.log( `Finding posts for ${ gameData.accounts[ accountIndex ].identifier }` );
const start = now();
indexPromises.push( indexers.reddit.findNewPosts( gameData.accounts[ accountIndex ].identifier, gameData.allowedSections, gameData.disallowedSections )
.then( ( newPosts ) => {
let addJobs = [];
for( let i = 0; i < newPosts.length; i = i + 1 ) {
addJobs.push( addJobToQueue( gameData.accounts[ accountIndex ].id, gameData.identifier, newPosts[ i ] ) );
}
return Promise.all( addJobs );
} )
.catch( ( indexError ) => {
console.error( indexError );
} )
);
const end = now();
// Make sure we don't do more than 1 request / MIN_REQUEST_TIMING ms
if ( end - start < requestTiming ) {
await sleep( Math.max( requestTiming, MIN_REQUEST_TIMING ) - ( end - start ) );
}
}
Promise.all( indexPromises )
.then( () => {
resolve();
} );
} );
};
const run = async function run () {
const gamePromises = [];
let gamesResponse = false;
let totalAccounts = 0;
try {
gamesResponse = await got( `https://api2.developertracker.com/games`, requestOptions );
} catch ( gameDataError ) {
throw gameDataError;
}
const gameData = {};
gamesResponse.body.data.forEach( ( gameConfig ) => {
if ( gameConfig.config && gameConfig.config.sources ) {
gameData[ gameConfig.identifier ] = gameConfig.config.sources;
}
} );
Object.keys( gameData ).forEach( ( gameIdentifier ) => {
gamePromises.push( got( `https://api2.developertracker.com/${ gameIdentifier }/accounts?active=1`, requestOptions )
.then( ( accountResponse ) => {
const accounts = [];
for ( let i = 0; i < accountResponse.body.data.length; i = i + 1 ) {
if ( accountResponse.body.data[ i ].service === 'Reddit' ) {
accounts.push( accountResponse.body.data[ i ] );
totalAccounts = totalAccounts + 1;
}
}
return {
accounts: accounts,
identifier: gameIdentifier,
allowedSections: gameData[ gameIdentifier ][ 'Reddit' ].allowedSections,
disallowedSections: gameData[ gameIdentifier ][ 'Reddit' ].disallowedSections,
};
} )
.catch( ( something ) => {
console.log( 'GOT ERROR' );
console.log( something );
} )
);
} );
Promise.all( gamePromises )
.then( async ( gameData ) => {
console.log( `Got ${ totalAccounts } Reddit accounts` );
requestTiming = Math.round( MAX_INDEX_TIME / totalAccounts );
for ( let gameIndex = 0; gameIndex < gameData.length; gameIndex = gameIndex + 1 ) {
console.time( gameData[ gameIndex ].identifier );
await indexGame( gameData[ gameIndex ] )
.catch( ( indexError ) => {
throw indexError;
} );
console.timeEnd( gameData[ gameIndex ].identifier );
}
// Close queue after all games are indexed
redditQueue.close();
} )
.catch( ( error ) => {
console.log( error );
} );
};
run();