-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalls_scrapper.php
69 lines (59 loc) · 2.62 KB
/
walls_scrapper.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
<?php
include_once 'utilities.php';
use FastSimpleHTMLDom\Document;
// searching wallpapers
function searchWallpaper($search) {
$search_query = str_replace(' ', '+', $search);
$search_url = "https://alpha.wallhaven.cc/search?q=" . $search_query;
echo "\nSearching " . $search . " on Wallhaven...\nSearch URL is : " . $search_url . "\n\n";
//getting data of the search url
$webpage_data = get_data($search_url);
$html = new Document();
$html->loadHtml($webpage_data);
// getting all classes with preview tags
$tags = $html->find('.preview');
foreach ($tags as $tag) {
$link = $tag->href . "\n"; // getting hrefs of preview class
$walls_array[] = $link; // saving hrefs an array
}
return $walls_array; //returning Array with all href links
}
function downloadWallpaper($walls_array) {
echo "\n\nDownloading Wallpapers... \n\n";
// iterating through every link in walls array
foreach ($walls_array as $key => $value) {
// getting just the image id present in a href
$image_id = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
//adding image id to the url
$url = "http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-" . $image_id[0][0];
/* Now the url needs an extension at the end. On wallhaven it can be
.jpg, .png, .bmp
storing these 3 in an array
and concatinating each one of them. and gettinf file contents.
if file contents returns 404, skipping the link else saving the image.
*/
$extensions = ['.jpg', '.png', '.bmp'];
foreach ($extensions as $extension) {
/*In PHP . is the concatenation operator which returns the
concatenation of its right and left arguments
*/
$download_url = $url . $extension; // concatinating iamge with extension
$file = @file_get_contents($download_url);
// getting file contents for the download_url
// if file_get_contents returns error, @ will return false
// if returned false -> moce to second link (if condition)
// if returns true -> download wallpaper (else condition)
if ($file === false) {
continue;
} else {
echo "Downloading Wallhaven-" . $image_id[0][0] . $extension;
echo "\n";
//path of wallpaper
$path = 'images/Wallhaven-' . $image_id[0][0] . $extension;
// putting contents of file in path
file_put_contents($path, file_get_contents($download_url));
}
}
}
}
?>