This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wowfeed.js
103 lines (89 loc) · 3.69 KB
/
wowfeed.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
'use strict';
const fs = require('fs'),
http = require('http'),
url = require('url'),
ua = require('universal-analytics'),
app = require('./lib/app.js'),
port = process.env.PORT || 3000,
wowfeed = {
/**
* Tell the client the search params were not correct
* @param response
*/
createIndexPage: (response) => {
fs.readFile('./docs/index.html', 'binary', (err, file) => {
if (err) {
console.log(err);
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
});
},
/**
* Tell the client that return value is of rss type
* @param response
*/
createFeedPage: (response, feed) => {
response.writeHead(200, {'Content-Type': 'application/rss+xml'});
response.write(feed.rss2());
response.end();
},
/**
* Tell the client what the error is
* @param error
*/
createErrorPage: (response, error) => {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(`Error: ${error.response.status} - ${error.response.statusText}`);
response.end();
},
initialize: () => {
// Create and start the server to handle requests
const server = http.createServer();
server.on('request', async (request, response) => {
let analytics = ua('UA-431999-5', {https: true});
// Extract the searchquery from the url
let urlParts = url.parse(request.url, true),
options = {
character: urlParts.query.character,
guild: urlParts.query.guild,
realm: urlParts.query.realm,
region: urlParts.query.region,
showSteps: urlParts.query.showSteps !== 'false',
maxItems: urlParts.query.maxItems || 20
};
// Check if all mandatory options are there
if (!options.region || !options.realm || !(options.character || options.guild)) {
analytics.pageview('index').send();
wowfeed.createIndexPage(response);
return;
}
// Replace ' in realm names like Khaz'goroth
if (options.realm) {
options.realm = options.realm.replace('\'', '');
}
try {
// Actually create the feed
const feed = await app.createFeed(options);
// Send analytics events
if (options.character) {
analytics.pageview(`character/${options.region}/${options.realm}/${options.character}`).send();
} else if (options.guild) {
analytics.pageview(`guild/${options.region}/${options.realm}/${options.guild}`).send();
}
return wowfeed.createFeedPage(response, feed);
} catch (error) {
// Send analytics exception
analytics.exception({
dp: request.url,
exd: `${error.response.status} - ${error.response.statusText}`
}).send();
wowfeed.createErrorPage(response, error);
}
});
server.listen(port);
console.log('Server running at port: ' + port);
}
};
wowfeed.initialize();