-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.js
More file actions
157 lines (126 loc) · 4.36 KB
/
rss.js
File metadata and controls
157 lines (126 loc) · 4.36 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
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
const https = require( 'https' );
const url = require( 'url' );
const RSS = require( 'rss' );
const AWS = require( 'aws-sdk' );
require( 'dotenv' ).config();
if ( !process.env.API_TOKEN ) {
throw new Error( 'Unable to load api key' );
}
if ( !process.env.AWS_ACCESS_KEY || !process.env.AWS_SECRET_KEY ) {
throw new Error( 'AWS auth not configured' );
}
const API_HOST = 'api.developertracker.com';
const S3_BUCKET = 'developer-tracker';
const s3 = new AWS.S3( {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
} );
const promiseGet = function promiseGet( requestUrl, headers = false ) {
return new Promise( ( resolve, reject ) => {
let httpsGet = requestUrl;
if ( headers ) {
const urlParts = url.parse( requestUrl );
httpsGet = {
headers: headers,
hostname: urlParts.hostname,
path: urlParts.path,
port: urlParts.port || 443,
};
}
console.log( `Loading ${ requestUrl }` );
const request = https.get( httpsGet, ( response ) => {
if ( response.statusCode < 200 || response.statusCode > 299 ) {
reject( new Error( `Failed to load ${ requestUrl }, status code: ${ response.statusCode }` ) );
}
const body = [];
console.log( `Done with ${ requestUrl }` );
response.on( 'data', ( chunk ) => {
body.push( chunk );
} );
response.on( 'end', () => {
resolve( body.join( '' ) );
} );
} );
request.on( 'error', ( requestError ) => {
reject( requestError );
} );
} );
};
const getGames = async function getGames() {
let allGamesConfig;
const gamesConfig = {};
try {
const gamesConfigResponse = await promiseGet( `https://${ API_HOST }/games`, {
Authorization: `Bearer ${ process.env.API_TOKEN }`,
} );
allGamesConfig = JSON.parse( gamesConfigResponse );
} catch ( getGamesError ) {
console.log( `Unable to load games. Got "${ getGamesError.message }"` );
throw getGamesError;
}
return allGamesConfig.data;
};
const buildRSS = async function buildRSS( game ){
const postsData = await promiseGet( `https://api.developertracker.com/${ game.identifier }/posts?excludeService=Twitter` );
let posts;
try {
posts = JSON.parse( postsData );
} catch ( parseFail ) {
console.error( `Failed to parse posts for ${ game.identifier }` );
throw parseFail;
}
let siteUrl = game.hostname;
if ( game.hostname === 'developertracker.com' ) {
siteUrl = `${ siteUrl }/${ game.identifier }/`;
}
const feed = new RSS( {
title: `${ game.name } dev feed`,
description: 'Feed with the latest posts from the developers',
site_url: `https://${ siteUrl }`,
feed_url: `https://${ siteUrl }/rss/`,
language: 'en-us',
pubDate: new Date(),
ttl: 10,
} );
const guidCache = [];
for ( const post of posts.data ) {
const formattedUrl = post.url.replace( '/&/g', '&' );
if ( guidCache.includes( formattedUrl ) ) {
console.error( `Duplicate guid: ${ formattedUrl }` );
continue;
} else {
guidCache.push( formattedUrl );
}
feed.item( {
title: post.topic,
description: post.content,
url: formattedUrl,
date: new Date( post.timestamp * 1000 ),
author: post.account.developer.nick || post.account.developer.name,
categories: [ post.account.service ],
} );
}
const params = {
Bucket: S3_BUCKET,
Key: `${ game.identifier }/rss`,
Body: feed.xml( { indent: true } ),
CacheControl: 'public, max-age=600',
ContentType: 'application/rss+xml',
};
s3.putObject( params, ( uploadError, data ) => {
if ( uploadError ) {
console.error( uploadError )
} else {
console.log( `Successfully uploaded rss for ${ game.identifier }` );
}
} );
};
getGames()
.then( ( games ) => {
for ( const game of games ) {
buildRSS( game );
}
} )
.catch( ( someError ) => {
console.error( someError );
} );