-
Notifications
You must be signed in to change notification settings - Fork 4
/
auto_requests.php
114 lines (101 loc) · 2.73 KB
/
auto_requests.php
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
<?php
require 'inc/main.inc';
function shuffle_assoc(&$array, $limit = null) {
$keys = array_keys($array);
shuffle($keys);
$new = [];
foreach($keys as $key) {
$new[$key] = $array[$key];
if($limit && count($new) >= $limit) {
break;
}
}
$array = $new;
return true;
}
$mongodb_query = new MongoDB\Driver\Query([]);
$rows = $mongodb_manager->executeQuery('ac.territories', $mongodb_query);
$territories = [];
$general_territory_ids = [];
foreach($rows as $row) {
// Always test US
if($row->_id == 'US') {
$general_territory_ids[] = $row->_id;
continue;
}
$territories[] = $row;
}
// Territory with fewest tested apps
usort($territories, function($a, $b) {
return $a->apps_count > $b->apps_count;
});
$territory = array_shift($territories);
$general_territory_ids[] = $territory->_id;
// Territory with most unavailable apps
usort($territories, function($a, $b) {
return $a->apps_unavailable < $b->apps_unavailable;
});
$territory = array_shift($territories);
$general_territory_ids[] = $territory->_id;
// Random territory
shuffle($territories);
$territory = array_shift($territories);
$general_territory_ids[] = $territory->_id;
$ids = [];
// Add top apps
$urls = [
'https://rss.itunes.apple.com/api/v1/us/ios-apps/top-free/all/100/explicit.json',
'https://rss.itunes.apple.com/api/v1/us/ios-apps/top-paid/all/100/explicit.json'
];
foreach($urls as $url) {
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->feed->results as $result) {
$app = get_app($result->id);
if(!$app) {
$ids[$result->id] = [];
}
}
}
// Add apps that haven't been tested in a long time
$mongodb_query = new MongoDB\Driver\Query([], [
'sort' => [
'last_ts' => 1
],
'limit' => 70
]);
$rows = $mongodb_manager->executeQuery('ac.agg_statuses', $mongodb_query);
foreach($rows as $row) {
if(!isset($ids[$row->id])) {
$ids[$row->id] = [];
}
$ids[$row->id][] = $row->territory;
}
// Add apps with high dissonance (eg unclear status)
$mongodb_query = new MongoDB\Driver\Query([], [
'sort' => [
'dissonance' => -1
],
'limit' => 30
]);
$rows = $mongodb_manager->executeQuery('ac.avg_status_dissonance', $mongodb_query);
foreach($rows as $row) {
if(!isset($ids[$row->id])) {
$ids[$row->id] = [];
}
$ids[$row->id][] = $row->territory;
}
shuffle_assoc($ids, 100);
foreach($ids as $id => $territory_ids) {
$territory_ids = array_merge($territory_ids, $general_territory_ids);
$territory_ids = array_unique($territory_ids);
foreach($territory_ids as $territory_id) {
$request = [];
$request['media'] = 'software';
$request['country'] = $territory_id;
$request['term'] = $id;
print 'Requesting ID ' . $request['term'] . ' for territory ' . $request['country'] . '...';
itunes_api_proxy_request($request);
print " done\n";
}
}