-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawler.php
More file actions
115 lines (76 loc) · 3.17 KB
/
crawler.php
File metadata and controls
115 lines (76 loc) · 3.17 KB
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
<?php
$newLine = "<br>\r\n";
require_once("partials/functions.php");
//timeExecution("Setup");
connect();
$lastRecord = getLastMeta();
//timeExecution("Query DB");
$playlist = file_get_contents($mediaPath . $playlistFilename);
//timeExecution("Read playlist");
$pos = strrpos($playlist, ",");
$fileName = trim(substr($playlist, $pos + 2));//get last file
if(!$lastRecord || $lastRecord->filename != $fileName){
$file = file_get_contents($mediaPath . $fileName, false,NULL,-1, 3000);
//timeExecution("Read track");;
$albumPos = strpos($file, $albumAnchor);
$artistPos = strpos($file,$artisAnchor);
$titlePos = strpos($file, $titleAnchor);
//$artworkPos = strpos($file, $artworkAnchor);
//$extentionPos = strpos($file, $extentionAnchor, $artworkPos);
if($titlePos){
$title = stripRandomChars(substr($file, $titlePos + strlen($titleAnchor), 100));
logText("Title: ".$title . " (". strlen($title).")");
}
$album = "";
if($albumPos && $artistPos){
$album =stripRandomChars(substr($file, $albumPos + strlen($albumAnchor), $artistPos - $albumPos-strlen($albumAnchor)));
logText("Album: ".$album . " (". strlen($album).")" );
}
if($artistPos && $titlePos){
$artist =stripRandomChars(substr($file, $artistPos + strlen($artisAnchor), $titlePos - $artistPos-strlen($artisAnchor)));
logText("Artist: ".$artist . " (". strlen($artist).")");
}
// else{
// logText("Not inough info");
// return;
// }
// if($artworkPos && $extentionPos){
// $artwork = stripRandomChars(substr($file,$artworkPos + strlen($artworkAnchor), $extentionPos + strlen($extentionAnchor) - $artworkPos-strlen($artworkAnchor)));
// logText("Artwork: ". $artwork);
// }
//timeExecution("Parse info");
if(isset($title) && isset($artist) && strlen($title) > $MIN_LENGTH && strlen($artist) > $MIN_LENGTH){
if(!$lastRecord || $lastRecord->title != $title ){
insertRecord($fileName, $title, $album, $artist);
//timeExecution("Insert record");
//iTunes API request
$term = urlencode($artist . ((isset($album) && strlen($album) > $MIN_LENGTH)?(" " . $album):"") . " " . $title);
// //echo $term;
$iTunesJSON = file_get_contents('http://itunes.apple.com/search?term='.$term.'&media=music&entity=song&limit=1');
$iTunesData = json_decode($iTunesJSON, true);
if($iTunesData["resultCount"] > 0){
$iTunesMetadata = $iTunesData["results"][0];
//timeExecution("Pull iTunes data");
insertMedia($iTunesMetadata);
//timeExecution("Add media");
}else{
logText("No info at iTunes API for " . $term);
}
//$content = "<div style='background-image: url({$artwork});'><h1>{$artist} - {$title}</h1></div>";
// $contentFile = fopen("content.php", "w");
// fwrite($contentFile, $content);
// fclose($contentFile);
}elseif($lastRecord){
//UPDATE filename in DB
updateRecord($lastRecord->id, $fileName);
//timeExecution("Update record");
logText("Skipping track that is already captured");
}
}else{
logText("Not enough meta info in the file");
//logText($file);
}
}else{
logText("Skipping file that is already captured");
}
disconnect();