-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.php
110 lines (91 loc) · 3.06 KB
/
script.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
<?php
class Clips
{
protected static ?Clips $instance = null;
protected array $config = [];
protected array $items = [];
public static function instance(): ?Clips
{
if (self::$instance === null) {
self::$instance = new Clips;
}
return self::$instance;
}
protected function line(string $string)
{
echo $string.PHP_EOL;
}
protected function __construct()
{
$this->line('Start');
$this->config = require_once __DIR__.'/config.php';
$this->getClips();
}
protected function getClips(?string $after = null)
{
$this->line('Make a Twitch API request');
$curl = curl_init();
if ($after) {
sleep($this->config['sleep']);
}
$query = [
'broadcaster_id' => $this->config['broadcaster_id'],
'first' => 100,
];
if ($after) {
$query = array_merge($query, [
'after' => $after,
]);
}
if ($this->config['started_at']) {
$query = array_merge($query, [
'started_at' => $this->config['started_at'],
]);
}
if ($this->config['ended_at']) {
$query = array_merge($query, [
'ended_at' => $this->config['ended_at'],
]);
}
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.twitch.tv/helix/clips?'.http_build_query($query),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'client-id: '.$this->config['client_id'],
'Authorization: Bearer '.$this->config['token']
],
]);
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
$items = $data['data'];
foreach ($items as $item) {
if ($item['game_id'] == $this->config['game_id']) {
$this->items[] = [
'title' => $item['title'],
'duration' => $item['duration'],
'view_count' => $item['view_count'],
'url' => $item['url'],
'creator_name' => $item['creator_name'],
'creator_id' => $item['creator_id'],
'created_at' => $item['created_at'],
'thumbnail_url' => $item['thumbnail_url'],
];
}
}
if (!empty($data['pagination']['cursor'])) {
$this->getClips($data['pagination']['cursor']);
return;
}
$this->line('Write file');
file_put_contents('data.json', json_encode($this->items));
$this->line('Finished');
}
}
Clips::instance();