PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/server/getalbumart.php

https://github.com/gandazgul/Mandolin
PHP | 325 lines | 256 code | 22 blank | 47 comment | 42 complexity | 26dfa52ca7e98c824dc8f078d598f1cd MD5 | raw file
  1. #!/usr/bin/php
  2. <?
  3. /* ----------------------------------------------------------------------------
  4. App Title: Get Album Art
  5. App Version: 1.0
  6. Author: Jared Breland <jbreland@legroom.net>
  7. Homepage: http://www.legroom.net/mysoft
  8. Script Function:
  9. Download cover image for a specific album
  10. Instructions:
  11. Configure the variables under "Setup Environment" for your environment
  12. Requirements:
  13. The following programs must be installed and available
  14. ImageMagick (http://www.imagemagick.org/)
  15. used to create freedesktop.org icon
  16. Please visit the app's homepage for additional information.
  17. ---------------------------------------------------------------------------- */
  18. /*
  19. TODO:
  20. */
  21. # Setup environment
  22. $convert = "/usr/bin/convert";
  23. $minx = 250;
  24. $miny = 250;
  25. $name = "00-Cover Front";
  26. $desktop = false;
  27. $path = getcwd();
  28. $albums = array();
  29. $images = 0;
  30. $dvdalbum = 0;
  31. $sorder = array("buy", "walmart", "amazon");
  32. $prog = basename($argv[0]);
  33. # Check for arguments
  34. for ($i = 1; $i <= $argc; $i++) {
  35. switch (strtolower($argv[$i])) {
  36. case "-h":
  37. case "--help":
  38. case "-?":
  39. warning();
  40. break;
  41. case "-c":
  42. $artist = basename(dirname(getcwd()));
  43. $album = basename(getcwd());
  44. break;
  45. case "-r":
  46. $artist = $argv[$i+1];
  47. break;
  48. case "-l":
  49. $album = $argv[$i+1];
  50. break;
  51. case "-p":
  52. $path = realpath($argv[$i+1]);
  53. break;
  54. case "-n":
  55. $name = $argv[$i+1];
  56. break;
  57. case "-d":
  58. $desktop = true;
  59. break;
  60. case "-q":
  61. $quiet = true;
  62. break;
  63. }
  64. }
  65. # Make sure necessary album information exists
  66. if ($artist == "" or $album == "") {
  67. warning();
  68. }
  69. # Check that required programs exist
  70. if ($desktop && !file_exists($convert)) {
  71. echo "Error: $convert does not exist\n";
  72. exit;
  73. }
  74. # Check for existing image
  75. if (file_exists("$path/$name.jpg")) {
  76. if (!$quiet) echo "$path/$name.jpg already exists. Skipping image download.\n";
  77. if ($desktop) desktop_icon();
  78. exit;
  79. }
  80. # Check if the album is a DVD
  81. if (strtoupper(substr($album, -6)) == " (DVD)")
  82. $dvdalbum = substr($album, 0, strlen($album)-6);
  83. # Set buy.com search options
  84. if ($dvdalbum)
  85. $buysearch = "http://www.buy.com/retail/searchresults.asp?search_store=4&qu=".urlencode("$artist, $dvdalbum");
  86. else
  87. $buysearch = "http://www.buy.com/retail/searchresults.asp?search_store=6&qu=".urlencode("$artist, $album");
  88. $buyimg = "http://ak.buy.com/db_assets/large_images/";
  89. # Set walmart.com search options
  90. if ($dvdalbum)
  91. $walsearch = "";
  92. else
  93. $walsearch = "http://www.walmart.com/catalog/search-ng.gsp?search_constraint=4104&search_query=".urlencode("$artist, $album");
  94. # Set amazon.com search options
  95. if ($dvdalbum)
  96. $azsearch = "http://www.amazon.com/exec/obidos/external-search/?index=dvd&field-title=".urlencode("$artist - $dvdalbum");
  97. else
  98. $azsearch = "http://www.amazon.com/exec/obidos/external-search/?index=music&field-artist=".urlencode($artist)."&field-title=".urlencode($album);
  99. $azimg1 = "http://images.amazon.com/images/P/";
  100. $azimg2 = ".01._SCLZZZZZZZ_.jpg";
  101. # Search each store as necessary
  102. foreach ($sorder as $item) {
  103. # Search store for album
  104. $albums = album_search($item);
  105. # If at least one found, download and validate
  106. if ($albums) $images = album_download($albums);
  107. # If download successful, break. Otherwise, process next store.
  108. if ($images) break;
  109. }
  110. # Check for success
  111. if ($images == 0) {
  112. if (!$quiet) echo "\nNo (new) suitable cover images could be found.\n";
  113. # Show notice for multiple images
  114. } else {
  115. # Create (or update) .directory files
  116. if ($desktop) desktop_icon();
  117. # Show warning if multiple images found
  118. if ($images > 1) {
  119. if (!$quiet) {
  120. echo "\nWarning: Multiple images were found.\n";
  121. echo "It is recommended that you review these images and select the correct one.\n";
  122. }
  123. }
  124. }
  125. exit;
  126. # Print usage message and exit is program called improperly
  127. function warning() {
  128. global $prog;
  129. echo "Usage: $prog {-c | -r \"Artist Name\" -l \"Album Name\"} \\\n";
  130. echo " ".str_repeat(" ", strlen($prog))." [-h] [-p path] [-n imagename] [-d] [-q]\n";
  131. echo "Download album art for given artist and album\n";
  132. echo "\nMandatory Arguments: (either -c or -r and -l must be specified)\n";
  133. echo " -c Use directory structure for album information:\n";
  134. echo " Album's name = current dir\n";
  135. echo " Artist's name = parent dir\n";
  136. echo " -r artist Specify Artist's name\n";
  137. echo " -l album Specify Album's name\n";
  138. echo "\nOptions:\n";
  139. echo " -h Display this help information\n";
  140. echo " -p path Downloaded location; Default is current directory\n";
  141. echo " -n imagename Image base name; Default is \"00-Cover Front.jpg\"\n";
  142. echo " -d Enable freedesktop.org .directory icon support\n";
  143. echo " -q Quiet mode\n";
  144. echo "\nArguments with spaces must be enclosed by quotes.\n";
  145. exit;
  146. }
  147. # Function to search the given store
  148. function album_search($store) {
  149. global $artist, $album, $path, $name, $buysearch, $buyimg, $walsearch, $azsearch, $azimg1, $azimg2, $quiet;
  150. $covers = array();
  151. if ($store == "buy") $search = $buysearch;
  152. elseif ($store == "walmart") $search = $walsearch;
  153. elseif ($store == "amazon") $search = $azsearch;
  154. # Skip if search string is invalid
  155. if ($search == "")
  156. return 0;
  157. # Search web site
  158. if (!$quiet) {
  159. echo "\nFetching cover\n";
  160. echo "For: $artist - $album\n";
  161. echo "To: $path/$name\n";
  162. echo "From: $search ... ";
  163. }
  164. $results = file($search);
  165. if (!$quiet) echo "complete.\n";
  166. # Scan through results for album image link(s)
  167. $multiple = false;
  168. foreach ($results as $line) {
  169. # Results contain multiple hits - break and process separately
  170. if ($store == "buy") $multstring = ": Music Search Results";
  171. elseif ($store == "walmart") $multstring = "Search results for";
  172. elseif ($store == "amazon") $multstring = "Amazon.com: Music Search";
  173. if (stristr($line, $multstring)) {
  174. $multiple = true;
  175. break;
  176. }
  177. # Results contain single hit
  178. if ($store == "buy") {
  179. if (stristr($line, "javascript:largeIM")) {
  180. $line = substr($line, strpos($line, "javascript:largeIM") + 20);
  181. $covers[0] = substr($line, 0, strpos($line, "'"));
  182. return $covers;
  183. }
  184. } elseif ($store == "walmart") {
  185. if (stristr($line, "javascript:photo_opener")) {
  186. $line = substr($line, strpos($line, "javascript:photo_opener") + 25);
  187. $covers[0] = substr($line, 0, strpos($line, "&"));
  188. return $covers;
  189. }
  190. } elseif ($store == "amazon") {
  191. if (stristr($line, "SCMZZZZZZZ")) {
  192. $line = substr($line, strpos($line, "img src=") + 9);
  193. $thumb = substr($line, 0, strpos($line, '"'));
  194. $prod = basename($thumb);
  195. list($prod) = explode('.', urldecode($prod));
  196. $covers[0] = $azimg1.$prod.$azimg2;
  197. return $covers;
  198. }
  199. }
  200. }
  201. # Process results for multiple hits
  202. if ($multiple) {
  203. $i = 0;
  204. $process = false;
  205. if ($store == "buy") {
  206. $head = "Sorted by:";
  207. $foot = "matching products.";
  208. } elseif ($store == "walmart") {
  209. # Cannot determine product image URL from this results page
  210. return 0;
  211. } elseif ($store == "amazon") {
  212. $head = "Sort by:";
  213. $foot = "/associates/";
  214. }
  215. foreach ($results as $line) {
  216. # Skip buy.com lookup if not found
  217. if ($store == "buy")
  218. if (stristr($line, "We could not find an exact match"))
  219. return 0;
  220. # Drop header/left
  221. if (stristr($line, $head)) $process = true;
  222. if ($process) {
  223. if ($store == "buy") {
  224. if (stristr($line, "http://ak.buy.com/db_assets/ad_images/")) {
  225. $line = substr($line, strpos($line, "http://ak.buy.com/db_assets/ad_images/") + 38);
  226. $thumb = substr($line, 0, strpos($line, '"'));
  227. list($thumb) = explode('.', urldecode($thumb));
  228. $covers[$i++] = $buyimg.$thumb.".jpg";
  229. }
  230. } elseif ($store = "amazon") {
  231. if (stristr($line, "THUMB")) {
  232. $line = substr($line, strpos($line, "img src=") + 9);
  233. $thumb = substr($line, 0, strpos($line, '"'));
  234. $prod = basename($thumb);
  235. list($prod) = explode('.', urldecode($prod));
  236. $covers[$i++] = $azimg1.$prod.$azimg2;
  237. }
  238. }
  239. # Drop right/footer
  240. if (stristr($line, $foot)) break;
  241. }
  242. }
  243. return $covers;
  244. }
  245. return 0;
  246. }
  247. # Function to download found covers
  248. function album_download($covers) {
  249. global $path, $name, $minx, $miny, $quiet;
  250. $saved = 0;
  251. foreach ($covers as $cover) {
  252. # Set first available output filename
  253. $file = "$path/$name";
  254. $i = 1;
  255. if (file_exists("$file.jpg")) {
  256. while (file_exists($file.++$i.".jpg")) {}
  257. $file .= $i;
  258. }
  259. # Download image
  260. $file .= ".jpg";
  261. copy($cover, $file);
  262. # Verify image dimensions
  263. $size = getimagesize($file);
  264. if ($size[0] < $minx || $size[1] < $miny) {
  265. unlink($file);
  266. continue;
  267. }
  268. # Check for duplicate image
  269. for ($j = $i-1; $j >= 1; $j--) {
  270. if ($j == 1) $x = "";
  271. else $x = $j;
  272. if (filesize($file) == filesize("$path/$name$x.jpg")) {
  273. unlink($file);
  274. continue 2;
  275. }
  276. }
  277. # Report and count successes
  278. if (!$quiet) echo "\nSaving $file\n";
  279. $saved++;
  280. }
  281. return $saved;
  282. }
  283. # Function to create freedesktop.org icon
  284. function desktop_icon() {
  285. global $convert, $path, $name, $quiet;
  286. if (file_exists("$path/.directory.png")) return false;
  287. passthru("$convert -scale 32x32 \"$path/$name.jpg\" \"$path/.directory.png\"");
  288. $outfile = fopen("$path/.directory", 'w');
  289. fwrite($outfile, "[Desktop Entry]\nIcon=./.directory.png\n");
  290. fclose($outfile);
  291. if (!$quiet) echo "\nfreedesktop.org directory icon created from $path/$name.jpg\n";
  292. }
  293. ?>