-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
249 lines (217 loc) · 6.82 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var url = require('url');
var collections = require('./collections');
var sprintf = require('./sprintf').sprintf;
var dirty = require('./dirty');
var md5 = require('./md5').md5;
var app = module.exports = express.createServer();
var ajax = function(urlStr, callback) {
var u = url.parse(urlStr);
http.get({ host: u.host, port: u.port, path: u.pathname + (u.search ? u.search : ''), headers: {'Accept': '*/*', 'User-Agent': 'curl'} },
function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
try {
callback(null, JSON.parse(data));
}
catch (err) {
console.log('exception during ajax');
console.log(err);
callback(err);
}
});
}).on('error', function(error){
console.log('error during ajax');
console.log(error);
callback(error);
});
}
var clean = function(urlStr, callback) {
u = url.parse(urlStr);
http.get({ host: u.host, port: u.port, path: u.pathname},
function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
try {
callback(null, dirty.parse(data));
}
catch (err) {
callback(err);
}
});
}).on('error', function(e) {
callback(e);
});
}
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res) {
res.render('index', {
title: 'ClockworkMod'
});
});
app.get('/desksms', function(req, res) {
res.redirect('https://desksms.appspot.com');
});
app.get('/rommanager', function(req, res) {
res.header('Cache-Control', 'max-age=300');
ajax("http://gh-pages.clockworkmod.com/ROMManagerManifest/devices.js", function(err, data) {
var devices = data.devices;
var version = data.version
devices = collections.filter(devices, function(index, device) {
if (device.officially_supported === false)
return null;
if (!device.version)
device.version = version;
var downloadUrl = device.readonly_recovery ? data.recovery_zip_url : data.recovery_url;
downloadUrl = sprintf(downloadUrl, device.version, device.key);
device.downloadUrl = downloadUrl;
if (device.touch_version) {
var touchDownloadUrl = device.readonly_recovery ? data.recovery_zip_url : data.recovery_url;
touchDownloadUrl = sprintf(touchDownloadUrl, 'touch-' + device.touch_version, device.key);
device.touchDownloadUrl = touchDownloadUrl;
}
return device;
});
collections.sort(devices, function(device) { return device.name; });
res.render('rommanager', {
title: 'ClockworkMod ROM Manager - Recoveries',
devices: devices
});
});
});
app.get('/rommanager/device/:device/developer/:developerId', function(req, res) {
res.header('Cache-Control', 'max-age=300');
var id = req.params.developerId;
var manifest = req.query.manifest;
var device = req.params.device;
var name = req.query.name;
var deviceName = req.query.deviceName;
if (!manifest || !name) {
res.redirect('/rommanager/devices');
return;
}
clean(manifest, function(err, data) {
if (err) {
res.render('rommanager/developer', {
title: 'ClockworkMod ROM Manager - ROMs',
developer: name,
error: 'This developer section is currently under maintenance.'
});
}
else {
var roms = collections.filter(data.roms, function(index, rom) {
if (rom.visible === false)
return;
if (rom.device == device || rom.device == 'all') {
if (rom.urls)
rom.url = rom.urls[0];
if (!rom.url)
return;
if (!rom.modversion)
rom.modversion = md5(rom.url);
return rom;
}
});
ajax("http://rommanager.clockworkmod.com/v2/ratings/" + id, function(err, data) {
if (!err) {
var ratings = data.result;
console.log(ratings);
collections.each(roms, function(index, rom) {
var rating = ratings[rom.modversion];
if (rating) {
rom.rating = Math.round(rating.rating * 100 / 5);
rom.downloadCount = rating.downloads;
console.log(rom.rating);
}
});
}
res.render('rommanager/developer', {
title: 'ClockworkMod ROM Manager - ROMs',
developer: name,
error: '',
roms: roms,
deviceName: deviceName
});
});
}
});
});
app.get('/rommanager/developers/:device', function(req, res) {
res.header('Cache-Control', 'max-age=300');
var name = req.query.name;
var url = 'http://developer.clockworkmod.com/merge/' + req.params.device + '.js';
ajax(url, function(err, data) {
var manifests = data.manifests;
ajax("http://rommanager.clockworkmod.com/v2/ratings", function(err, data) {
if (data) {
var ratings = data.result;
collections.each(manifests, function(index, manifest) {
var rating = ratings[manifest.id];
if (rating) {
manifest.rating = Math.round(rating.totalRating / rating.ratingCount * 100 / 5);
manifest.downloadCount = rating.downloadCount + rating.anonymousDownloadCount;
}
});
}
res.render('rommanager/developers', {
title: 'ClockworkMod ROM Manager - ROMs',
manifests: manifests,
device: req.params.device,
deviceName: name
});
});
});
});
app.get('/tether/drivers', function(req, res) {
res.render('drivers', {
app: 'Tether',
title: 'ClockworkMod Tether'
});
});
app.get('/carbon', function(req, res) {
res.header('Cache-Control', 'max-age=300');
res.render('carbon', {
title: 'ClockworkMod Carbon'
})
});
app.get('/carbon/drivers', function(req, res) {
res.render('drivers', {
app: 'Carbon',
title: 'ClockworkMod Carbon'
});
});
app.get('/survey/ad', function(req, res) {
res.render('survey/ad', {
title: 'ClockworkMod ROM Manager - Survey'
});
});
app.get('/allcast/discover', function(req, res) {
res.redirect('https://play.google.com/store/apps/details?id=com.koushikdutta.cast');
});
var listenPort = process.env.PORT == null ? 3000 : parseInt(process.env.PORT);
app.listen(listenPort);
console.log('Express app started on port ' + listenPort);