/
View.aspx.cs
51 lines (42 loc) · 1.95 KB
/
View.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TuneRank
{
public partial class View : System.Web.UI.Page
{
// define MusixMatch API key
private const string apiKey = "cbd3340887455ec7c422dd1564e02ec6";
protected void Page_Load(object sender, EventArgs e)
{
// extract the MusixMatch song ID from the query string
int trackId = Convert.ToInt32(Request.QueryString["id"]);
// initialize web client
WebClient client = new WebClient();
string response1 = client.DownloadString("http://api.musixmatch.com/ws/1.1/track.get?track_id=" + trackId + "&apikey=" + apiKey);
// fetch the track information from MusixMatch
var track = Json.Decode(response1).message.body.track;
// output the track and artist name
this.lblTitle.Text = track.track_name;
this.lblArtist.Text = track.artist_name;
// read the previously downloaded lyrics from disk
StreamReader sr = new StreamReader(Server.MapPath("~/Data/" + track.track_id + ".txt"));
this.lblLyrics.Text = sr.ReadToEnd().Replace("\n", "<br />\n");
// download the Spotify embed component
string response2 = client.DownloadString("http://ws.spotify.com/search/1/track.json?q=" + track.track_name);
if (Json.Decode(response2).tracks.Count > 0)
{
// parse the Spotify URI for this song
var spotifyUri = Json.Decode(response2).tracks[0].href;
// output the Spotify embed component
this.litPlayButton.Text = "<iframe src='https://embed.spotify.com/?uri=" + spotifyUri + "' width='400' height='480' frameborder='0' allowtransparency='true'></iframe>";
}
}
}
}