public static string md5(string input) { //MD5.CS is missing from Source Control for IronCow.WindowsPhone solution //https://ironcow.codeplex.com/workitem/6841 //http://www.jeff.wilcox.name/2008/03/silverlight-2-md5/ return(MD5CryptoServiceProvider.GetMd5String(input)); }
internal static string CalSig(List <APIParameter> parameters) { parameters.Sort(new ParameterComparer()); StringBuilder sbList = new StringBuilder(); foreach (APIParameter para in parameters) { sbList.AppendFormat("{0}={1}", para.Name, para.Value); } sbList.Append(Client.ClientSecret); return(MD5CryptoServiceProvider.GetMd5String((sbList.ToString()))); }
async void btn_SettingsLoginLastFM_Click(object sender, System.Windows.RoutedEventArgs e) { try { //Check account name and password if (!String.IsNullOrWhiteSpace(txt_SettingsLastfmAccount.Text) && !String.IsNullOrWhiteSpace(txt_SettingsLastfmPassword.Password)) { vApplicationSettings["LastfmAccount"] = txt_SettingsLastfmAccount.Text.ToLower(); if (vApplicationSettings["LastfmPassword"].ToString() != txt_SettingsLastfmPassword.Password) { vApplicationSettings["LastfmPassword"] = MD5CryptoServiceProvider.GetMd5String(txt_SettingsLastfmPassword.Password); txt_SettingsLastfmPassword.Password = vApplicationSettings["LastfmPassword"].ToString(); } vApplicationSettings.Save(); await LastFMAuth(); if ((bool)vApplicationSettings["LoadLastFMData"]) { await LoadLoved(); await LoadRecent(); await LoadArtists(); //await LoadLikes(); } } else { if (String.IsNullOrWhiteSpace(txt_SettingsLastfmAccount.Text)) { txt_SettingsLastfmAccount.Focus(); } else if (String.IsNullOrWhiteSpace(txt_SettingsLastfmPassword.Password)) { txt_SettingsLastfmPassword.Focus(); } MessageBox.Show("Please enter your Last.fm account name and password to login to your Last.fm profile.", "ScrobbleMe", MessageBoxButton.OK); } } catch { } return; }
async Task LastFMAuth() { try { //Check if there is an internet connection if (!NetworkInterface.GetIsNetworkAvailable()) { MessageBox.Show("It seems like you currently don't have an internet connection available, please make sure you have an internet connection before you try to login.", "ScrobbleMe", MessageBoxButton.OK); return; } else { //Auth to Last.fm ProgressDisableUI("Logging in to your Last.fm profile..."); string LastFMApiKey = "a62159e276986acf81f6990148b06ae3"; //Yes, I know I didn't remove the api key. string LastFMApiSecret = "fa570ce8eeb81a3e1685b0e8a27d6517"; //Yes, I know I didn't remove the api key. string LastFMMethod = "auth.getMobileSession"; string LastFMAuthToken = MD5CryptoServiceProvider.GetMd5String(vApplicationSettings["LastfmAccount"].ToString().ToLower() + vApplicationSettings["LastfmPassword"].ToString()); string LastFMApiSig = MD5CryptoServiceProvider.GetMd5String("api_key" + LastFMApiKey + "authToken" + LastFMAuthToken + "method" + LastFMMethod + "username" + vApplicationSettings["LastfmAccount"].ToString().ToLower() + LastFMApiSecret); XDocument ResponseXml = null; using (HttpClient HttpClientLogin = new HttpClient()) { HttpClientLogin.DefaultRequestHeaders.Add("User-Agent", "ScrobbleMe"); HttpClientLogin.DefaultRequestHeaders.Add("Accept-Charset", "UTF-8"); HttpClientLogin.DefaultRequestHeaders.Add("Cache-Control", "no-cache, no-store"); Uri PostUri = new Uri("https://ws.audioscrobbler.com/2.0/"); HttpStringContent PostContent = new HttpStringContent("api_key=" + HttpUtility.UrlEncode(LastFMApiKey) + "&authToken=" + HttpUtility.UrlEncode(LastFMAuthToken) + "&method=" + HttpUtility.UrlEncode(LastFMMethod) + "&username="******"LastfmAccount"].ToString().ToLower()) + "&api_sig=" + HttpUtility.UrlEncode(LastFMApiSig), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/x-www-form-urlencoded"); ResponseXml = XDocument.Parse((await HttpClientLogin.PostAsync(PostUri, PostContent)).Content.ToString()); } if (ResponseXml.Element("lfm").Attribute("status").Value == "ok") { if (!(bool)vApplicationSettings["LastfmReduceConfirmation"]) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("You have successfully logged into your Last.fm profile, you can now begin to scrobble on the Scrobble tab.", "ScrobbleMe", MessageBoxButton.OK); }); } vApplicationSettings["LastfmSessionKey"] = ResponseXml.Element("lfm").Element("session").Element("key").Value; vApplicationSettings.Save(); ProgressEnableUI(); return; } else if (!String.IsNullOrEmpty(ResponseXml.Element("lfm").Element("error").Value)) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to auth with Last.fm, please check your Last.fm account settings or check your internet connection.\n\nError Message: " + ResponseXml.Element("lfm").Element("error").Value, "ScrobbleMe", MessageBoxButton.OK); }); vApplicationSettings["LastfmSessionKey"] = ""; vApplicationSettings.Save(); ProgressEnableUI(); return; } } } catch (Exception ex) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to auth with Last.fm, please check your Last.fm account settings or check your internet connection.\n\nException Message: " + ex.Message, "ScrobbleMe", MessageBoxButton.OK); }); vApplicationSettings["LastfmSessionKey"] = ""; vApplicationSettings.Save(); ProgressEnableUI(); return; } }
async void ScrobbleSongs() { try { //Check for songs to scrobble ProgressDisableUI("Checking scrobble songs..."); List <TblSong> DBSongQueryList = DBCon.TblSong.ToList().Where(x => x.Plays > 0 & x.Plays > x.Scrobbles & x.Ignored == 0).ToList(); //Check if there are songs to scrobble if (DBSongQueryList.Count == 0) { if (!(bool)vApplicationSettings["LastfmReduceConfirmation"]) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("There were no songs found to scrobble, now it's time to play some new songs!", "ScrobbleMe", MessageBoxButton.OK); }); } ProgressEnableUI(); return; } //Sort songs to scrobble if (!(bool)vApplicationSettings["LastfmScrobbleArtistOrder"]) { if ((bool)vApplicationSettings["LastfmScrobbleSongOrder"]) { DBSongQueryList = DBSongQueryList.OrderBy(x => x.Artist).ThenBy(x => x.Album).ThenBy(x => x.Title).ToList(); } else { DBSongQueryList = DBSongQueryList.OrderBy(x => x.Artist).ThenBy(x => x.Album).ThenBy(x => x.Track).ToList(); } } else { if ((bool)vApplicationSettings["LastfmScrobbleSongOrder"]) { DBSongQueryList = DBSongQueryList.OrderByDescending(x => x.Artist).ThenByDescending(x => x.Album).ThenByDescending(x => x.Title).ToList(); } else { DBSongQueryList = DBSongQueryList.OrderByDescending(x => x.Artist).ThenByDescending(x => x.Album).ThenByDescending(x => x.Track).ToList(); } } //Last.fm scrobble song int SubmitCountTotal = 0; int SubmitCountBatch = 0; int SubmitCountMax = 0; string SubmitResult = ""; string SubmitErrorMsg = ""; DateTime SubmitUnixTime = DateTime.UtcNow.AddMinutes(-(int)vApplicationSettings["LastfmDelayTimeInt"]); //Submit to Last.fm #1 string LastFMApiKey = "a62159e276986acf81f6990148b06ae3"; //Yes, I know I didn't remove the api key. string LastFMApiSecret = "fa570ce8eeb81a3e1685b0e8a27d6517"; //Yes, I know I didn't remove the api key. string LastFMMethod = "track.scrobble"; List <int> ScrobbleBatchListId = new List <int>(); Dictionary <string, string> ScrobbleBatchList = new Dictionary <string, string>(); ScrobbleBatchList.Add("api_key", LastFMApiKey); ScrobbleBatchList.Add("method", LastFMMethod); ScrobbleBatchList.Add("sk", vApplicationSettings["LastfmSessionKey"].ToString()); //Calculate maximum songs foreach (TblSong MaxDBSong in DBSongQueryList) { int ScrobbleCount = MaxDBSong.Plays - MaxDBSong.Scrobbles; if ((bool)vApplicationSettings["SkipMultiplePlays"]) { ScrobbleCount = 1; } for (int i = 1; i <= ScrobbleCount; i++) { SubmitCountMax++; } } //Batch scrobble songs foreach (TblSong DBSong in DBSongQueryList) { int ScrobbleCount = DBSong.Plays - DBSong.Scrobbles; if ((bool)vApplicationSettings["SkipMultiplePlays"]) { ScrobbleCount = 1; } for (int i = 1; i <= ScrobbleCount; i++) { //Calculate scrobble time TimeSpan SongSeconds = new TimeSpan(Convert.ToDateTime(DBSong.Duration).Hour, Convert.ToDateTime(DBSong.Duration).Minute, Convert.ToDateTime(DBSong.Duration).Second); SubmitUnixTime = SubmitUnixTime.AddSeconds(-SongSeconds.TotalSeconds); long SubmitUnixTimeTicks = (SubmitUnixTime.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks) / 10000000; ScrobbleBatchListId.Add(DBSong.Id); ScrobbleBatchList.Add("album[" + SubmitCountBatch + "]", DBSong.Album); ScrobbleBatchList.Add("artist[" + SubmitCountBatch + "]", DBSong.Artist); ScrobbleBatchList.Add("duration[" + SubmitCountBatch + "]", Convert.ToString(SongSeconds.TotalSeconds)); ScrobbleBatchList.Add("timestamp[" + SubmitCountBatch + "]", Convert.ToString(SubmitUnixTimeTicks)); ScrobbleBatchList.Add("track[" + SubmitCountBatch + "]", DBSong.Title); ScrobbleBatchList.Add("trackNumber[" + SubmitCountBatch + "]", Convert.ToString(DBSong.Track)); SubmitCountBatch++; SubmitCountTotal++; if (SubmitCountBatch == 15 || SubmitCountTotal == SubmitCountMax) { ProgressDisableUI("Scrobbling songs: " + SubmitCountTotal + "/" + SubmitCountMax); List <string> ScrobbleBatchListSorted = ScrobbleBatchList.Select(x => x.Key).ToList(); ScrobbleBatchListSorted.Sort(StringComparer.Ordinal); //Build Signature String StringBuilder sbSig = new StringBuilder(); foreach (string Key in ScrobbleBatchListSorted) { sbSig.Append(Key.ToString() + ScrobbleBatchList[Key]); } sbSig.Append(LastFMApiSecret); //Build Post String StringBuilder sbPost = new StringBuilder(); foreach (string Key in ScrobbleBatchListSorted) { sbPost.Append("&" + Key.ToString() + "=" + HttpUtility.UrlEncode(ScrobbleBatchList[Key])); } sbPost.Append("&api_sig=" + HttpUtility.UrlEncode(MD5CryptoServiceProvider.GetMd5String(sbSig.ToString()))); XDocument ResponseXml = null; using (HttpClient HttpClientSubmit = new HttpClient()) { HttpClientSubmit.DefaultRequestHeaders.Add("User-Agent", "ScrobbleMe"); HttpClientSubmit.DefaultRequestHeaders.Add("Accept-Charset", "UTF-8"); HttpClientSubmit.DefaultRequestHeaders.Add("Cache-Control", "no-cache, no-store"); Uri PostUri = new Uri("https://ws.audioscrobbler.com/2.0/"); HttpStringContent PostContent = new HttpStringContent(sbPost.ToString(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/x-www-form-urlencoded"); ResponseXml = XDocument.Parse((await HttpClientSubmit.PostAsync(PostUri, PostContent)).Content.ToString()); } //Submit to Last.fm #2 if (ResponseXml.Element("lfm").Attribute("status").Value == "ok") { SubmitResult = "ok"; //Mark batch songs as scrobbled foreach (int SongID in ScrobbleBatchListId) { TblSong DBSongID = DBSongQueryList.Where(x => x.Id == SongID).FirstOrDefault(); DBSongID.Scrobbles = DBSongID.Plays; } DBCon.SubmitChanges(); if (Convert.ToUInt32(ResponseXml.Element("lfm").Element("scrobbles").Attribute("ignored").Value) > 0) { SubmitErrorMsg = " But some songs might have been ignored by Last.fm."; } } else if (!String.IsNullOrEmpty(ResponseXml.Element("lfm").Element("error").Value)) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to scrobble your songs to Last.fm, please check your Last.fm account settings, try to relog into your account or check your internet connection.\n\nError Message: " + ResponseXml.Element("lfm").Element("error").Value, "ScrobbleMe", MessageBoxButton.OK); }); SubmitResult = "failed"; break; } SubmitCountBatch = 0; ScrobbleBatchListId.Clear(); ScrobbleBatchList.Clear(); ScrobbleBatchList.Add("api_key", LastFMApiKey); ScrobbleBatchList.Add("method", LastFMMethod); ScrobbleBatchList.Add("sk", vApplicationSettings["LastfmSessionKey"].ToString()); } } ; if (SubmitResult == "failed") { break; } } if (SubmitResult == "ok") { //Set last scrobble date vApplicationSettings["LastfmScrobbled"] = DateTime.Now.ToString("d MMMM yyyy", new System.Globalization.CultureInfo("en-US")) + " at " + DateTime.Now.ToShortTimeString(); vApplicationSettings.Save(); if (!(bool)vApplicationSettings["LastfmReduceConfirmation"]) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("All your played songs have successfully been scrobbled to your Last.fm profile." + SubmitErrorMsg, "ScrobbleMe", MessageBoxButton.OK); }); } } } catch (Exception ex) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to scrobble your songs to Last.fm, please check your Last.fm account settings, try to relog into your account or check your internet connection.\n\nException Message: " + ex.Message, "ScrobbleMe", MessageBoxButton.OK); }); } if ((bool)vApplicationSettings["LoadLastFMData"]) { await LoadLoved(); await LoadArtists(); //await LoadLikes(); } await LoadRecent(); Thread ThreadLoadScrobble = new Thread(() => LoadScrobbleIgnore()); ThreadLoadScrobble.Start(); return; }
//Sync loved songs async void SyncLovedSongs() { try { //Check loved songs ProgressDisableUI("Checking for loved songs..."); List <Song> MediaLibraryLovedList = vMediaLibrary.Songs.ToList().Where(x => x.Rating == 8).ToList(); if (MediaLibraryLovedList.Count == 0) { if (!(bool)vApplicationSettings["LastfmReduceConfirmation"]) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("It seems like you haven't loved any songs yet, it's time to start loving your music a bit more in your music player <3", "ScrobbleMe", MessageBoxButton.OK); }); } ProgressEnableUI(); return; } //Submit loved songs int SubmitCount = 0; string SubmitResult = ""; string SubmitErrorMsg = ""; //Submit to Last.fm #1 string LastFMApiKey = "a62159e276986acf81f6990148b06ae3"; //Yes, I know I didn't remove the api key. string LastFMApiSecret = "fa570ce8eeb81a3e1685b0e8a27d6517"; //Yes, I know I didn't remove the api key. string LastFMMethod = "track.love"; foreach (Song DevSong in MediaLibraryLovedList.ToList()) { SubmitCount++; ProgressDisableUI("|" + SubmitCount + "/" + MediaLibraryLovedList.Count() + "| " + DevSong.Artist + " - " + DevSong.Name + " (" + DevSong.Album + ")"); //Submit to Last.fm #2 string LastFMApiSig = MD5CryptoServiceProvider.GetMd5String("api_key" + LastFMApiKey + "artist" + DevSong.Artist.ToString() + "method" + LastFMMethod + "sk" + vApplicationSettings["LastfmSessionKey"].ToString() + "track" + DevSong.Name.ToString() + LastFMApiSecret); XDocument ResponseXml = null; using (HttpClient HttpClientLoved = new HttpClient()) { HttpClientLoved.DefaultRequestHeaders.Add("User-Agent", "ScrobbleMe"); HttpClientLoved.DefaultRequestHeaders.Add("Accept-Charset", "UTF-8"); HttpClientLoved.DefaultRequestHeaders.Add("Cache-Control", "no-cache, no-store"); Uri PostUri = new Uri("https://ws.audioscrobbler.com/2.0/"); HttpStringContent PostContent = new HttpStringContent("api_key=" + HttpUtility.UrlEncode(LastFMApiKey) + "&artist=" + HttpUtility.UrlEncode(DevSong.Artist.ToString()) + "&method=" + HttpUtility.UrlEncode(LastFMMethod) + "&sk=" + HttpUtility.UrlEncode(vApplicationSettings["LastfmSessionKey"].ToString()) + "&track=" + HttpUtility.UrlEncode(DevSong.Name.ToString()) + "&api_sig=" + HttpUtility.UrlEncode(LastFMApiSig), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/x-www-form-urlencoded"); ResponseXml = XDocument.Parse((await HttpClientLoved.PostAsync(PostUri, PostContent)).Content.ToString()); } if (ResponseXml.Element("lfm").Attribute("status").Value == "ok") { SubmitResult = "ok"; } else if (!String.IsNullOrEmpty(ResponseXml.Element("lfm").Element("error").Value)) { if (ResponseXml.Element("lfm").Element("error").Value.Contains("Track not recognised")) { SubmitErrorMsg = " But atleast one song was not recognized and loved."; SubmitResult = "ok"; } else { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to love your songs on Last.fm, please check your Last.fm account settings, try to relog into your account or check your internet connection.\n\nError Message: " + ResponseXml.Element("lfm").Element("error").Value, "ScrobbleMe", MessageBoxButton.OK); }); SubmitResult = "failed"; break; } } } if (SubmitResult == "ok") { if (!(bool)vApplicationSettings["LastfmReduceConfirmation"]) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Your loved songs have successfully been submitted to your Last.fm profile." + SubmitErrorMsg, "ScrobbleMe", MessageBoxButton.OK); }); } } } catch (Exception ex) { Dispatcher.BeginInvoke(delegate { MessageBox.Show("Failed to love your songs on Last.fm, please check your Last.fm account settings, try to relog into your account or check your internet connection.\n\nException Message: " + ex.Message, "ScrobbleMe", MessageBoxButton.OK); }); } await LoadLoved(); ProgressEnableUI(); return; }