Ticket #3796: amazon_album_art_scraper.3.pl

File amazon_album_art_scraper.3.pl, 3.0 KB (added by thierry@…, 18 years ago)

good version - updated to take current hostname into account when fetching mythmusic location

Line 
1use File::Basename;
2use DBI;
3use LWP::UserAgent;
4use Sys::Hostname;
5
6print "Amazon AlbumArt Scraper\n";
7print "=======================\n";
8
9
10my $hostname = hostname;
11print "[+] Hostname Found: $hostname\n";
12my $dbh = DBI->connect('dbi:mysql:database=mythconverg;host=localhost;', 'root', '') || die "[-] Cannot connect to DB\n";
13$dbh->{'mysql_auto_reconnect'}=1;
14my $sth = $dbh->prepare("select data from settings where value = 'MusicLocation' and hostname = ?") || die "[-] Cannot prepare SQL statement 1\n";;
15my $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";
16my $sth2 = $dbh->prepare("select filename from music_songs where album_id = ? limit 1") || die "[-] Cannot prepare SQL statement 3\n";
17my $ua = LWP::UserAgent->new( agent => "" ) or die "[-] Cannot Create UserAgent\n";
18
19
20$sth->execute($hostname) || die "[-] Error Fetching MythMusic Directory\n";
21my $directory = $sth->fetchrow_array;
22if ($directory) {
23 print "[+] MythMusic Directory found at $directory\n";
24} else {
25 print "[-] MythMusic Directory not found\n";
26 exit;
27}
28$sth->finish;
29my $rows = $sth1->execute;
30print "[+] $rows albums found\n";
31while (my ($id, $album, $artist) = $sth1->fetchrow_array) {
32 my ($imageurl, $filename, $dirname, $searchstring,$amazon_searchstring, $content, $resp, $jpeg) = undef;
33 $sth2->execute($id);
34 my $filename = $sth2->fetchrow_array;
35 my $dirname = dirname($filename);
36 print "[+] Fetching on $album by $artist...\t";
37 my $searchstring = "$artist+$album";
38 $searchstring =~ s/ /+/g;
39 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";
40 my $resp = $ua->get("$amazon_searchstring");
41 unless ($resp->is_success) {
42 print "[-] Error Searching Amazon For Album. Exiting...\n";
43 next;
44 }
45 $content = $resp->content;
46 if ($content =~ /did not match any/) {
47 print "Not Found. Next...\n";
48 next;
49 }
50 while ($content =~ /class\="resultCount\">Showing (\d+) Result/igm){
51 $results = $1;
52 }
53 print "$results Results\n";
54 if ($content =~ /<a href\=\"(.+?)\"><span class\=\"srTitle\">/ig) {
55 $albumurl = $1;
56 }
57 $resp = $ua->get("$albumurl") || sub{ print "[-] Cannot Fetch Album Info\n";next;};
58 $content = $resp->content;
59 while ($content =~ /registerImage\(\"original_image\",.\"(.+?)"/igm) {
60 $imageurl = $1;
61 $imageurl =~ s/AA240/SS500/;
62 }
63 if ($imageurl) {
64 print "[+] Cover Image Found at $imageurl\n";
65 } else {
66 print "[-] No Cover Image Found\n";
67 next;
68 }
69 $resp = $ua->get("$imageurl");
70 $jpeg = $resp->content;
71 $filename = "$directory$dirname/albumart.jpg";
72 print "[+] Saving to $filename\n";
73 open(FILE, ">", "$filename") || die "[-] Cannot open file: $!\n";
74 print FILE $jpeg;
75 close(FILE);
76 sleep 10;
77
78
79}
80
81$sth1->finish;
82$sth2->finish;
83$dbh->disconnect;