forked from php-youtubers/directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
132 lines (102 loc) Β· 3.95 KB
/
update.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
<?php
$apiKey = 'AIzaSyBirL9XMWHNxuVUUGPBdSqu9UxqcY06dVo';
$content = file_get_contents('README.md');
$lines = explode("\n", $content);
$youtubers = [];
function getFollowers($url, $apiKey)
{
preg_match('/channel_id=([a-zA-Z0-9_-]+)/', get_content_with_retry($url, 3), $matches);
$channelId = $matches[1];
$json_url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id={$channelId}&key={$apiKey}";
$data = json_decode(get_content_with_retry($json_url, 3), true);
echo "got {$data['items'][0]['statistics']['subscriberCount']} subs for {$url} \n";
return $data['items'][0]['statistics']['subscriberCount'];
}
function get_content_with_retry($url, $maxRetries)
{
$retryCount = 0;
while ($retryCount < $maxRetries) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 15); //timeout in seconds
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($content === false || $info['http_code'] != 200) {
$retryCount++;
sleep(5);
} else {
return $content;
}
}
throw new Exception('Failed after ' . $maxRetries . ' attempts.');
}
function parseWithFollowers($line, $apiKey): array
{
$handle = substr($line, strpos($line, '[') + 1, strpos($line, ']') - (strpos($line, '[') + 1));
$url = substr($line, strpos($line, '(https://') + 1, strpos($line, ')**') - (strpos($line, '(https://') + 1));
$segments = explode(' β§ ', substr($line, strpos($line, '**:') + 4));
$description = null;
$name = null;
if (count($segments) === 3) {
// Line Format: followers β§ name β§ description
$name = $segments[1] ?? null;
$description = $segments[2] ?? null;
} elseif (count($segments) === 2) {
// Line Format: followers β§ description
$description = $segments[1] ?? null;
}
$followers = getFollowers($url, $apiKey);
return compact('handle', 'url', 'name', 'description', 'followers');
}
function parseWithoutFollowers($line, $apiKey): array
{
$handle = substr($line, strpos($line, '[') + 1, strpos($line, ']') - (strpos($line, '[') + 1));
$url = substr($line, strpos($line, '(https://') + 1, strpos($line, ')**') - (strpos($line, '(https://') + 1));
$descriptionAndName = substr($line, strpos($line, '**:') + 4);
$splitPos = strpos($descriptionAndName, ' β§ ');
if ($splitPos !== false) {
$name = substr($descriptionAndName, 0, $splitPos);
$description = substr($descriptionAndName, $splitPos + 3);
} else {
$name = null;
$description = $descriptionAndName;
}
$followers = getFollowers($url, $apiKey);
return compact('handle', 'url', 'name', 'description', 'followers');
}
foreach ($lines as $line) {
if (empty($line = trim($line))) {
continue;
}
if (preg_match('/[0-9.]*[KM]? β§/i', $line)) {
$youtubers[] = parseWithFollowers($line, $apiKey);
} else {
$youtubers[] = parseWithoutFollowers($line, $apiKey);
}
}
uasort($youtubers, function ($a, $b) {
return $b['followers'] <=> $a['followers'];
});
function followersCount($count)
{
if ($count > 1000000) {
return round($count / 1000000, 1) . 'M';
}
if ($count > 1000) {
return round($count / 1000, 1) . 'K';
}
return $count;
}
$sortedList = '';
foreach ($youtubers as $youtuber) {
if ($youtuber['name'] !== null) {
$description = followersCount($youtuber['followers']) . " β§ {$youtuber['name']} β§ {$youtuber['description']}";
} else {
$description = followersCount($youtuber['followers']) . " β§ " . $youtuber['description'];
}
$sortedList .= "- **[{$youtuber['handle']}](https://www.youtube.com/{$youtuber['handle']})**: {$description}\n";
}
file_put_contents('README.md', $sortedList);