E
_Emma_
Original poster
Использоваться скрипт будем следущим способом: php parser.php ссылка директория.
Вначале обрабатываем аргументы,всего их 3
1 - ввод имени файла
2 - для ссылки
3 - директория для картинок ( является необязательной )
Меняем конец ссылки на .json для работы с API,а также создаём массив для этих ссылок
$imageLinks = [];
$request = preg_split('~(html)$~', $url, null, PREG_SPLIT_NO_EMPTY);
$result = json_decode(file_get_contents($request[0] . 'json'), true);
$request = preg_split('~(html)$~', $url, null, PREG_SPLIT_NO_EMPTY);
$result = json_decode(file_get_contents($request[0] . 'json'), true);
Записываем в массив все ссылки на картинкеДокументация находится на сайте php.net также доступна на русском языке
$threads = $result['threads'][0];
foreach ($threads as $thread) {
foreach ($thread as $posts) {
foreach ($posts as $key => $value) {
if ($key == 'files') {
foreach ($value as $path) {
$imageLinks[] = '' . $path['path'];
}
}
}
}
}
foreach ($threads as $thread) {
foreach ($thread as $posts) {
foreach ($posts as $key => $value) {
if ($key == 'files') {
foreach ($value as $path) {
$imageLinks[] = '' . $path['path'];
}
}
}
}
}
Создаём функуцию сохранения картинок.
Она принимает массив ссылок и директорию вывода.
function saveImages($imageLinks, $imagesDirectory) {
$extensionPattern = '~(https:\/\/2ch.hk\/[a-z]+\/[a-z]+\/[0-9]+\/[0-9]+)~';
$imageNamePattern = '~(
foreach ($imageLinks as $imageLink) {
$imageName = preg_split($imageNamePattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$extension = preg_split($extensionPattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$imageName = $imageName[0];
$extension = $extension[0];
if ($extension == '.webm' || $extension == '.mp4' || $extension == '.gif') {
print_r($imageName . " will not be saved!\n");
continue;
} else {
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $imagesDirectory . DIRECTORY_SEPARATOR . $imageName, file_get_contents($imageLink));
print_r($imageName . " was saved successfully\n");
}
}
}
$extensionPattern = '~(https:\/\/2ch.hk\/[a-z]+\/[a-z]+\/[0-9]+\/[0-9]+)~';
$imageNamePattern = '~(
foreach ($imageLinks as $imageLink) {
$imageName = preg_split($imageNamePattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$extension = preg_split($extensionPattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$imageName = $imageName[0];
$extension = $extension[0];
if ($extension == '.webm' || $extension == '.mp4' || $extension == '.gif') {
print_r($imageName . " will not be saved!\n");
continue;
} else {
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $imagesDirectory . DIRECTORY_SEPARATOR . $imageName, file_get_contents($imageLink));
print_r($imageName . " was saved successfully\n");
}
}
}
Объявляем 2 регулярных выражения которые отвечают за расширение картинки а также имени.
if (file_exists($imagesDirectory)) {
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
} else {
mkdir($imagesDirectory);
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
}
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
} else {
mkdir($imagesDirectory);
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
}
Весь код
<?php
if (isset($argv[1])) {
$url = $argv[1];
} else {
print_r("Usage: php {$argv[0]} url output_directory(optional)\n");
exit("\n\tExit with status: wrong script usage");
}
if (isset($argv[2])) {
$imagesDirectory = $argv[2];
} else {
$imagesDirectory = 'images';
}
$imageLinks = [];
$request = preg_split('~(html)$~', $url, null, PREG_SPLIT_NO_EMPTY);
$result = json_decode(file_get_contents($request[0] . 'json'), true);
$threads = $result['threads'][0];
foreach ($threads as $thread) {
foreach ($thread as $posts) {
foreach ($posts as $key => $value) {
if ($key == 'files') {
foreach ($value as $path) {
$imageLinks[] = '' . $path['path'];
}
}
}
}
}
function saveImages($imageLinks, $imagesDirectory) {
$extensionPattern = '~(https:\/\/2ch.hk\/[a-z]+\/[a-z]+\/[0-9]+\/[0-9]+)~';
$imageNamePattern = '~(
foreach ($imageLinks as $imageLink) {
$imageName = preg_split($imageNamePattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$extension = preg_split($extensionPattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$imageName = $imageName[0];
$extension = $extension[0];
if ($extension == '.webm' || $extension == '.mp4' || $extension == '.gif') {
print_r($imageName . " will not be saved!\n");
continue;
} else {
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $imagesDirectory . DIRECTORY_SEPARATOR . $imageName, file_get_contents($imageLink));
print_r($imageName . " was saved successfully\n");
}
}
}
if (file_exists($imagesDirectory)) {
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
} else {
mkdir($imagesDirectory);
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
}
if (isset($argv[1])) {
$url = $argv[1];
} else {
print_r("Usage: php {$argv[0]} url output_directory(optional)\n");
exit("\n\tExit with status: wrong script usage");
}
if (isset($argv[2])) {
$imagesDirectory = $argv[2];
} else {
$imagesDirectory = 'images';
}
$imageLinks = [];
$request = preg_split('~(html)$~', $url, null, PREG_SPLIT_NO_EMPTY);
$result = json_decode(file_get_contents($request[0] . 'json'), true);
$threads = $result['threads'][0];
foreach ($threads as $thread) {
foreach ($thread as $posts) {
foreach ($posts as $key => $value) {
if ($key == 'files') {
foreach ($value as $path) {
$imageLinks[] = '' . $path['path'];
}
}
}
}
}
function saveImages($imageLinks, $imagesDirectory) {
$extensionPattern = '~(https:\/\/2ch.hk\/[a-z]+\/[a-z]+\/[0-9]+\/[0-9]+)~';
$imageNamePattern = '~(
foreach ($imageLinks as $imageLink) {
$imageName = preg_split($imageNamePattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$extension = preg_split($extensionPattern, $imageLink, null, PREG_SPLIT_NO_EMPTY);
$imageName = $imageName[0];
$extension = $extension[0];
if ($extension == '.webm' || $extension == '.mp4' || $extension == '.gif') {
print_r($imageName . " will not be saved!\n");
continue;
} else {
file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . $imagesDirectory . DIRECTORY_SEPARATOR . $imageName, file_get_contents($imageLink));
print_r($imageName . " was saved successfully\n");
}
}
}
if (file_exists($imagesDirectory)) {
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
} else {
mkdir($imagesDirectory);
saveImages($imageLinks, $imagesDirectory);
print_r("\nJob Done!\n");
}