]> git.sesse.net Git - shamaz/blob - lookup.pl
Split XML parsing out in a separate program.
[shamaz] / lookup.pl
1 #! /usr/bin/perl
2 use LWP::UserAgent;
3 use HTTP::Request;
4 use IPC::Open2;
5 use XML::DOM;
6 use POSIX;
7
8 use strict;
9 use warnings;
10 no warnings qw(once);
11
12 my $ua = LWP::UserAgent->new;
13 push(@LWP::Protocol::http::EXTRA_SOCK_OPTS, SendTE => 0);
14 $ua->agent('Dalvik/1.2.0 (Linux; U; Android Froyo Build/MASTER)');
15
16 my $requestid = recognize($ua, $ARGV[0]);
17 lookup($ua, $requestid);
18
19 sub recognize {
20         my ($ua, $filename) = @_;
21         my $amr;
22         {
23                 local $/ = undef;
24                 $amr = `cat $filename`;
25         }
26
27         my @parts = (
28                 language => make_part('en_US'),
29                 service => make_part('cn=CH,cn=V3,cn=SmartClub,cn=ShazamiD,cn=services'),
30                 applicationIdentifier => make_encrypted_string('ShazamId_SmartPhone_Gamma__1.2'),
31                 deviceId => make_encrypted_string('1234567812345678'),
32                 deviceModel => make_encrypted_string('Nexus_One'),
33                 userAgent => make_encrypted_string('Mozilla/5.0_(Linux;_U;_Android_1.0;_en-us;_generic)_AppleWebKit/525.10+_(KHTML,_like_Gecko)_Version/3.0.4_Mobile_Safari/523.12.2'),
34                 tagDate => make_encrypted_string(POSIX::strftime("%Y-%m-%dT%H:%M:%S", localtime)),
35                 cryptToken => make_encrypted_string('A220B36F'),
36                 sample => make_encrypted_part($amr, 'janet.3gp.amr', 'application/octet-stream'),
37         );
38
39         my $res = $ua->post(
40                 'http://goog.shazamid.com/orbit/DoRecognition1',
41                 \@parts,
42                 'content-language' => 'en_US',
43                 'Content_Type' => 'multipart/form-data; boundary=A3r_ISAAC_eQeY2Bh');
44          
45         if (!$res->is_success) {
46                 die $res->status_line;
47         }
48
49         my $parser = XML::DOM::Parser->new;
50         my $doc = $parser->parse($res->content);
51         my $ids = $doc->getElementsByTagName("requestId");
52         if ($ids->getLength == 0) {
53                 print "Didn't recognize song\n";
54                 exit;
55         }
56         return $ids->item(0)->getAttribute('id');
57 }
58
59 sub lookup {
60         my ($ua, $requestid) = @_;
61
62         my @parts = (
63                 language => make_part('en_US'),
64                 service => make_part('cn=CH,cn=V3,cn=SmartClub,cn=ShazamiD,cn=services'),
65                 applicationIdentifier => make_encrypted_string('ShazamId_SmartPhone_Gamma__1.2'),
66                 deviceId => make_encrypted_string('1234567812345678'),
67                 deviceModel => make_encrypted_string('Nexus_One'),
68                 userAgent => make_encrypted_string('Mozilla/5.0_(Linux;_U;_Android_1.0;_en-us;_generic)_AppleWebKit/525.10+_(KHTML,_like_Gecko)_Version/3.0.4_Mobile_Safari/523.12.2'),
69                 requestId => make_encrypted_string($requestid),
70                 coverartSize => make_encrypted_string('304'),
71                 artistartX => make_encrypted_string('81'),
72                 artistartY => make_encrypted_string('0'),
73                 cryptToken => make_encrypted_string('A220B36F'),
74         );
75
76         my $res = $ua->post(
77                 'http://goog.shazamid.com/orbit/RequestResults1',
78                 \@parts,
79                 'content-language' => 'en_US',
80                 'Content_Type' => 'multipart/form-data; boundary=A3r_ISAAC_eQeY2Bh');
81          
82         if (!$res->is_success) {
83                 die $res->status_line;
84         }
85
86         print $res->content;
87 }
88
89 sub make_part {
90         my ($content, $filename, $mime_type) = @_;
91         if (defined($mime_type)) {
92                 return [ undef, $filename, 'Content' => $content, 'Content-Type' => $mime_type ];
93         } else {
94                 return [ undef, $filename, 'Content' => $content ];
95         }
96 }
97
98 sub make_encrypted_string {
99         my ($content, $filename, $mime_type) = @_;
100         if (length($content) % 8 == 0) {
101                 $content .= "\0\0\0\0\0\0\0\0";
102         }
103         my $enc = '#0x' . join("", map { sprintf "%02X", $_ } (unpack("W*", encrypt($content))));
104         return make_part($enc, $filename, $mime_type);
105 }
106
107 sub make_encrypted_part {
108         my ($content, $filename, $mime_type) = @_;
109         my $enc = encrypt($content);
110         return make_part($enc, $filename, $mime_type);
111 }
112         
113 sub encrypt {
114         my $content = shift;
115         my $blocks = (length($content) + 7) / 8;
116         my $ret = "";
117         local $/ = undef;
118
119         my $pid = IPC::Open2::open2(\*CHLD_OUT, \*CHLD_IN, './encrypt-ice');
120         for my $i (0..($blocks-1)) {
121                 my $block = substr($content, 8*$i, 8);
122                 print CHLD_IN $block;
123         }
124
125         close CHLD_IN;
126         $ret = <CHLD_OUT>;
127         close CHLD_OUT;
128         return $ret;
129 }