This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_for_artist_art.php
115 lines (94 loc) · 3.14 KB
/
scan_for_artist_art.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
<?php
function scan_for_artist_art() {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT artist_mbid,name FROM `v2_artist` WHERE ((last_art_check < NOW() - INTERVAL 3 DAY) OR (last_art_check IS NULL)) AND (art = '0' OR art = '2') LIMIT 50";
$result = $mysqli->query($sql_query);
if($mysqli->query($sql_query) === false) {
trigger_error('Wrong SQL: ' . $sql_query . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$num_rows = $result->num_rows;
$result->data_seek(0);
$total_artist_art = 0;
$total_artist_art_not_found = 0;
while($row = $result->fetch_assoc()){
sleep(1);
chdir(dirname(__FILE__));
$artist = rawurlencode($row['name']);
$mbid = $row['artist_mbid'];
$saved_thumb = 0;
$saved_full = 0;
$saved_large = 0;
$xml = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist={$artist}&api_key=";
$xml = @file_get_contents($xml);
if(!$xml) {
goto a;
}
$xml = new SimpleXMLElement($xml);
$xml = $xml->artist;
$thumb = $xml->image[1];
$normal = $xml->image[2];
$large = $xml->image[3];
$xlarge = $xml->image[4];
if ($xml != '') {
$image_urls = array("thumb"=>$thumb,"full"=>$normal,"large"=>$large,"xlarge"=>$xlarge);
} else {
$image_urls = array();
}
// Save each image ...
if ($image_urls['thumb'] != '') {
$url = $image_urls['thumb'];
$parts=pathinfo($url);
$ext = $parts["extension"];
$file_name = "artist/thumb/".$mbid . "." . $ext;
if(file_put_contents($file_name, fopen($url, 'r'))) {
$saved_thumb = 1;
//echo "Saved thumb size.<br/>";
}
}
if ($image_urls['full'] != '') {
$url = $image_urls['full'];
$parts=pathinfo($url);
$ext = $parts["extension"];
$file_name = "artist/".$mbid . "." . $ext;
if(file_put_contents($file_name, fopen($url, 'r'))) {
$saved_full = 1;
//echo "Saved full size.<br/>";
}
}
if ($image_urls['large'] != '') {
$url = $image_urls['large'];
$parts=pathinfo($url);
$ext = $parts["extension"];
$file_name = "artist/large/".$mbid . "." . $ext;
if(file_put_contents($file_name, fopen($url, 'r'))) {
$saved_large = 1;
//echo "Saved large size.<br/>";
}
}
a:
if ($saved_thumb && $saved_full && $saved_large) {
$filename = $mbid . "." . $ext;
// mark album as art was found
$sql_query = "UPDATE v2_artist SET
art = '$filename',last_art_check = '" . date("Y-m-d") . "'
WHERE artist_mbid = '" . $mbid ."'";
if($mysqli->query($sql_query) === false) {
trigger_error('Wrong SQL: ' . $sql_query . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$total_artist_art++;
} else {
// mark album as art was not found
$sql_query = "UPDATE v2_artist SET
art = '2',last_art_check = '" . date("Y-m-d") . "'
WHERE artist_mbid = '" . $mbid ."'";
if($mysqli->query($sql_query) === false) {
trigger_error('Wrong SQL: ' . $sql_query . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$total_artist_art_not_found++;
}
}
if ($total_artist_art > 0 || $total_artist_art_not_found > 0) {
echo "Total Artist Art Found: $total_artist_art / Not Found: $total_artist_art_not_found <br/>";
}
}