-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.php
88 lines (69 loc) · 2.31 KB
/
rank.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
<?php
require_once 'config.php';
global $db, $genres, $countryCodes;
if (!isset($_GET['id']) || !is_numeric($_GET['id']))
die(json_encode([
'status' => false,
'result' => 'invalid id'
]));
$result = [];
$id = $_GET['id'];
$country = isset($_GET['country']) && validateCountry($_GET['country']) ? $_GET['country'] : null;
$platform = isset($_GET['platform']) && validatePlatform($_GET['platform']) ? $_GET['platform'] : null;
$lastUpdateId = $db->query("SELECT `id` FROM `updates` WHERE `active` = true ORDER BY `id` DESC")->fetch_assoc()['id'];
if (!is_numeric($lastUpdateId))
die(json_encode([
'status' => false,
'result' => 'internal error'
]));
$query = "SELECT `rank`, `apple-id`, `platform`, `type`, `genre`, `country` FROM `applications` WHERE `update_id` = $lastUpdateId AND `apple-id` = '$id' AND `active` = 1";
if ($country)
$query .= " AND `country` = '$country'";
if ($platform)
$query .= " AND `platform` = '$platform'";
$ranks = collect($db->query($query)->fetch_all(MYSQLI_ASSOC));
if ($ranks->isEmpty())
die(json_encode([
'status' => false,
'result' => 'no result'
]));
$result = $ranks->map(function ($app) {
return [
'platform' => $app['platform'],
'country' => $app['country'],
'genre' => parseGenre($app['genre']),
'type' => parseType($app['type']),
'rank' => (int) $app['rank'],
'category' => $app['genre'] == 36 ? 'all' : 'in-genre'
];
})->groupBy('country')->map(function ($groupedByCountry) {
return $groupedByCountry->groupBy('category')->map(function ($groupedByCategory) {
return $groupedByCategory->map(function ($item) {
unset($item['category']);
return $item;
});
});
});
echo json_encode([
'status' => true,
'result' => $result
]);
// Functions
function parseType($type) {
if (strstr($type, 'Free'))
return 'Free';
else if (strstr($type, 'Paid'))
return 'Paid';
else if (strstr($type, 'Revenue'))
return 'Grossing';
}
function parseGenre($genre) {
global $genres;
return $genres[$genre];
}
function validateCountry($country) {
return preg_match('/^[A-Z]{2}$/', $country);
}
function validatePlatform($platform) {
return in_array($platform, ['iphone', 'ipad', 'mac']);
}