private async void Form1_Load(object sender, EventArgs e) { spotify.Update(); progressBar1.Maximum = (int)mh.GetCurrentTrack().GetLength() * 100; pictureBox1.Image = await spotify.GetMusicHandler().GetCurrentTrack().GetAlbumArtAsync(AlbumArtSize.SIZE_160); pictureBox2.Image = await spotify.GetMusicHandler().GetCurrentTrack().GetAlbumArtAsync(AlbumArtSize.SIZE_640); linkLabel1.Text = mh.GetCurrentTrack().GetTrackName(); linkLabel1.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetTrackURI()); linkLabel2.Text = mh.GetCurrentTrack().GetArtistName(); linkLabel2.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetArtistURI()); linkLabel3.Text = mh.GetCurrentTrack().GetAlbumName(); linkLabel3.LinkClicked += (senderTwo, args) => Process.Start(mh.GetCurrentTrack().GetAlbumURI()); label9.Text = mh.IsPlaying().ToString(); label11.Text = ((int)(mh.GetVolume() * 100)).ToString(); label7.Text = mh.IsAdRunning().ToString(); eh.OnTrackChange += new SpotifyEventHandler.TrackChangeEventHandler(trackchange); eh.OnTrackTimeChange += new SpotifyEventHandler.TrackTimeChangeEventHandler(timechange); eh.OnPlayStateChange += new SpotifyEventHandler.PlayStateEventHandler(playstatechange); eh.OnVolumeChange += new SpotifyEventHandler.VolumeChangeEventHandler(volumechange); eh.SetSynchronizingObject(this); eh.ListenForEvents(true); }
private async void UpdateTrack(Track track) { if (track != null && !mh.IsAdRunning()) { string artistName = track.GetArtistName(); string trackName = track.GetTrackName(); double totalTimeSeconds = track.GetLength(); string totalTime = formatTime(totalTimeSeconds); App.Current.MainWindow.Title = String.Format("{0} - {1} - {2}", appName, artistName, trackName); model.TrackName = trackName; model.ArtistName = artistName; model.TotalTimeSeconds = totalTimeSeconds; model.TotalTime = totalTime; model.Image = await track.GetAlbumArtAsync(AlbumArtSize.SIZE_640); growl.TrackChanged(artistName, trackName, track.GetAlbumArtURL(AlbumArtSize.SIZE_160)); model.LastTrack = track; } }
//executeDownload(mh.GetCurrentTrack().GetArtistName(), mh.GetCurrentTrack().GetTrackName(), true); private async Task executeDownload(string artist, string name, bool tellIfAlreadyExists = false) { tryNumber = 0; time = 0; startdownload: try { if (downloading) { addToLog("Already downloading a song, ignoring requested download", logBox); return; } downloading = true; string term = artist + " - " + name; //Building search term term = new Regex(string.Format("[{0}]", Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars())))).Replace(term, ""); //Removing any invalid characters? if (string.IsNullOrWhiteSpace(artist) || string.IsNullOrWhiteSpace(name) || mh.IsAdRunning()) //If there is no artist or no name or an ad is running return since theres nothing to download { downloading = false; return; } if (term.Length > 16) { if (term.Substring(term.Length - 15).ToLower() == " - original mix") { term = term.Substring(0, term.Length - 15); //Remove "Original Mix" } } if (term.Contains(" - feat")) { term = term.Split(new string[] { " - feat" }, StringSplitOptions.None)[0]; //Remove feat } lastDownloaded = term; if (!File.Exists(browseForFolderBox.Text + lastDownloaded + ".mp3")) //If the song wasent downloaded to the folder already { addToLog("Searching MP3Clan for term \"" + term + "\"", logBox); string pageSource = client.DownloadString(new Uri(string.Format("http://mp3clan.com/mp3_source.php?q={0}", term.Replace(" ", "+")))); //Perform search and get page source //Perfom a search query in your browser and you can view the source to understand the next line Match trackId = new Regex("<div class=\"mp3list-table\" id=\"(.+?)\">").Match(pageSource); //Checks if div exists in the page source //What is (.+?) if (!trackId.Success || string.IsNullOrWhiteSpace(trackId.Groups[1].Value)) //If there was no match or ...I don't know what is trackId.Groups[1].Value? Is it the ID of the div? { if (tryNumber < 3) //If try number is less than 3, retry download { downloading = false; tryNumber++; addToLog("Could not find TrackID, retrying " + tryNumber + "/3", logBox); goto startdownload; } else //If try number is greater than 3, then skip the download/give up { addToLog("Could not find TrackID, skipping download", logBox); downloading = false; return; } } addToLog("TrackId: " + trackId.Groups[1].Value, logBox); string dlLink = string.Format("http://mp3clan.com/app/get.php?mp3={0}", trackId.Groups[1].Value); addToLog("Downloading from link: " + dlLink, logBox); sw.Start(); //Start the download stopwatch await Task.WhenAll(downloadFileAsync(dlLink, lastDownloaded)); //Download the track } else { if (tellIfAlreadyExists) { addToLog("Song already downloaded", logBox); } downloading = false; } //FIle already downloaded FileInfo fileInfo = new FileInfo(browseForFolderBox.Text + lastDownloaded + ".mp3"); if (fileInfo.Length < 1000000 && retryIfUnder1Mb.Checked) //If length of file is less than 1mb and retry under 1mb is checked then retry download { if (tryNumber < 3) { downloading = false; tryNumber++; if (File.Exists(browseForFolderBox.Text + lastDownloaded + ".mp3")) { File.Delete(browseForFolderBox.Text + lastDownloaded + ".mp3"); } addToLog("File downloaded was under 1MB, redownloading try " + tryNumber + "/3", logBox); goto startdownload; } else { downloading = false; addToLog(term + " failed to download every try, skipping", logBox); if (Settings.Default.DownloadNotifications) { notifyIcon.ShowBalloonTip(3000, "Download error", "The download for \"" + lastDownloaded + "\" failed to download.", ToolTipIcon.Error); } } } downloading = false; } catch (Exception e) { downloading = false; tryNumber++; addToLog("Error downloading file, retrying " + tryNumber + "\n" + e.ToString(), logBox); goto startdownload; } }