Ejemplo n.º 1
0
        public FileInfo GetCover(TrackInfo info)
        {
            string hash = GetMd5Hash(info.Artist + ":" + info.Title);
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "NowPlayingKiosk", "Cache", hash.Substring(0, 2));
            Directory.CreateDirectory(path);

            string filename = Path.Combine(path, hash + ".png");
            FileInfo file = new FileInfo(filename);
            if (file.Exists)
            {
                // Get from cache
                return file;
            }
            else
            {
                Response resp = MakeRequest(info);
                if (resp != null)
                {
                    Item item = ProcessResponse(resp);
                    if (item != null)
                    {
                        DownloadImage(item, file);
                        return file;
                    }
                }
            }

            return GetDefaultCover();
        }
        public TrackInfo WhatIsNowPlaying()
        {
            // Get the process
            Process[] processList = Process.GetProcessesByName("spotify");

            string wantedPrefix = "Spotify - ";
            foreach (Process process in processList)
            {
                if (!String.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.Equals("Spotify"))
                {
                    // Nothing playing right now
                    lastPlayed = null;
                    return null;
                }
                else if (!String.IsNullOrEmpty(process.MainWindowTitle) && process.MainWindowTitle.StartsWith(wantedPrefix))
                {
                    // Spotify is playing something
                    string windowTitle = process.MainWindowTitle.Substring(wantedPrefix.Length);
                    TrackInfo nowPlaying = null;
                    if (windowTitle != null)
                    {
                        // For example "Foo Fighters – Monkey Wrench"
                        int idx = windowTitle.IndexOf('–');
                        if (idx != -1)
                        {
                            string artist = windowTitle.Substring(0, idx).Trim();
                            string title = windowTitle.Substring(idx + 1).Trim();
                            nowPlaying = new TrackInfo(artist, title);
                        }
                    }

                    lastPlayed = nowPlaying;
                    return nowPlaying;
                }
                else if (String.IsNullOrEmpty(process.MainWindowTitle))
                {
                    return lastPlayed;
                }
            }

            return null;
        }
Ejemplo n.º 3
0
 private Response MakeRequest(TrackInfo info)
 {
     try
     {
         string query = "q=artist:" + WebUtility.UrlEncode(info.Artist) + "+track:" + WebUtility.UrlEncode(info.Title);
         string market = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName;
         string url = "https://api.spotify.com/v1/search?" + query + "&type=track&market=" + market;
         HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
         using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
         {
             if (response.StatusCode != HttpStatusCode.OK)
                 throw new Exception(String.Format(
                 "Server error (HTTP {0}: {1}).",
                 response.StatusCode,
                 response.StatusDescription));
             DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
             object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
             Response jsonResponse
             = objResponse as Response;
             return jsonResponse;
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return null;
     }
 }