-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.php
133 lines (105 loc) · 4.2 KB
/
updater.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
set_time_limit(0);
include 'config.php';
global $countryCodes, $genres, $chartTypes, $notificationsLink, $globalHeaders, $db;
$responses = [];
$errors = [];
$insertUpdate = $db->query("INSERT INTO `updates` (`created_at`) VALUES (UNIX_TIMESTAMP())");
if (!$insertUpdate)
die('internal error');
$updateId = $db->insert_id;
$links = makeUrls();
foreach ($links as $link) {
$response = json_decode(execCurl($link['url']));
if (!$response)
$errors[] = [
'url' => str_replace('https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?limit=100&', '', $link['url']),
'response' => $response
];
else {
if (empty($response->resultIds))
continue;
$values = '';
$time = time();
foreach ($response->resultIds as $index => $id) {
$rank = $index + 1;
$values .= "($updateId, '$id', $rank, '$link[platform]', '$link[type]', '$link[genre]', '$link[country]', '$time'), ";
}
// Remove last comma from value tuples and make query
$query = "INSERT INTO `applications` (`update_id`, `apple-id`, `rank`, `platform`, `type`, `genre`, `country`, `created_at`) VALUES " . substr($values, 0 , -2);
$insert = $db->query($query);
if (!$insert)
$responses[] = [
'url' => str_replace('https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?limit=100&', '', $link['url']),
'query' => $query,
'response' => $db->error
];
}
}
$estimatedCount = (count($links) * 100) - 700; // China region doesn't have News category, so 7 requests will returns null
$negligibleCountOfMissingRanks = 0.1 * $estimatedCount;
$insertedCount = (int) $db->query("SELECT COUNT(`id`) as `count` FROM `applications` WHERE update_id = '$updateId'")->fetch_assoc()['count'];
// If less than X percent of data is incorrect, mark update as a valid update
$isValid = ($estimatedCount - $insertedCount) <= $negligibleCountOfMissingRanks;
if ($isValid) {
$db->query("UPDATE `updates` SET `active` = 1, `count` = '$insertedCount' WHERE `id` = '$updateId'");
$db->query("DELETE FROM `applications` WHERE update_id != '$updateId'");
}
$validation = [
'update_id' => $updateId,
'estimated_count' => $estimatedCount,
'negligible_missing_count' => $negligibleCountOfMissingRanks,
'inserted_count' => $insertedCount,
'missing_count' => $estimatedCount - $insertedCount,
'is_valid' => $isValid
];
file_put_contents('errors.txt', json_encode($errors));
file_put_contents('responses.txt', json_encode($responses));
file_put_contents('validation.txt', json_encode($validation));
if (!$isValid) exec("curl $notificationsLink");
echo json_encode([
'validation' => $validation,
'errors' => $errors,
'queries' => $responses
]);
function execCurl($url) {
global $globalHeaders;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $globalHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function makeUrls() {
global $genres, $countryCodes, $chartTypes;
$genreKeys = array_keys($genres);
$urls = [];
foreach ($countryCodes as $country)
foreach ($genreKeys as $genre)
foreach ($chartTypes as $type) {
$platform = stristr($type, 'ipad') ? 'ipad' : ($type === 'FreeMacAppsV2' ? 'mac' : 'iphone');
$urls[] = [
'country' => $country,
'genre' => $genre,
'type' => $type,
'platform' => $platform,
'url' => buildUrl($country, $genre, $type)
];
}
return $urls;
}
function buildUrl($country, $genre, $type, $limit = 100) {
$prefix = 'https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/charts?';
// @TODO: make url manually instead of using http_build_query
return
$prefix .
http_build_query([
'limit' => $limit,
'cc' => $country,
'g' => $genre,
'name' => $type
]);
}