-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_api_opm.php
executable file
·174 lines (145 loc) · 4.83 KB
/
s_api_opm.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env php
<?php
declare(strict_types=1);
namespace bin;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7;
/*
* Bin is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bin. If not, see <https://www.gnu.org/licenses/>.
*/
# TODO: cache api response to a file for future requests.
$home_dir = getenv("HOME");
require $home_dir .
DIRECTORY_SEPARATOR .
".config/composer/vendor/autoload.php";
// API
final class Response
{
public function __construct(
private string $url,
private string $config_file
) {
}
// use php's stream-handling
private function native(): string
{
$context = stream_context_create([
"http" => [
"method" => "GET",
"header" => [
"Accept: application/json",
"Content-Type: application/json",
],
],
]);
$response_json = file_get_contents($this->url, false, $context);
return $response_json;
}
private function guzzly(): string
{
try {
$client = new Client(["base_uri" => $this->url]);
$response = $client->get("");
return $response->getBody()->getContents();
} catch (ClientException $e) {
echo Psr7\Message::toString($e->getRequest());
echo Psr7\Message::toString($e->getStatusCode());
exit();
}
}
private function curly(): array
{
$handler = curl_init();
curl_setopt_array($handler, [
CURLOPT_URL => $this->url,
CURLOPT_HTTPGET => true,
CURLOPT_HTTPHEADER => ["Accept: application/json"],
CURLOPT_RETURNTRANSFER => true,
]);
$response_json = curl_exec($handler);
curl_close($handler);
return $response_json;
}
// cache response contents to a file
private function cache(string $grabbed): void
{
file_put_contents($this->config_file, $grabbed);
}
public function chapters(): ?array
{
if (file_exists($this->config_file)) {
$config_content = file_get_contents($this->config_file);
$response = json_decode($config_content, true);
return $response["chapters"];
}
$grabbed = $this->native();
if (is_null($grabbed)) {
$this->cache($grabbed);
} else {
}
return json_decode($grabbed, true)["chapters"];
}
}
// CLI
// TODO: add a proper cli options with --force, --cache, --url URL, --to DIR
$destination_dir = "";
$default_folder =
$home_dir .
DIRECTORY_SEPARATOR .
"Downloads" .
DIRECTORY_SEPARATOR .
"One Punch Man";
$destination_dir = realpath(count($argv) === 1 ? $default_folder : $argv[1]);
if (!file_exists($destination_dir)) {
mkdir($destination_dir);
}
$config_file = $destination_dir . "/opm.json";
echo "destination: ", $destination_dir, "\n";
echo "config file: ", $config_file, "\n";
# BEGIN
const URL = "https://gist.githubusercontent.com/funkyhippo/1d40bd5dae11e03a6af20e5a9a030d81/raw/?";
$chapters = (new Response(URL, $config_file))->chapters();
// ACTION
foreach ($chapters as $chapter) {
$chapter_title = "{$chapter["title"]}";
$chapter_dir = $destination_dir . DIRECTORY_SEPARATOR . $chapter_title;
// create dir of chapter if needed
if (!file_exists($chapter_dir) && !is_dir($chapter_dir)) {
echo "creating directory: {$chapter_dir}";
mkdir($chapter_dir, 0755);
}
// select pictures array
$pictures = $chapter["groups"]["/r/OnePunchMan"];
// Check if folder has at least one pic
// so to avoid API pics naming change.
if (count(scandir($chapter_dir)) <= 2) {
foreach ($pictures as $picture) {
$pic_name_decoded = base64_decode(basename($picture));
$pic_real_name = substr(
$pic_name_decoded,
strpos($pic_name_decoded, "_") + 1
);
$pic_path = $chapter_dir . DIRECTORY_SEPARATOR . $pic_real_name;
// download file only if needed
if (!file_exists($pic_path)) {
echo "\nGetting pic: {$pic_path}";
file_put_contents($pic_path, file_get_contents($picture));
}
}
} else {
echo "DONE: {$chapter_title}\n";
}
}