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_album_art.php
114 lines (95 loc) · 3.43 KB
/
scan_for_album_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
<?php
function scan_for_album_art() {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT v2_release_group.release_mbid, v2_artist.name as artist, v2_release_group.title as title FROM `v2_release_group` LEFT JOIN `v2_artist` ON v2_release_group.artist_id = v2_artist.artist_id WHERE ((v2_release_group.last_art_check < NOW() - INTERVAL 30 DAY) OR (v2_release_group.last_art_check IS NULL)) AND (v2_release_group.art = '0' OR v2_release_group.art = '2') ORDER BY v2_release_group.last_art_check ASC, v2_release_group.date DESC LIMIT 80";
$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_art = 0;
$total_art_not_found = 0;
while($row = $result->fetch_assoc()){
sleep(1);
chdir(dirname(__FILE__));
$artist = rawurlencode($row['artist']);
$album = rawurlencode($row['title']);
$mbid = $row['release_mbid'];
$saved_thumb = 0;
$saved_full = 0;
$saved_large = 0;
$xml = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&artist={$artist}&album={$album}&api_key=";
$xml = file_get_contents($xml);
if(!$xml) {
goto a;
}
$xml = new SimpleXMLElement($xml);
$xml = $xml->album;
$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 = "covers/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 = "covers/".$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 = "covers/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_release_group SET
art = '$filename',last_art_check = '" . date("Y-m-d") . "'
WHERE release_mbid = '" . $mbid ."'";
if($mysqli->query($sql_query) === false) {
trigger_error('Wrong SQL: ' . $sql_query . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$total_art++;
} else {
// mark album as art was not found
$sql_query = "UPDATE v2_release_group SET
art = '2',last_art_check = '" . date("Y-m-d") . "'
WHERE release_mbid = '" . $mbid ."'";
if($mysqli->query($sql_query) === false) {
trigger_error('Wrong SQL: ' . $sql_query . ' Error: ' . $mysqli->error, E_USER_ERROR);
}
$total_art_not_found++;
}
}
if ($total_art > 0 || $total_art_not_found > 0 ) {
echo "Total Album Art Found: $total_art / Not Found: $total_art_not_found <br/>";
}
}