Skip to content

Commit 429f5e5

Browse files
aleabJohnnyCrazy
authored andcommitted
Fixed GetAlbumArtUrl always returning empty string (#156)
* Fixed GetAlbumArtUrl always returning empty string * Simplified the regex pattern
1 parent 7ea1a1a commit 429f5e5

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

SpotifyAPI/Local/Models/Track.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.IO;
66
using System.Net;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89

910
namespace SpotifyAPI.Local.Models
@@ -66,24 +67,23 @@ public string GetAlbumArtUrl(AlbumArtSize size)
6667
string raw;
6768
using (WebClient wc = new WebClient())
6869
{
70+
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
6971
raw = wc.DownloadString("http://open.spotify.com/album/" + AlbumResource.Uri.Split(new[] { ":" }, StringSplitOptions.None)[2]);
7072
}
7173
raw = raw.Replace("\t", "");
7274

73-
// < meta property = "og:image" content = "http://o.scdn.co/cover/12b318ffe0e4c92f9b4e1486e4726a57e6437ca7" >
74-
// Spotify changed the response so I am now getting the substring from the first line that parses out the above tag.
75+
// <img id="cover-img" src="https://d3rt1990lpmkn.cloudfront.net/640/e62a04cfea4122961f3b9159493730c27d61f71b" ...
7576
string[] lines = raw.Split(new[] { "\n" }, StringSplitOptions.None);
76-
string startString = "<meta property=\"og:image\"";
77-
string endString = "\">";
77+
const string pattern = "id=\"cover-img\".*?src=\"(.*?)\"";
78+
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
7879
foreach (string line in lines)
7980
{
80-
if (line.Trim().Contains("<meta property=\"og:image\""))
81+
MatchCollection matches = rgx.Matches(line);
82+
if (matches.Count > 0)
8183
{
82-
int start = line.IndexOf(startString, 0) + startString.Length;
83-
int end = line.IndexOf(endString, start);
84-
string content = line.Substring(start, end - start);
84+
string content = matches[0].Groups[1].Value;
8585
string[] l = content.Split(new[] { "/" }, StringSplitOptions.None);
86-
return "http://o.scdn.co/" + albumsize + @"/" + l[4].Replace("\"", "").Replace(">", "");
86+
return "http://o.scdn.co/" + albumsize + @"/" + l[4];
8787
}
8888
}
8989
return "";

0 commit comments

Comments
 (0)