jmftrindade / cs511-imusic

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

commit 29: 192f7385c5ec
parent 28: 9051f7c6aacc
branch: default
fix album_tracklist.php and track_lyricswiki.php
huongluu
5 months ago

Changed (Δ1.1 KB):

raw changeset »

album_gracenote_tracklist.php (1 lines added, 1 lines removed)

album_tracklist.php (37 lines added, 63 lines removed)

track_lyrics.php (16 lines added, 15 lines removed)

track_lyricswiki.php (9 lines added, 3 lines removed)

Up to file-list album_gracenote_tracklist.php:

@@ -102,7 +102,7 @@ if (isset($_GET['album']) && isset($_GET
102
102
103
103
           } else {
104
104
             	// if cannot found on gracenote, try to search from lastfm
105
             	tracklist_lastfm_search ($album);
105
             	tracklist_lastfm_search ($album,$artist);
106
106
           }
107
107
108
108
	} // if there's no mbid, we search on gracenote -> end

Up to file-list album_tracklist.php:

1
1
<?php
2
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);
3
function tracklist_lastfm_search ($album,$artist) {
4
    $album = urlencode($album);
5
    $artist = urlencode($artist);
6
    $tracklist_url = "http://www.last.fm/music/";
7
    $tracklist_url .= $artist.'/'.$album;
8
    //echo $tracklist_url;
9
    $raw = file_get_contents($tracklist_url);
10
    $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
11
    $content = str_replace($newlines, "", html_entity_decode($raw));
19
12
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
            }
13
  // echo $raw;
14
    $start_pos = strpos($content,"<tbody>");
15
    $end_pos = strpos($content,"</tbody");
16
    $result = substr($content, $start_pos, $end_pos - $start_pos);
17
    $result = str_replace($newlines, "",$result);
18
19
    $pos_string = "<td class=\"positionCell\">";
20
    $name_string = "<td class=\"subjectCell\">";
21
    $start_pos = 0;
22
    $tracklist = array();
23
    for ($i =1; $i <= 20; $i++){
24
        $tracklist[$i] = "";
35
25
    }
36
26
37
    // Huong: if found 1
38
    if (strcmp($tracklist_url,"") != 0){
27
    while (strpos($result, $pos_string, $start_pos) != FALSE) {
28
        $start_pos = strpos($result, $pos_string, $start_pos);
29
        $end_pos = strpos($result, "</td>", $start_pos);
30
        $pos = substr($result, $start_pos + strlen($pos_string),$end_pos - $start_pos - strlen($pos_string));
39
31
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>';
32
        $start_pos = strpos($result, $name_string, $start_pos);
33
        $start_pos = strpos($result,"album=\"\">",$start_pos);
34
        $end_pos = strpos($result, "</a>", $start_pos);
35
        $name = substr($result, $start_pos + 9,$end_pos - $start_pos - 9);
47
36
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
37
38
        $tracklist[(int)$pos] = $name;
39
        $start_pos++;
40
    }
41
        // Huong: display result
42
        for ($i= 1 ; $i <= sizeof($tracklist); $i++) {
43
            if ($tracklist[$i] != "")
44
                echo $i.': <a href="index.php?artist='.$artist.'&track='.$tracklist[$i].'"  title="Search on iMusic for '.$artist.' - '.$tracklist[$i].'"">'.$artist.' - '.$tracklist[$i].'</a><br/>';
45
        }
56
46
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
47
}
74
48
?>

Up to file-list track_lyrics.php:

