Ticket #3796: amazon_album_art_scraper.2.pl

File amazon_album_art_scraper.2.pl, 2.9 KB (added by thierry@…, 17 years ago)

Updated to take hostname into account when fetching mythmusic storage 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}
27$sth->finish;
28my $rows = $sth1->execute;
29print "[+] $rows albums found\n";
30while (my ($id, $album, $artist) = $sth1->fetchrow_array) {
31        my ($imageurl, $filename, $dirname, $searchstring,$amazon_searchstring, $content, $resp, $jpeg) = undef;
32        $sth2->execute($id);
33        my $filename = $sth2->fetchrow_array;
34        my $dirname = dirname($filename);
35        print "[+] Fetching on $album by $artist...\t";
36        my $searchstring = "$artist+$album";
37        $searchstring =~ s/ /+/g;
38        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";
39        my $resp = $ua->get("$amazon_searchstring");
40        unless ($resp->is_success) {
41                print "[-] Error Searching Amazon For Album. Exiting...\n";
42                next;
43        }
44        $content = $resp->content;
45        if ($content =~ /did not match any/) {
46                print "Not Found. Next...\n";
47                next;
48        }
49        while ($content =~ /class\="resultCount\">Showing (\d+) Result/igm){
50                $results = $1;
51        }
52        print "$results Results\n";
53        if ($content =~ /<a href\=\"(.+?)\"><span class\=\"srTitle\">/ig) {
54                $albumurl = $1;
55        }
56        $resp = $ua->get("$albumurl") || sub{ print "[-] Cannot Fetch Album Info\n";next;};
57        $content = $resp->content;
58        while ($content =~ /registerImage\(\"original_image\",.\"(.+?)"/igm) {
59                $imageurl = $1;
60                $imageurl =~ s/AA240/SS500/;
61        }
62        if ($imageurl) {
63                print "[+] Cover Image Found at $imageurl\n";
64        } else {
65                print "[-] No Cover Image Found\n";
66                next;
67        }
68        $resp = $ua->get("$imageurl");
69        $jpeg = $resp->content;
70        $filename = "$directory$dirname/albumart.jpg";
71        print "[+] Saving to $filename\n";
72        open(FILE, ">",  "$filename") || die "[-] Cannot open file: $!\n";
73        print FILE $jpeg;
74        close(FILE);
75        sleep 10;
76
77       
78}
79
80$sth1->finish;
81$sth2->finish;
82$dbh->disconnect;