jmftrindade / cs511-imusic

A music search engine prototype for CS 511 (Advanced Database Systems) at UIUC.

commit 23: a0c27e9170d8
parent 22: 046ab1e70691
branch: default
search from last.fm for tracklist
huongluu
5 months ago

Changed (Δ2.6 KB):

raw changeset »

album_tracklist.php (74 lines added, 0 lines removed)

Up to file-list album_tracklist.php:

1
<?php
2
3
function tracklist_lastfm_search ($album) {
4
    $query = urlencode($album);
5
 
6
    // Huong: Get last.fm album search results, and create simplexml from it
7
    $lastfmhost = 'http://ws.audioscrobbler.com/2.0/';
8
    $apikey = '33b95aaa4235e255b823aead4871c151';
9
    $method = 'album.search';
10
    $url = $lastfmhost.'?method='.$method.'&album='.$query.'&api_key='.$apikey;
11
   
12
     $ch = curl_init($url);
13
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
14
    curl_setopt($ch, CURLOPT_HEADER, 0);
15
    $data = curl_exec($ch);
16
  //  echo $data;
17
    curl_close($ch);
18
    $lastfm_results = new SimpleXmlElement($data, LIBXML_NOCDATA);
19
20
    $album_url = "";
21
    $tracklist_url = "";
22
    foreach ($lastfm_results->results->albummatches->album as $theresult) {
23
        // Huong: select the url for exactly match
24
       if (strcasecmp($theresult->name, $album) == 0)
25
            {
26
               // get info to search next
27
               $album_name = urlencode($theresult->name);
28
               $artist_name = urlencode($theresult->artist);
29
               $method = 'album.getInfo';
30
               $album_url = $lastfmhost.'?method='.$method.'&artist='.$artist_name.'&album='.$album_name.'&api_key='.$apikey;
31
               $tracklist_url = $theresult->url;
32
             //  echo $tracklist_url. "HAHAHHA <br>";
33
               break;  // take the first matched album
34
            }
35
    }
36
37
    // Huong: if found 1
38
    if (strcmp($tracklist_url,"") != 0){
39
40
        $yql_query = "select * from html where url=\"";
41
        $yql_query .= $tracklist_url;
42
        $yql_query .= "\" and xpath=\"/html/body/div[2]/div/div[4]/div[2]/div/table/tbody\"";
43
        $yql_query = rawurlencode($yql_query);
44
        $yql_query = "http://query.yahooapis.com/v1/public/yql?q=". $yql_query;
45
        $yql_query .= "&format=xml";
46
        // echo $yql_query.'<br>';
47
48
        $ch2 = curl_init($yql_query);
49
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
50
        curl_setopt($ch2, CURLOPT_HEADER, 0);
51
        $data2 = curl_exec($ch2);
52
        curl_close($ch2);
53
        $tracklist_results = new SimpleXmlElement($data2, LIBXML_NOCDATA);
54
        //echo $tracklist_results->asXML();
55
56
57
        $tracklist = array();
58
        foreach ($tracklist_results->results->tbody->tr as $rows) {
59
            $pos = $rows->td[0]->p;
60
            if ($rows->td[2]->p->a != "")
61
                { $name = $rows->td[2]->p->a; }
62
            else
63
                { $name = $rows->td[2]->a; }
64
65
            if ($pos != "")
66
                $tracklist[(int)$pos]= $name;
67
         }
68
        // Huong: display result
69
        for ($i =1 ; $i <= sizeof($tracklist); $i++) {
70
                echo $i.": ".$tracklist[$i].'<br>';
71
          }
72
        }
73
}
74
?>