@@ -9,14 +9,14 @@ function GetTag($p,$t,$l) {
9
9
	if($l==0) {
10
10
		$l=1;
11
11
	}
12
	
12
13
13
	$sp=0;
14
14
	while($l>0) {
15
15
		$sp=strpos($p, "<".$t.">", $sp);
16
16
		$sp=$sp+strlen($t)+2;
17
17
		$l--;
18
18
	}
19
	
19
20
20
	if($sp-strlen($t)-2==0) {
21
21
		$GetTag=FALSE;
22
22
	} else {
@@ -24,7 +24,7 @@ function GetTag($p,$t,$l) {
24
24
		$ep=$ep-$sp;
25
25
		$GetTag=substr($p,$sp,$ep);
26
26
	}
27
	
27
28
28
	return $GetTag;
29
29
}
30
30
@@ -36,45 +36,45 @@ function filterChar($str) {
36
36
	return $str;
37
37
}
38
38
39
39
function track_lyricsfly($track, $artist){
40
40
// Huong -> s ir our search query variable
41
if(isset($_GET['track']) && isset($_GET['artist']) ) {
41
 //if(isset($_GET['track']) && isset($_GET['artist']) ) {
42
42
	$title = $_GET['track'];
43
43
	$artist = $_GET['artist'];
44
44
45
45
	//******** setings ***************************************************************
46
	$user_ID="c67d3bc5dfd3a5e7a-temporary.API.access";	//Set your lyricsfly.com User ID key
46
	$user_ID="80605b0b9a84c5e2c-temporary.API.access";	//Set your lyricsfly.com User ID key
47
47
	//$artist="Britney Spears";							//Set the artist you want to find lyrics for
48
48
	//$title="Baby One More Time";						//Set the title for the song you want to find lyrics to
49
49
	$song_number=1;										//Set to 1 if you want the first result or 2 if you want 2nd result and so on...
50
50
	//******** setings ***************************************************************
51
51
52
52
	$artist=urlencode(filterChar($artist));		//filter and encode for url string
53
	$title=urlencode(filterChar($title));            
53
	$title=urlencode(filterChar($title));
54
54
	$url="http://lyricsfly.com/api/api.php?i=".$user_ID."&a=".$artist."&t=".$title;   //url construction string with pluged in from above values
55
55
	$html=file_get_contents($url);				//retrieve the entire xml page into string
56
56
57
57
	$found_check= GetTag($html,"sg",$song_number);
58
	
58
59
59
	//check if song was found
60
60
	if($found_check=="Not found") {
61
61
		echo "Sorry, lyrics for this title are not available in lyricsfly database.<br>";	//... if not print a message
62
62
		echo "Please <a href='http://lyricsfly.com/submit/'>submit</a>";					//and print link to submit form
63
63
	}
64
	
64
65
65
	//check if site ID is good
66
66
	elseif($found_check=="Wrong site ID!") {
67
67
		echo "Sorry, access to lyricsfly API has been terminated please contact the administrator";
68
68
	}
69
	
69
70
70
	//check if user ID access key is missing and if the number of song titles is not passed the amount which was returned
71
71
	elseif($found_check=="Missing site ID!" or $found_check==FALSE)	{
72
72
		echo "Sorry, the API code was not properly implemented";
73
73
	}
74
	
74
75
75
	//finally, if everything check on we retrieve and print the data returned by lyricsfly database
76
76
	else {
77
		$found_checksum=GetTag($html,"cs",$song_number);	//get song checksum
77
             $found_checksum=GetTag($html,"cs",$song_number);	//get song checksum
78
78
		$found_ID=GetTag($html,"id",$song_number);			//get song ID
79
79
		$found_title=GetTag($html,"tt",$song_number);		//get song title
80
80
		$found_artist=GetTag($html,"ar",$song_number);		//get song artist
@@ -87,10 +87,11 @@ if(isset($_GET['track']) && isset($_GET[
87
87
		echo "<b>Artist: $found_artist</b><br>";	//print artist name
88
88
		echo "<b>Album: $found_album</b><br><br>";	//print album name
89
89
		echo "$found_lyrics";						//print lyrics
90
		
90
91
91
		//create a link to correction form for users to be able to correct lyrics
92
		echo "<a href='http://lyricsfly.com/search/correction.php?".$found_checksum."&id=".$found_ID."'>Fix above lyrics here</a><br><br>";	
93
	}
92
		echo "<a href='http://lyricsfly.com/search/correction.php?".$found_checksum."&id=".$found_ID."'>To see full lyric</a><br><br>";
93
94
          }
94
95
95
96
}
96
97
?>

Up to file-list track_lyricswiki.php:

1
1
<?php
2
2
include "./track_lyrics.php";
3
3
if(isset($_GET['artist']) && isset($_GET['track'])) {
4
4
	// bug fix for when artist or track contains single quotes
5
5
	$artist = $_GET['artist'];
@@ -22,9 +22,15 @@ if(isset($_GET['artist']) && isset($_GET
22
22
	$dom = @DOMDocument::loadHTML($html);
23
23
	
24
24
	echo "<h1>Lyrics</h1>";
25
	echo '<h2>'.$artist.' - '.$track.'</h2><br/>';
26
25
27
26
	// print only lyrics node (it's the first "<p>")
28
	echo $dom->saveXML($dom->documentElement->getElementsByTagName('p')->item(0));
27
        $lyric = $dom->saveXML($dom->documentElement->getElementsByTagName('p')->item(0));
28
        if (strpos($lyric,"http-status-message=\"Not Found\"") != FALSE) {
29
            track_lyricsfly($track, $artist);
30
        } else{
31
            echo '<h2>'.$artist.' - '.$track.'</h2><br/>';
32
            echo $lyric;
33
        }
34
	
29
35
}
30
36
?>