-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.php
executable file
·201 lines (178 loc) · 6.53 KB
/
scrape.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env php
<?php
/**
* # artflow.ai Portrait Scraper
*
* This script scrapes CC-BY portrait images from artflow.ai. Images in the
* galleries are featured randomly, so duplicates will become increasingly
* common as scraping continues. Filenames are generated with the slug and
* numeric ID, and are not re-downloaded if the file already exists.
*
* ## Usage
*
* This script works with the default PHP modules, plus ImageMagick. It requires
* no Composer modules.
*
* To scrape the Editor's Choice and Community Creations galleries directly, the
* script can be invoked from the command line or from a cron job using
* `./scrape.php` or `php scrape.php`.
*
* If you wish to scrape content directly from your browser (eg. the gallery of
* your own creations), you may do so by opening the web inspector and copying
* the relevant HTML snippets. You can then pipe the content directly into the
* scrape command, like with the following:
*
* # paste directly from clipboard (Mac)
* pbpaste | php scrape.php
*
* # paste directly from clipboard (Linux)
* xsel --clipboard --output | php scrape.php
*
* # paste manually into terminal
* php scrape.php <<EOF
* <paste some HTML here>
* EOF
*
* ## Self-hosting notes
*
* All scraped content is available at
* https://github.com/initiative-sh/artflow-portraits, so you should not need to
* run this yourself.
*
* As always, if you do intend to run this script yourself, please be respectful
* of target server resources. artflow.ai has graciously chosen to use a free
* license for their material and share it online, if not in the most accessible
* format.
*
* This script runs on a cron making one request per minute with a unique,
* identifiable user agent to allow the host to throttle or contact us if
* necessary. Please assign a different user agent when running it yourself
* according to the same principles.
*/
const USER_AGENT = 'github.com/initiative-sh/artflow-portraits';
/**
* Copyright © 2021 Mikkel Paulson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (stream_isatty(STDIN)) {
return scrape_listing();
} else {
$input_raw = file_get_contents('php://stdin');
if (preg_match_all('/([a-f0-9]{32})\.webp/', $input_raw, $match, PREG_PATTERN_ORDER)) {
$filenames = $match[1];
} else {
return scrape_listing();
}
if (preg_match_all("/id=\"my_work_title\".*?>([^<]*)/", $input_raw, $match, PREG_PATTERN_ORDER)) {
$text_prompts = array_values(array_filter(
$match[1],
fn($v) => $v !== "Red paint cup",
));
} else {
echo "Missing expected value: text_prompt\n";
exit(1);
}
if (preg_match_all("/twitter_div_(\d+)/", $input_raw, $match, PREG_PATTERN_ORDER)) {
$ids = $match[1];
} else {
echo "Missing expected value: id\n";
exit(1);
}
if (count($filenames) !== count($text_prompts) || count($filenames) !== count($ids)) {
echo "The number of matches for each set much be the same.\n";
echo "filenames: " . var_export($filenames, true);
echo "text_prompts: " . var_export($text_prompts, true);
echo "ids: " . var_export($ids, true);
exit(1);
}
for ($i = 0; $i < count($filenames); $i++) {
download_result($text_prompts[$i], $ids[$i], $filenames[$i]);
}
}
function scrape_single(string $text_prompt, string $id) {
$results_raw = file_get_contents(
"https://artflow.ai/check_status",
false,
stream_context_create([
"http" => [
"method" => "POST",
"content" => http_build_query(["my_work_id" => $id]),
"header" => [
"User-agent: " . USER_AGENT,
],
],
]),
);
echo trim($results_raw) . "\n";
$results = json_decode($results_raw);
if (isset($results->filename)) {
return download_result($text_prompt, $id, $results->filename);
} else {
echo "Unexpected result: " . trim(var_export($results, true));
exit(1);
}
}
function scrape_listing() {
$source_url = mt_rand(0, 9)
? "https://artflow.ai/api/show_community_work"
: "https://artflow.ai/api/show_editor_choice";
echo "Scraping {$source_url}\n";
$results_raw = file_get_contents(
$source_url,
false,
stream_context_create([
"http" => [
"method" => "POST",
"header" => [
"User-agent: " . USER_AGENT,
],
]
]),
);
echo trim($results_raw) . "\n";
$results = json_decode($results_raw);
foreach ($results as $result) {
download_result(
$result->text_prompt,
$result->id,
$result->filename,
);
}
}
function download_result(string $text_prompt, string $id, string $filename) {
$source_url = "https://artflowbucket-new.s3.amazonaws.com/generated/{$filename}.webp";
$slug = trim(preg_replace("/\W+/", "-", $text_prompt), '-');
$output_filename = sprintf(
'%s/images/%02d/%s%d.jpg',
__DIR__,
$id % 100,
$slug ? "$slug-" : '',
$id,
);
if (file_exists($output_filename)) {
echo "$output_filename already exists.\n";
} else {
echo "$output_filename <- $source_url\n";
$image = new Imagick();
$image->readImageFile(fopen($source_url, "rb"));
$image->setImageFormat("jpeg");
$image->writeImage($output_filename);
}
}