jmftrindade / cs511-imusic

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

commit 22: 046ab1e70691
parent 21: 547c0ea2b5ae
branch: default
replace old code for gracenote search and add call to last.fm search
huongluu
5 months ago

Changed (Δ1.1 KB):

raw changeset »

album_gracenote_tracklist.php (95 lines added, 98 lines removed)

Up to file-list album_gracenote_tracklist.php:

1
1
<?php
2
//include "./query_process.php";
2
include "./album_tracklist.php";
3
3
4
4
if (isset($_GET['album']) && isset($_GET['artist'])) {
5
5
	$album = $_GET['album'];
@@ -7,104 +7,101 @@ if (isset($_GET['album']) && isset($_GET
7
7
	
8
8
	// if there's no mbid for the album, we search on gracenote
9
9
	if (!isset($_GET['album_mbid'])) {
10
		$query = urlencode($album);
11
	
12
		// scrape
13
		$url = "http://www.gracenote.com/search/?query=".$query."&search_type=album";
14
		$raw = file_get_contents($url);
15
		$start_pos = strpos($raw, "<div id=\"main-container\">");
16
		$end_pos = strpos($raw, "<div class=\"column-right\">" );
17
		$container = substr($raw, $start_pos, $end_pos - $start_pos);
18
		$start_pos = 0;
19
		$album_details = "";
20
		$artist_details = "";
10
	     $query = urlencode($album);
11
	     $url = "http://www.gracenote.com/search/?query=".$query."&search_type=album";
12
             $raw = file_get_contents($url);
13
             $start_pos = strpos($raw, "<div id=\"main-container\">");
14
             $end_pos = strpos($raw, "<div class=\"column-right\">" );
15
             $container = substr($raw, $start_pos, $end_pos - $start_pos);
16
             $start_pos = 0;
17
             $album_details = "";
18
             $artist_details = "";
19
             while (strpos($container,"<div class=\"item\"", $start_pos ) != FALSE) {
20
                 $start_pos = strpos($container,"<div class=\"item\"", $start_pos);
21
                 $end_pos = strpos($container, "<div class=\"item\"", $start_pos + 1);
22
                 if ($end_pos == FALSE)
23
                    $end_pos = strlen($container);
24
                 // Huong: $str contains each search result
25
                 $str = substr($container, $start_pos, $end_pos - $start_pos);
26
                 $start_pos = $end_pos;
21
27
22
		while (strpos($container,"<div class=\"item\"", $start_pos ) != FALSE) {
23
			$start_pos = strpos($container,"<div class=\"item\"", $start_pos);
24
			$end_pos = strpos($container, "<div class=\"item\"", $start_pos + 1);
25
	
26
			if ($end_pos == FALSE) {
27
				$end_pos = strlen($container);
28
			}
29
	
30
			// Huong: $str contains each search result
31
			$str = substr($container, $start_pos, $end_pos - $start_pos);
32
			$start_pos = $end_pos;
33
	
34
			// Huong: process each result
35
			// Album info
36
			$pos = strpos($str, "<strong>Album:</strong>");
37
			$pos = strpos($str, "<a href=\"",$pos);
38
			$end = strpos($str, "\">", $pos);
39
			$album_details = substr($str, $pos + 9, $end - $pos - 9);     
40
			$pos = strpos($str, "</a>", $end);
41
			$album_name = substr($str, $end + 2, $pos - $end - 2);
42
	
43
			// Artist info
44
			$pos = strpos($str, "<strong>Artist:</strong>");
45
			$pos = strpos($str, "<a href=\"",$pos);
46
			$end = strpos($str, "\">", $pos);
47
			$artist_details = substr($str, $pos + 9, $end - $pos - 9);
48
			$pos = strpos($str, "</a>", $end);
49
			$artist_name = substr($str, $end + 3, $pos - $end - 3);
50
	
51
			// check for MATCH          
52
			if ((strcasecmp($album,$album_name) == 0) && (strcasecmp($artist, $artist_name) == 0) ) {
53
				break;       // take the first matched
54
			}
55
		}
56
	
57
		// Huong: found 1
58
		if ($album_details != "") {
59
			$url = "http://www.gracenote.com". $album_details;
60
	
61
			// scrape album page
62
			$raw = file_get_contents($url);
63
			$start_pos = strpos($raw, "<div id=\"main-container\">");
64
			$end_pos = strpos($raw, "<div class=\"column-right\">" );
65
			$container = substr($raw, $start_pos, $end_pos - $start_pos);
66
	
67
			// YEAR OF RELEASE
68
			$pos = strpos($container,"<div class=\"year\">");
69
			$pos = strpos($container,"</strong>",$pos);
70
			$end = strpos($container, "</div>",$pos);
71
			$year_of_release = substr($container, $pos + 12, $end - $pos - 12 );
72
	
73
			// PRODUCER
74
			$pos = strpos($container,"<div class=\"label\">");
75
			$pos = strpos($container,"</strong>",$pos);
76
			$end = strpos($container, "</div>",$pos);
77
			$producer = substr($container, $pos + 12, $end - $pos - 12 );
78
	
79
			// TRACKLIST
80
			$tracklist = array();
81
			$start_pos = strpos($container, "<div class=\"track-list\">");
82
			$tracknum_str = "<div class=\"track_num\">";
83
			$trackname_str = "<div class=\"track_name\">";
84
			$pos = 0;
85
	
86
			while ( strpos($container, $tracknum_str,$pos) != FALSE) {
87
				$pos = strpos($container, $tracknum_str,$pos);
88
				$end = strpos($container, "</div>",$pos);
89
				$tracknum = substr($container, $pos + strlen($tracknum_str), $end - $pos - strlen($tracknum_str ) -1);
90
	
91
				$pos = strpos($container, $trackname_str,$pos);
92
				$end = strpos($container, "</div>",$pos);
93
				$trackname = substr($container, $pos + strlen($trackname_str), $end - $pos- strlen($trackname_str));
94
	
95
				$tracklist[(int)$tracknum] = $trackname;           
96
			}
97
	
98
	
99
			// HUONG: DISPLAYING RESULT
100
			echo "Year of release: ".$year_of_release.'<br>';
101
			echo "Producer: ". $producer.'<br>';
102
			echo "<h1>Tracklist</h1>";
103
	
104
			for ($i = 1; $i <= sizeof($tracklist); $i++) {
105
				echo $i.': <a href="index.php?artist='.$artist.'&track='.$tracklist[$i].'"  title="Search on iMusic for '.$artist.' - '.$tracklist[$i].'"">'.$tracklist[$i].'</a><br/>';
106
			}
107
		}
28
                 // Huong: process each result
29
                 // Album info
30
                 $pos = strpos($str, "<strong>Album:</strong>");
31
                 $pos = strpos($str, "<a href=\"",$pos);
32
                 $end = strpos($str, "\">", $pos);
33
                 $album_details = substr($str, $pos + 9, $end - $pos - 9);
34
                 $pos = strpos($str, "</a>", $end);
35
                 $album_name = substr($str, $end + 2, $pos - $end - 2);
36
                 // Artist info
37
                 $pos = strpos($str, "<strong>Artist:</strong>");
38
                 $pos = strpos($str, "<a href=\"",$pos);
39
                 $end = strpos($str, "\">", $pos);
40
                 $artist_details = substr($str, $pos + 9, $end - $pos - 9);
41
                 $pos = strpos($str, "</a>", $end);
42
                 $artist_name = substr($str, $end + 3, $pos - $end - 3);
43
44
                // check for MATCH
45
                 if ((strcasecmp($album,$album_name) == 0)&& (strcasecmp($artist, $artist_name) == 0) )
46
                    {
47
                        break;       // take the first matched
48
                    }
49
                 else $album_details = "";
50
             }
51
             // Huong: found 1
52
             if ($album_details != "") {
53
                 $url = "http://www.gracenote.com". $album_details;
54
                 // scrape album page
55
                 $raw = file_get_contents($url);
56
                 $start_pos = strpos($raw, "<div id=\"main-container\">");
57
                 $end_pos = strpos($raw, "<div class=\"column-right\">" );
58
                 $container = substr($raw, $start_pos, $end_pos - $start_pos);
59
60
                 // YEAR OF RELEASE
61
                 $pos = strpos($container,"<div class=\"year\">");
62
                 $pos = strpos($container,"</strong>",$pos);
63
                 $end = strpos($container, "</div>",$pos);
64
                 $year_of_release = substr($container, $pos + 12, $end - $pos - 12 );
65
66
                 // PRODUCER
67
                 $pos = strpos($container,"<div class=\"label\">");
68
                 $pos = strpos($container,"</strong>",$pos);
69
                 $end = strpos($container, "</div>",$pos);
70
                 $producer = substr($container, $pos + 12, $end - $pos - 12 );
71
72
                 // TRACKLIST
73
                 $tracklist = array();
74
                 $start_pos = strpos($container, "<div class=\"track-list\">");
75
                 $tracknum_str = "<div class=\"track_num\">";
76
                 $trackname_str = "<div class=\"track_name\">";
77
                 $pos = 0;
78
                 while ( strpos($container, $tracknum_str,$pos) != FALSE) {
79
                    $pos = strpos($container, $tracknum_str,$pos);
80
                    $end = strpos($container, "</div>",$pos);
81
                    $tracknum = substr($container, $pos + strlen($tracknum_str), $end - $pos - strlen($tracknum_str ) -1);
82
83
                    $pos = strpos($container, $trackname_str,$pos);
84
                    $end = strpos($container, "</div>",$pos);
85
                    $trackname = substr($container, $pos + strlen($trackname_str), $end - $pos- strlen($trackname_str));
86
87
                    $tracklist[(int)$tracknum] = $trackname;
88
                 }
89
90
91
                 // HUONG: DISPLAYING RESULT
92
                   echo "ARTIST: ". $artist.'<br>';
93
                   echo "ALBUM: ".$album. '<br>';
94
                   echo "Year of release: ".$year_of_release.'<br>';
95
                   echo "Producer: ". $producer.'<br>';
96
                   echo "TRACKLIST".'<br>';
97
                   for ($i = 1; $i <= sizeof($tracklist); $i++)
98
                      echo $i.": ".$tracklist[$i].'<br>';
99
           } else {
100
             // if cannot found on gracenote, try to search from lastfm
101
                   tracklist_lastfm_search ($album);
102
103
           }
104
108
105
	} // if there's no mbid, we search on gracenote -> end
109
106
	
110
107
	// if there's mbid, we search on musicbrainz