Ticket #3796: amazon_album_art_scraper.pl

File amazon_album_art_scraper.pl, 3.3 KB (added by thierry@…, 18 years ago)
Line 
1# Amazon Album Art Scraper by thierry@icebo.org
2# It would be nice if you keep the credits intact...
3
4# Usage:
5# Change the Username and Password a couple of lines below
6# Run the script command line or in a cron job on your mythtv machine
7
8#!/usr/bin/perl
9use File::Basename;
10use DBI;
11use LWP::UserAgent;
12
13#--------- CHANGE THIS ---------------
14my $username = 'USER';
15my $password = 'PASSWORD';
16
17#--------- STOP CHANGING HERE --------
18
19print "Amazon AlbumArt Scraper by <thierry\@icebo.org>\n";
20print "===============================================\n";
21
22my $dbh = DBI->connect('dbi:mysql:database=mythconverg;host=localhost;', $username, $password) || die "[-] Cannot connect to DB: $DBI::errstr\n";
23$dbh->{'mysql_auto_reconnect'}=1;
24my $sth = $dbh->prepare("select data from settings where value = 'MusicLocation'") || die "[-] Cannot prepare SQL statement 1\n";;
25my $sth1 = $dbh->prepare("select distinct music_songs.album_id,album_name, artist_name from music_songs, music_albums, music_artists where music_songs.artist_id = music_artists.artist_id and music_songs.album_id = music_albums.album_id order by rand()") || die "[-] Cannot prepare SQL statement 2\n";
26my $sth2 = $dbh->prepare("select filename from music_songs where album_id = ? limit 1") || die "[-] Cannot prepare SQL statement 3\n";
27my $ua = LWP::UserAgent->new( agent => "" ) or die "[-] Cannot Create UserAgent\n";
28
29
30$sth->execute || die "[-] Error Fetching MythMusic Directory\n";
31my $directory = $sth->fetchrow_array;
32if ($directory) {
33 print "[+] MythMusic Directory found at $directory\n";
34} else {
35 print "[-] MythMusic Directory not found\n";
36}
37$sth->finish;
38my $rows = $sth1->execute;
39print "[+] $rows albums found\n";
40while (my ($id, $album, $artist) = $sth1->fetchrow_array) {
41 my ($imageurl, $filename, $dirname, $searchstring,$amazon_searchstring, $content, $resp, $jpeg) = undef;
42 $sth2->execute($id);
43 my $filename = $sth2->fetchrow_array;
44 my $dirname = dirname($filename);
45 print "[+] Fetching $album by $artist...\t";
46 my $searchstring = "$artist+$album";
47 $searchstring =~ s/ /+/g;
48 my $amazon_searchstring = "http://www.amazon.com/s/?initialSearch=1&url=search-alias%3Dpopular&field-keywords=$searchstring&Go.x=0&Go.y=0&Go=Go";
49 my $resp = $ua->get("$amazon_searchstring");
50 unless ($resp->is_success) {
51 print "[-] Error Searching Amazon For Album. Exiting...\n";
52 next;
53 }
54 $content = $resp->content;
55 if ($content =~ /did not match any/) {
56 print "Not Found. Next...\n";
57 next;
58 }
59 while ($content =~ /class\="resultCount\">Showing (\d+) Result/igm){
60 $results = $1;
61 }
62 print "$results Results\n";
63 if ($content =~ /<a href\=\"(.+?)\"><span class\=\"srTitle\">/ig) {
64 $albumurl = $1;
65 }
66 $resp = $ua->get("$albumurl") || sub{ print "[-] Cannot Fetch Album Info\n";next;};
67 $content = $resp->content;
68 while ($content =~ /registerImage\(\"original_image\",.\"(.+?)"/igm) {
69 $imageurl = $1;
70 $imageurl =~ s/AA240/SS500/;
71 }
72 if ($imageurl) {
73 print "[+] Cover Image Found at $imageurl\n";
74 } else {
75 print "[-] No Cover Image Found\n";
76 next;
77 }
78 $resp = $ua->get("$imageurl");
79 $jpeg = $resp->content;
80 $filename = "$directory$dirname/albumart.jpg";
81 print "[+] Saving to $filename\n";
82 open(FILE, ">", "$filename") || die "[-] Cannot open file: $!\n";
83 print FILE $jpeg;
84 close(FILE);
85 sleep 10;
86
87
88}
89
90$sth1->finish;
91$sth2->finish;
92$dbh->disconnect;