-
Notifications
You must be signed in to change notification settings - Fork 0
/
GifGrabber.php
executable file
·70 lines (58 loc) · 1.7 KB
/
GifGrabber.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
<?php
require_once('libs/simple_html_dom.php');
/**
* This class scrapes the given website and saves all the gifs to a folder.
* It depends on simple_html_dom library (http://simplehtmldom.sourceforge.net/)
* @since 20-04-2013
* @author Maik Diepenbroek
*
*/
class GifGrabber{
private $file_extension = "gif";
private $gifUrls = array();
private $gifFolder = "gifs";
private $pagesAmount = 2;
private $websiteUrl = "http://thejoysofcode.com/page/";
public function __construct() {
for($i = 1; $i <= $this->pagesAmount; $i++) {
$this->gifUrls[$i] = $this->getGifsFromPage( file_get_html($this->websiteUrl.$i),
$this->getTitleFromGif(file_get_html($this->websiteUrl.$i)) );
}
$this->saveGifsToFolder();
}
private function getTitleFromGif($gif) {
$page = $gif->find('h2 a');
$hrefs = [];
foreach($page as $href) {
$hrefs[] = ucfirst(str_replace("-", " ", substr($href->href,42))) ;
}
return $hrefs;
}
private function getGifsFromPage($page, $gifTitles) {
$gifsFromPage = array();
$i = 0;
foreach($page->find('img') as $element) {
if($this->get_file_extension($element->src) == "gif") {
$gifInGifs = array();
$gifInGifs['title'] = $gifTitles[$i];
$gifInGifs["gifUrl"] = $element->src;
$gifsFromPage[] = $gifInGifs;
$i++;
}
}
array_pop($gifsFromPage);
return $gifsFromPage;
}
private function saveGifsToFolder() {
foreach($this->gifUrls as $gifs) {
foreach($gifs as $gifInGifs) {
file_put_contents('gifs/'.$gifInGifs['title'].'.gif', file_get_contents($gifInGifs['gifUrl']));
}
}
}
private function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
}
$grabber = new GifGrabber();
?>