/
hivemind_music.php
371 lines (325 loc) · 13.6 KB
/
hivemind_music.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**** Reddit Music Parser **
author: Ryan Guthrie (ryanguthrie.com)
description: Parse the reddit music thread for youtube links. Add link and comment data to memcache
TODO: refactor to parse several locations. store several playlists with each parsed music
Sources: Reddit Comments, Subreddit, Boston.com Big Picture
**** ****** */
$debug = 1;
$mc = new Memcache;
$mc->connect('localhost',11211) or die("Could not connect to memcache\n");
$keylist = array();
$url = 'http://www.reddit.com/r/AskReddit/comments/b9ud4/submit_one_song_that_you_think_everyone_should/.json?sort=new&all=true';
$browser_id = 'Hivemind/1.0 sokercap@gmail.com';
//What sources to parse - pl name,url, type, media
$sources = array
(
array('recommends','http://www.reddit.com/r/AskReddit/comments/b9ud4/submit_one_song_that_you_think_everyone_should/.json?sort=new&all=true','comments',array('music'=>1) ),
array('listentothis','http://www.reddit.com/r/listentothis/.json?sort=new&all=true','subreddit',array('music'=>1,'video'=>1)),
array('funny','http://www.reddit.com/r/funny/.json?sort=new&all=true','subreddit',array('music'=>1,'video'=>1,'pics'=>1)),
array('videos','http://www.reddit.com/r/videos/.json?sort=new&all=true','subreddit',array('video'=>1)),
array('pics','http://www.reddit.com/r/pics/.json?sort=new&all=true','subreddit',array('pics'=>1)),
array('electronic','http://www.reddit.com/r/electronicmusic/.json?sort=new&all=true','subreddit',array('music'=>1,'video'=>1)),
array('bigpicture','http://www.boston.com/bigpicture/.json?sort=new&all=true','bigpicture',array('pics'=>1)),
array("itap","http://www.reddit.com/r/itookapicture/new/.json?sort=new&all=true","subreddit",array("pics"=>1)),
array("windowshots","http://www.reddit.com/r/windowshots/new/.json?sort=new&all=true","subreddit",array("pics"=>1)),
array("wallpaper","http://www.reddit.com/r/wallpaper/new/.json?sort=new&all=true","subreddit",array("pics"=>1)),
array("gonewild","http://www.reddit.com/r/gonewild/new/.json?sort=new&all=true","subreddit",array("pics"=>1,"video"=>1)),
array('video','http://www.reddit.com/r/video/.json?sort=new&all=true','subreddit',array("video"=>1)),
array('music','http://www.reddit.com/r/music/.json?sort=new&all=true','subreddit',array("music"=>1,"video"=>1)),
array('wearethemusic','http://www.reddit.com/r/WeAreTheMusicMakers/.json?sort=true&all=true','subreddit',array('music'=>1,'video'=>1,'pics'=>1)),
array('classical','http://www.reddit.com/r/classicalmusic/.json?sort=true&all=true','subreddit',array('music'=>1,'video'=>1)),
array('mixes','http://www.reddit.com/r/mixes/.json?sort=true&all=true','subreddit',array('music'=>1,'video'=>1)),
array('mashups','http://www.reddit.com/r/mashups/.json?sort=true&all=true','subreddit',array('music'=>1,'video'=>1)),
array('chill','http://www.reddit.com/r/chillmusic/.json?sort=true&all=true','subreddit',array('music'=>1,'video'=>1)),
);
foreach( $sources as $src){
beginParse($src);
sleep(1);
}
/**************** end global code ***********/
//Begin parsing an individual source based on source type
function beginParse($source){
global $keylist, $mc, $debug, $browser_id;
$ch = curl_init();
$cOptions = array
(
CURLOPT_URL=>$source[1],
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_USERAGENT=>$browser_id
);
curl_setopt_array($ch,$cOptions);
$rawURL = curl_exec($ch);
//curl_close($ch);
if($debug){ print "Fetched json with size ".strlen($rawURL)."\n";
print substr($rawURL,0,200)."\n";
if(strlen($rawURL)< 100){
print "EMPTY RESULTS: url- ".$source[1]." -ERROR: ".curl_error($ch)."\n";
}
print "\n\n";
}
curl_close($ch);
$keylist = array();
$parsed = 0;
if($source[2] == 'subreddit'){
$parsed = parseSubreddit($source,$rawURL);
}
elseif($source[2] == 'comments'){
$parsed = parseComments($source,$rawURL);
}
elseif($source[2] == 'bigpicture'){
$parsed = parseBigPicture($source,$rawURL);
}
else {
//fail - no matching source type
if($debug){ print "Source (".$source[0].") has no matching parse type - ".$source[2]." - skipping.. \n\n"; }
return;
}
if(!$parsed){
if($debug){ print "Source (".$source[0].") failed to parse - skipping.. \n\n"; }
return;
}
/*
$rawJSON = json_decode($rawURL,false);
$jsonERR = json_last_error();
if($jsonERR != JSON_ERROR_NONE){ die("Error parsing reddit json - ".$jsonERR."\n"); }
if($debug){ print "JSON parsed!\n"; }
//print $rawJSON[0]->children[0]->{'title'}."\n";
//print_r($rawJSON[0]->data->children[0]->data);
$threadData = $rawJSON[0]->data->children[0]->data;
if($debug) { print $threadData->title . "\n".$threadData->selftext."\n*********************\n\n"; }
//print_r($rawJSON[1]->data->children[1]->data->replies->data->children[0]->data);
parseChildren($rawJSON[1]->data->children);
*/
print "PLAYLIST: ".$source[0]." - ".count($keylist)." ITEMS \n";
if(count($keylist) > 0 && $source[0]){
$oldlist = $mc->get($source[0]);
print "OLD PLAYLIST: ".$source[0]." - ".count($oldlist)." ITEMS \n";
//determine which of the pulled items do not already exist in the playlist
/*$offset = 0;
while( $offset < count($keylist) && in_array($keylist[$offset],$oldlist) ){
$offset++;
}
if($oldlist && count($oldlist) > 0 && $offset < count($keylist)){
$keylist = array_merge($oldlist, array_slice($keylist, $offset) );
}*/
if(!$oldlist || count($oldlist) < 1){
print "REPLACING OLDLIST\n";
$oldlist = $keylist ;
} else {
foreach($keylist as $key){
//print "CHECKING FOR EXISTENCE OF ".$key." ... ";
if(! in_array($key, $oldlist )){
array_push($oldlist, $key);
//print "Does Not Exist!\n";
}
//else { print "Already exists!\n"; }
}
}
print "NEW PLAYLIST: ".$source[0]." - ".count($oldlist)." ITEMS \n".print_r($oldlist,1)."\n\n";
$mc->set($source[0],$oldlist);
}
}
//start parsing a subreddit
function parseSubreddit($source,$raw){
global $debug, $keylist, $mc;
$rawJSON = json_decode($raw,false);
$jsonERR = json_last_error();
if($jsonERR != JSON_ERROR_NONE){
if($debug){ print "Error parsing reddit json - ".$jsonERR."\n"; }
return 0;
}
if($debug){ print "JSON parsed!\n"; }
//each submission stored as an entry in the main data array
foreach($rawJSON->data->children as $li)
{
$entry = $li->data;
//restrict parsing to media type
$ismedia = 0;
$oembed = 0; //flag if there is compatible embedding code
$rembed = 0; //flag for embed html junk
$media = 'none';
if($source[2]['video'] ){
if( $entry->domain == 'youtube.com' ){
$ismedia = 1; }
if($entry->media && $entry->media->oembed && $entry->media->oembed->type == "rich"){ $ismedia = 1; $oembed = 1; }
if(!$oembed && isset($entry) && $entry->media_embed && isset($entry->media_embed->content) ){ $ismedia = 1; $rembed = 1; }
}
if($source[2]['music'] ){
if( $entry->domain == 'youtube.com' || $entry->domain == 'soundcloud.com' ){
$ismedia = 1; }
if($entry->media && $entry->media->oembed && ( $entry->media->oembed->type == "rich" || $entry->media->oembed->type == "video" ) ){
$ismedia = 1; $oembed = 1;}
if(!$oembed && $entry->media_embed && isset($entry->media_embed->content) ){ $ismedia = 1; $rembed = 1; }
}
if($ismedia == 0 && $source[2]['pics'] ){
$ext = substr($entry->url, strrpos($entry->url, '.') + 1);
if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif' ){
$ismedia = 1; $media = 'pic'; }
}
if($ismedia == 0){ continue; } //invalid media type, skip to next entry
$info = 0;
if($oembed){
$info = parseSubEmbed($entry); }
elseif($rembed){
if($debug){ print $entry->subreddit ." - ".$entry->title." : REMBED OK\n"; }
$info = parseSubREmbed($entry); }
elseif($ismedia && $media != 'pic' && ($source[2]['music'] || $source[2]['video'] ) ){
$info = parseSubTube($entry); }
elseif( $ismedia && $source[2]['pics'] ){
//parse picture info
$info = array (
"id"=> ($entry->subreddit."_".$entry->id),
"title_r"=>$entry->title,
"body"=>$entry->selftext,
"body_h"=>$entry->selftext,
"thumbnail"=>$entry->thumbnail,
"author"=>$entry->author,
"ups"=>$entry->ups,
"source"=>$entry->domain,
"media"=>"pic",
"downs"=>$entry->downs,
"link"=> $entry->url,
"score"=> ($entry->ups - $entry->downs)
);
}
if(!$info){ continue; } //didn't receive valid song infos
if(!$info['id']){
if($debug){ print "NO ID FOR ITEM:\n".print_r($info,1)."\n\n"; }
continue;
}
array_push($keylist,$info['id']);
$mc->set($info['id'],$info,0,259200);
//print "LOADED MEDIA: \n".print_r($info,1)."\n\n";
}
if($debug) { print "DONE LOADING PLAYLIST ".$source[0]." - ".count($keylist)." ITEMS\n\n"; }
return 1;
}
//parse embedded media
function parseSubEmbed($entry){
$thumburl = isset($entry->media->oembed->thumbnail) ? $entry->media->oembed->thumbnail : 'http://i.imgur.com/Iu8Sljx.png';
$info = array (
"id"=> ($entry->subreddit."_".$entry->id),
"title_r"=>$entry->title,
"body"=>$entry->selftext,
"body_h"=>$entry->selftext,
"author"=>$entry->author,
"ups"=>$entry->ups,
"source"=>$entry->domain,
"media"=>$entry->media->oembed->type,
"embed_html"=>$entry->media->oembed->html,
"embed" => 1,
"embed_provider"=>$entry->media->oembed->provider_name,
"embed_thumb"=> $thumburl,
"downs"=>$entry->downs,
"link"=> $entry->url,
"score"=> ($entry->ups - $entry->downs)
);
return $info;
}
function parseSubREmbed($entry){
$info = array (
"id"=> ($entry->subreddit."_".$entry->id),
"title_r"=>$entry->title,
"body"=>$entry->selftext,
"body_h"=>$entry->selftext,
"author"=>$entry->author,
"ups"=>$entry->ups,
"source"=>$entry->domain,
"media"=>'video',
"embed_html"=>$entry->media_embed->content,
"embed" => 1,
"embed_provider"=>$entry->domain,
"embed_thumb"=> $entry->thumbnail,
"downs"=>$entry->downs,
"link"=> $entry->url,
"score"=> ($entry->ups - $entry->downs) );
return $info;
}
//no embed, parse supported tube site for media link - right now youtube and soundcloud i think
function parseSubTube($entry){
return 0;
}
//start parsing a reddit comment thread
function parseComments($source,$raw){
global $debug;
$rawJSON = json_decode($raw,false);
$jsonERR = json_last_error();
if($jsonERR != JSON_ERROR_NONE){
if($debug){ print "Error parsing reddit json - ".$jsonERR."\n"; }
return 0;
}
if($debug){ print "JSON parsed!\n"; }
//print $rawJSON[0]->children[0]->{'title'}."\n";
//print_r($rawJSON[0]->data->children[0]->data);
$threadData = $rawJSON[0]->data->children[0]->data;
if($debug) { print $threadData->title . "\n".$threadData->selftext."\n*********************\n\n"; }
//print_r($rawJSON[1]->data->children[1]->data->replies->data->children[0]->data);
//start parsing each level of comments
parseChildren_Comments($source[2],$rawJSON[1]->data->children);
return 1;
}
//start parsing Boston Big Picture
function parseBigPicture($source,$raw){
return 0;
}
//Main recursive parser for Reddit Comments. Feed it the types to look for and children array and it parses for links and feeds itself any replies
function parseChildren_Comments($types,$list)
{
global $mc, $debug, $keylist;
$limit = 50;
$i = 0;
foreach($list as $li)
{
//if($i > $limit) { exit; }
if($types['video'] || $types['music'] ) {
if($li->data && $li->data->body && preg_match('/\[(.+?)\]\(http:\/\/www\.youtube\.com\/watch(?:\?|(?:\#\!))v=([\w\-]+)/S',$li->data->body,$matches) )
{
//if($debug) { print $matches[0]."\n".$matches[1]." - ".$matches[2]."\n\n"; }
$song_info = array (
"id"=>$matches[2],
"title_r"=>$matches[1],
"body"=>$li->data->body,
"body_h"=>$li->data->body_html,
"author"=>$li->data->author,
"ups"=>$li->data->ups,
"source"=>"youtube",
"media"=>"video",
"downs"=>$li->data->downs,
"link"=> ('http://www.youtube.com/watch?v='.$matches[2]),
"score"=> ($li->data->ups - $li->data->downs)
);
array_push($keylist,'youtube_'.$matches[2]);
$mc->set('youtube_'.$matches[2],$song_info,0,259200);
}
else {
if($li->data && $li->data->body_html && preg_match('/http:\/\/www\.youtube\.com\/watch\?v=([\w\-\_]+)/S',$li->data->body_html,$matches) )
{
if($debug) { print $matches[0]."\n".$li->data->body."\n\n"; }
$song_info = array (
"id"=>$matches[1],
"title_r"=>$li->data->body,
"body"=>$li->data->body,
"body_h"=>$li->data->body_html,
"author"=>$li->data->author,
"ups"=>$li->data->ups,
"source"=>"youtube",
"media"=>"video",
"link"=>$matches[0],
"downs"=>$li->data->downs,
"score"=> ($li->data->ups - $li->data->downs)
);
array_push($keylist,'youtube_'.$matches[1]);
//print "SAVING SONG ".$song_info->title_r." to MC\n";
$mc->set('youtube_'.$matches[1],$song_info,0,259200);
}
else { //print "NOO : "; print_r($li->data->body); print "\n";
}
}
}
if($li->replies && $li->replies->kind && $li->replies->kind == "Listing"){ parseChildren_Comments($types, $li->replies->data->children); }
$i++;
} //end loop
}