-
Notifications
You must be signed in to change notification settings - Fork 0
/
ats_iphone_ipad_local.js
170 lines (133 loc) · 4.81 KB
/
ats_iphone_ipad_local.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
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var http = require('http');
var app = express();
// Globals
var num_of_games = 200;
// Output globals
var output = [];
var output_line = 0;
// Step 1: get all games from both iphone and ipad
var types = { iPhone : 'http://www.appninjaz.com/appsense/index.php?chart=topfreeapplications&country=us&genre=7006',
iPad: 'http://www.appninjaz.com/appsense/index.php?chart=topfreeipadapplications&country=us&genre=7006' }
var singles_lists = {};
var ids_to_names = [];
var types_completed = 0;
// Step 2: check if games are really not in USA (maybe has lower rank than 200)
var ids_to_check = {};
var ids_to_check_count = 0;
function checkIfScriptIsDone() {
ids_to_check_count--;
if (!ids_to_check_count) {
// No more ids to check. write to file and exit.
fs.writeFile('iphone_ipad_output.csv', output, function(err){
console.log('File successfully written! - Check your project directory for the iphone_ipad_output.csv file');
});
console.log("Done comparison.");
}
}
function getGameInfo(id, kind) {
var options = {
host: 'itunes.apple.com',
path: '/lookup?id=' + id + '&country=us'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
str = JSON.parse(str);
var rawDate = str["results"][0]["releaseDate"].split("T")[0].split("-");
var date = ("0" + rawDate[1]).slice(-2) + "/" + ("0" + rawDate[2]).slice(-2) + "/" + rawDate[0];
output[output_line++] = kind + " only," + id + "," + ids_to_names[id] + "," + str["results"][0]["artistName"].replace(/,/g," ") + "," + date + "\n";
checkIfScriptIsDone();
});
}
http.request(options, callback).end();
}
function checkIfGameHasKind(id, kind) {
var options = {
host: 'itunes.apple.com',
path: '/lookup?id=' + id + '&country=us'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
str = JSON.parse(str);
// console.log(id + " " +str)
if (str["results"][0]["supportedDevices"].indexOf(kind) == -1) {
// Game is not of kind 'kind'
getGameInfo(id, kind);
} else {
checkIfScriptIsDone();
}
});
}
http.request(options, callback).end();
}
function findSingles() {
var iphone = singles_lists["iPhone"];
var ipad = singles_lists["iPad"];
output[output_line++] = ",Type, ID, Name, Company, Release date\n";
ids_to_check["iPhone"] = [];
for (var id in iphone) {
var game_id = iphone[id];
if (ipad.indexOf(game_id) == -1 ) {
ids_to_check_count++;
ids_to_check["iPhone"].push(game_id);
}
}
ids_to_check["iPad"] = [];
for (var id in ipad) {
var game_id = ipad[id];
if (iphone.indexOf(game_id) == -1 ) {
ids_to_check_count++;
ids_to_check["iPad"].push(game_id);
}
}
console.log("Checking candidates...");
for (var id in ids_to_check["iPhone"]) {
checkIfGameHasKind(ids_to_check["iPhone"][id], "iPad");
}
for (var id in ids_to_check["iPad"]) {
checkIfGameHasKind(ids_to_check["iPad"][id], "iPhone");
}
}
function getGamesList ( type ) {
var url = types[type];
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
// skip children 0 because it's titles row
singles_lists[type] = [];
for (var i = 1; i<=num_of_games; i++) {
var id = $("#game_name_" + i)[0]["attribs"]["href"].split("=")[1];
var name = $("#game_name_" + i).text().replace(/,/g," ");
ids_to_names[id] = name;
singles_lists[type].push(id);
}
} else {
console.log(error);
}
types_completed++;
console.log('Done loading list for ' + type + " (" + types_completed + "/" + Object.keys(types).length + ")");
if (types_completed == Object.keys(types).length) {
findSingles();
}
});
}
console.log('Running iPhone vs. iPad script...');
for (var type in types) {
getGamesList(type);
}
exports = module.exports = app;