public static async Task GetLocalSpotifyTrackInfoAsync() { Process proc = null; try { proc = Process.GetProcessesByName("Spotify").FirstOrDefault (p => !string.IsNullOrWhiteSpace(p.MainWindowTitle)); } catch (Exception e) { Logger.Error("Unable to get spotify track info: " + e.Message); return; } LocalSpotifyTrackInfo localTrackInfo = new LocalSpotifyTrackInfo(); if (proc != null) { // spotify is open, get the track info string spotifyTrackInfo = proc.MainWindowTitle; // split it string[] stringParts = spotifyTrackInfo.Split('-'); if (stringParts != null && stringParts.Length == 2) { localTrackInfo.artist = stringParts[0].Trim(); localTrackInfo.name = stringParts[1].Trim(); } } await HandleTrackInfoAsync(localTrackInfo); }
public LocalSpotifyTrackInfo Clone() { LocalSpotifyTrackInfo info = new LocalSpotifyTrackInfo(); info.start = this.start; info.local_start = this.local_start; info.end = this.end; info.genre = this.genre; info.type = this.type; info.duration = this.duration; info.state = this.state; info.artist = this.artist; info.name = this.name; info.id = this.id; return(info); }
protected static async Task HandleTrackInfoAsync(LocalSpotifyTrackInfo localTrackInfo) { try { if (!SoftwareHttpManager.HasSpotifyAccessToken()) { // initialize the token await SoftwareHttpManager.InitializeSpotifyClientGrantAsync(); } } catch (Exception e) { Logger.Error("Code Time: Unable to access spotify, error: " + e.Message); return; } if (!SoftwareHttpManager.HasSpotifyAccessToken()) { return; } bool hasLocalTrackData = (localTrackInfo.name != null && localTrackInfo.artist != null) ? true : false; bool hasCurrentTrackData = (CurrentTrackInfo != null && CurrentTrackInfo.name != null && CurrentTrackInfo.artist != null) ? true : false; bool isNewTrack = true; if (hasLocalTrackData && hasCurrentTrackData && localTrackInfo.name.Equals(CurrentTrackInfo.name) && localTrackInfo.artist.Equals(CurrentTrackInfo.artist)) { isNewTrack = false; } HttpResponseMessage response = null; try { if (isNewTrack && hasLocalTrackData) { if (hasCurrentTrackData) { // close the previous track CurrentTrackInfo.end = SoftwareCoUtil.getNowInSeconds(); // send it to the app server response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/data/music", CurrentTrackInfo.GetAsJson()); } // fill in the missing attributes from the spotify API await SoftwareHttpManager.GetSpotifyTrackInfoAsync(localTrackInfo); // send it to the app server response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/data/music", localTrackInfo.GetAsJson()); CurrentTrackInfo = localTrackInfo.Clone(); } else if (hasCurrentTrackData && !hasLocalTrackData) { // send this to close it CurrentTrackInfo.end = SoftwareCoUtil.getNowInSeconds(); // send it to the app server response = await SoftwareHttpManager.SendRequestAsync( HttpMethod.Post, "/data/music", CurrentTrackInfo.GetAsJson()); CurrentTrackInfo = null; } } catch (Exception e) { Logger.Error("Code Time: Unable to process track information, error: " + e.Message); } if (response != null && !SoftwareHttpManager.IsOk(response)) { Logger.Error(response.ToString()); } }
public static async Task GetSpotifyTrackInfoAsync(LocalSpotifyTrackInfo trackInfo) { string webResponse = string.Empty; try { HttpClient client = new HttpClient(); string searchQuery = string.Format("artist:{0} track:{1}", trackInfo.artist, trackInfo.name); searchQuery = WebUtility.UrlEncode(searchQuery); string spotifyUrl = string.Format("https://api.spotify.com/v1/search?q={0}&type=track&limit=2&offset=0", searchQuery); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(spotifyUrl); webRequest.Method = "GET"; webRequest.ContentType = "application/json"; webRequest.Accept = "application/json"; webRequest.Headers.Add("Authorization: Bearer " + token.Access_token); HttpWebResponse resp = (HttpWebResponse)await webRequest.GetResponseAsync(); string json = ""; using (Stream respStr = resp.GetResponseStream()) { using (StreamReader rdr = new StreamReader(respStr, Encoding.UTF8)) { // should get back a string we can turn into a json json = await rdr.ReadToEndAsync(); rdr.Close(); } } IDictionary <string, object> jsonObj = (IDictionary <string, object>)SimpleJson.DeserializeObject(json); jsonObj.TryGetValue("tracks", out object trackObj); IDictionary <string, object> trackDict = (trackObj == null) ? null : (IDictionary <string, object>)trackObj; JsonArray items = null; if (trackDict != null) { trackDict.TryGetValue("items", out object itemsObj); items = (itemsObj == null) ? null : (JsonArray)itemsObj; } // need: id, name (already have it), artist (already have it), state, duration // type = "spotify" // other attributes to send: genre, start, end if (items != null && items.Count > 0) { IDictionary <string, object> trackData = (IDictionary <string, object>)items[0]; trackData.TryGetValue("uri", out object spotifyIdObj); string spotifyId = (spotifyIdObj == null) ? null : Convert.ToString(spotifyIdObj); trackData.TryGetValue("duration_ms", out object durationMsObj); long durationSec = (durationMsObj == null) ? 0L : Convert.ToInt64(durationMsObj); trackInfo.duration = durationSec; trackInfo.type = "spotify"; trackInfo.state = "playing"; trackInfo.genre = ""; trackInfo.id = spotifyId; trackInfo.start = SoftwareCoUtil.getNowInSeconds(); double offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes; trackInfo.local_start = trackInfo.start + ((int)offset * 60); } } catch (Exception tex) { Logger.Error("error: " + tex.Message); } }