|
4 | 4 | using System.Drawing;
|
5 | 5 | using System.IO;
|
6 | 6 | using System.Net;
|
| 7 | +using System.Text.RegularExpressions; |
7 | 8 | using System.Threading.Tasks;
|
8 | 9 |
|
9 | 10 | namespace SpotifyAPI.Local.Models
|
@@ -66,24 +67,23 @@ public string GetAlbumArtUrl(AlbumArtSize size)
|
66 | 67 | string raw;
|
67 | 68 | using (WebClient wc = new WebClient())
|
68 | 69 | {
|
| 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"); |
69 | 71 | raw = wc.DownloadString("http://open.spotify.com/album/" + AlbumResource.Uri.Split(new[] { ":" }, StringSplitOptions.None)[2]);
|
70 | 72 | }
|
71 | 73 | raw = raw.Replace("\t", "");
|
72 | 74 |
|
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" ... |
75 | 76 | 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); |
78 | 79 | foreach (string line in lines)
|
79 | 80 | {
|
80 |
| - if (line.Trim().Contains("<meta property=\"og:image\"")) |
| 81 | + MatchCollection matches = rgx.Matches(line); |
| 82 | + if (matches.Count > 0) |
81 | 83 | {
|
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; |
85 | 85 | 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]; |
87 | 87 | }
|
88 | 88 | }
|
89 | 89 | return "";
|
|
0 commit comments