/
scrape.php
275 lines (229 loc) · 7.54 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
include_once "simplehtmldom_1_5/simple_html_dom.php";
class Mog {
public $name;
public $srcOrigin;
public $imgUrl;
// public $localPath;
public $srcViews;
public $srcFaves;
public $srcUrl;
public $rating;
public $rateBias;
public $active;
public function __construct($name, $imgUrl, $srcViews, $srcFaves, $srcUrl, $active) {
$this->name = $name;
// $this->srcOrigin = $srcOrigin;
$this->imgUrl = $imgUrl;
// $this->localPath = $localPath;
$this->srcViews = $srcViews;
$this->srcFaves = $srcFaves;
$this->rating = ($srcViews + $srcFaves);
$this->srcUrl = $srcUrl;
$this->active = $active;
}
public function saveToDB() {
try {
$db = new PDO('mysql:host=localhost;dbname=MemeSlamDev;charset=utf8','root','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db->prepare("insert into MogMaster (name, img_url, src_url, rating, active)
values (:name, :imgUrl, :srcUrl, :rating, :active)");
$query->execute(['name'=>$this->name, 'imgUrl'=>$this->imgUrl, 'srcUrl'=>$this->srcUrl, 'rating'=>$this->rating, 'active'=>$this->active]);
} catch (PDOException $e) {
die($e->getMessage());
}
}
public function saveToCSV($filename) {
$line = [];
$line[] = $this->name;
$line[] = $this->srcUrl;
$line[] = $this->rating;
$line[] = $this->active;
$csv = fopen($filename, 'a') or die("Unable to open the file!");
fputcsv($csv, $line);
fclose($csv);
}
}
mainExecute();
/*********************************
Functions for work
**********************************/
//Main Logic Function
function mainExecute() {
//support variables for logic
$matches = [];
$alt_dom = '';
$i = 0;
$memeIDCounter = 1;
$page_count = 50;
$csvFileName = 'mogmaster.csv';
$toCSV = true;
//array for offline pages testing
// $offlinePages = ['staticpages/Slender Man _ Know Your Meme.html','staticpages/Forever Alone _ Know Your Meme.html', 'staticpages/Zerg Rush _ Know Your Meme.html'];
//Regexes for matching values from extractions
$rgx_src = '/data-src="(.*)" src/'; //image url
$rgx_title = '/title="(.*)"/'; //Meme title
$rgx_pg_href = '/href="(.*)">/'; //url for meme's page
$rgx_faves = '/>(.*)</'; //favorites count
$rgx_views = '/>(.*)</'; //View count
$rgx_origin = '/>(.*)</'; //Origin
$rgx_org_year = '';
$rgx_nsfw = '/>NSFW</';
//utility variables specifying desired Dom content selectors for extractContent function
$meme_img_path = '.entry_list .photo img';
$meme_url_path = '.entry_list h2 > a';
$meme_faves_path = '.num';
$meme_views_path = 'dd.views a';
$meme_origin_path = 'dd.entry_origin_link';
$meme_nsfw_path = 'span.label-nsfw';
//Variables to store scraped content
$meme_name = '';
$meme_img_url = '';
// $meme_localPath = '';
$meme_faves = null;
$meme_views = null;
$meme_origin = null;
$meme_year = null;
$meme_learn_more = '';
//Recreate CSV file
createCSV($csvFileName);
//main work loop
while ($i <= $page_count) {
//counter for tracking meme url index in $meme_href
$j = 0;
//counter used for offline pages array index
// $m = 0;
//pull and store scraped dom
$html = getDOM('http://knowyourmeme.com/memes/popular/page/' . ($i + 1));
delay();
//These arrays should refer to the same memes on the same indexes
//extracts array used for meme images and titles
$img_content = extractContent($html, $meme_img_path);
//extracts array used for meme url to access additional content
$meme_href = extractContent($html, $meme_url_path);
foreach ($img_content as $curr) {
//check if meme is NSFW
$meme_nsfw = extractContent($curr, $meme_nsfw_path);
if(!empty($meme_nsfw)) {
$active = 0;
} else {
$active = 1;
}
echo "Meme NSFW: $active" . PHP_EOL;
// //save meme name
$meme_name = getValue($curr, $rgx_title, false, '');
preg_match($rgx_title, $curr, $matches);
$meme_name = $matches[1];
echo "Meme Name: " . $meme_name . PHP_EOL;
//save meme img url
$meme_img_url = getValue($curr, $rgx_src, false, '');
preg_match($rgx_src, $curr, $matches);
$meme_img_url = $matches[1];
echo "Meme IMG URL: " . $meme_img_url . PHP_EOL;
//get href for current meme in $curr and set variable
preg_match($rgx_pg_href, $meme_href[$j], $matches);
$meme_learn_more = "http://knowyourmeme.com" . $matches[1];
echo "Meme Main URL: " . $meme_learn_more . PHP_EOL;
//Get DOM for current selected meme to scrape additional content
//for offline testing
$alt_dom = getDOM("http://knowyourmeme.com" . $matches[1]);
delay();
// $alt_dom = getDOM($offlinePages[$m]);
//extract favorite count
$meme_faves = getValue($alt_dom, $rgx_faves, true, $meme_faves_path);
$fave_segment = extractContent($alt_dom, $meme_faves_path);
preg_match($rgx_faves, $fave_segment[0], $matches);
$meme_faves = $matches[1];
echo "Meme Favorite Count: ".$meme_faves . PHP_EOL;
//extract view count
$view_segment = extractContent($alt_dom, $meme_views_path);
preg_match($rgx_views, $view_segment[0], $matches);
$meme_views = $matches[1];
echo "Meme Views: " . $meme_views . PHP_EOL;
//Save image file to local path and collect path for saving
saveImg($meme_img_url, $memeIDCounter);
$memeIDCounter++;
// //extract meme origin
// $origin_segment = extractContent($alt_dom, $meme_origin_path);
// preg_match($rgx_origin, $origin_segment[0], $matches);
// $meme_origin = $matches[1];
// echo "Meme Origin: " . $meme_origin . "<br>";
echo PHP_EOL;
//create Mog object and call save method
$mog = new Mog($meme_name, $meme_img_url, $meme_views, $meme_faves, $meme_learn_more, $active);
if($toCSV) {
$mog->saveToCSV($csvFileName);
} else {
$mog->saveToDB();
}
echo "Total Memes Scraped: $memeIDCounter" . PHP_EOL;
// $m++;
$j++;
}
echo 'Page ' . ($i + 1) . 'Done...' . PHP_EOL;
echo PHP_EOL;
$i++;
sleep(rand(15,60));
}
echo "all pages done!" . PHP_EOL;
}
//Get's DOM at given url
function getDOM($given) {
//set user agent (to avoid 403 errors) and save html from set destination
ini_set('user_agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36');
$html = file_get_html($given);
return $html;
}
//Returns extraction of content given dom and selector
function extractContent($given_html, $selector) {
$result = $given_html->find($selector);
return $result;
}
//Gets desired content given arguments
function getValue($dom, $rgx, $extract, $selector) {
if ($extract) {
$segment = extractContent($dom, $selector);
preg_match($rgx, $segment[0], $matches);
} else {
$segment = $dom;
preg_match($rgx, $segment, $matches);
}
if (!empty($matches)){
$result = scrubMatch($matches[1]);
} else {
$result = 0;
}
return $result;
}
//scrubs html entities from match
function scrubMatch($given) {
$result = $given;
if (substr($given,0,1) == '<') {
$result = substr($given, (strpos($given,'>') + 1), (strrpos($given,'<')));
}
return $result;
}
//Sleeps scraper to avoid accidental DDOS
function delay() {
$min = 2;
$max = 5;
sleep(rand($min, $max));
}
//saves scraped image file
function saveImg($given, $index) {
$ch = curl_init($given);
$path = "../../database/img/mogs/$index";
$fp = fopen($path, 'wb');
$path = "database/img/mogs/$index";
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
//creates csv file for storing scraped information
function createCSV($filename) {
$csv = fopen($filename, "w") or die("Unable to open file!");
fwrite($csv, 'name,src_url,rating,active' . PHP_EOL);
fclose($csv);
}