private static void setAmtSongs(int thread, int songs) { QabuzeDLMThreadStatus tmp = getStatusByThread(thread); tmp.setAmtSongs(songs); while (frmDLStat.instance.updating) { Thread.Sleep(1); } status[thread] = tmp; }
private static QabuzeDLMThreadStatus getStatusByThread(int thread) { QabuzeDLMThreadStatus tmp; try { tmp = status[thread]; } catch (KeyNotFoundException) { tmp = new QabuzeDLMThreadStatus(); tmp.thread = thread; } return(tmp); }
private static void pushStatus(int thread, DLMEvent dlmevent, string logEntry, int song = 0) { string prefix = "Thread #" + thread.ToString() + " (" + dlmevent.ToString() + "): "; string entry = prefix + logEntry; logs.Add(entry); Console.WriteLine(entry); QabuzeDLMThreadStatus tmp = getStatusByThread(thread); if (song == 0) { tmp.updateThreadStatus(dlmevent, logEntry); if (dlmevent == DLMEvent.done) { string threadDoneEntry = "Total songs: " + tmp.amtSongs.ToString() + " Loaded: " + tmp.songsLoaded.ToString() + " Tagged: " + tmp.songsTagged.ToString() + " Done: " + tmp.songsDone.ToString() + " Failed: " + tmp.songsFailed.ToString(); logs.Add(prefix + threadDoneEntry); Console.WriteLine(prefix + threadDoneEntry); tmp.log.Add(threadDoneEntry); } else if (dlmevent == DLMEvent.failed) { string threadDoneEntry = "Total songs: " + tmp.amtSongs.ToString() + " Loaded: " + tmp.songsLoaded.ToString() + " Tagged: " + tmp.songsTagged.ToString() + " Done: " + tmp.songsDone.ToString() + " Failed: " + tmp.songsFailed.ToString(); logs.Add(prefix + threadDoneEntry); Console.WriteLine(prefix + threadDoneEntry); tmp.log.Add(threadDoneEntry); } } else { tmp.addSongStatus(dlmevent); } while (frmDLStat.instance.updating) { Thread.Sleep(1); } status[thread] = tmp; updateProgressBar(); frmDLStat.instance.updateThreadSafe(status); }
private static void DownloadAsyncWorker(QabuzeAlbum album) { #if (WITHDOWNLOAD) int currentThread = amtThreads; bool tmp = false; amtThreads++; setAmtSongs(currentThread, album.songs.Count); getStatusByThread(currentThread).album = album; while (activeThreads >= maxActiveThreads) { Thread.Sleep(100); if (!tmp) { pushStatus(currentThread, DLMEvent.waiting, "Waiting for a slot ..."); tmp = true; } } activeThreads++; WebClient webClient = new WebClient(); pushStatus(currentThread, DLMEvent.working, "Starting with the download of " + album.songs.Count.ToString() + " songs."); KeyValuePair <int, QabuzeSong> kvp_song; for (int i = 0; i < album.songs.Count; i++) { kvp_song = album.songs[i]; string foldername = (QabuzeAPI.Config.getInstance().outputFolder + "\\" + @Utils.escapeMetaString(QabuzeAPI.Config.getInstance().folderScheme, album, kvp_song.Value)); System.IO.Directory.CreateDirectory(foldername); if (!System.IO.File.Exists(foldername + "\\folder.jpg")) { webClient.DownloadFile(new Uri(album.coverURL), foldername + "\\folder.jpg"); pushStatus(currentThread, DLMEvent.working, "Downloaded Cover!"); } //kvp_song.Value.track_id, kvp_song.Value.getDownloadLink(Properties.Settings.Default.lossless)) string filename = Utils.escapeMetaString(QabuzeAPI.Config.getInstance().fileScheme, album, kvp_song.Value).Replace("\\", ""); string fullFilename = Utils.truncateIfNecessary(foldername + "\\" + @filename); if (System.IO.File.Exists(fullFilename)) { pushStatus(currentThread, DLMEvent.done, "Skipping \"" + filename + "\"! File already exists...", i + 1); continue; } pushStatus(currentThread, DLMEvent.working, "Now downloading \"" + filename + "\" ...", i + 1); string url = kvp_song.Value.getDownloadLink(true); if (url == null) { pushStatus(currentThread, DLMEvent.downloadFailed, "ERROR with \"" + filename + "\"!", i + 1); continue; } try { bool finished = false; while (!finished) { webClient.DownloadFile(new Uri(url), fullFilename); if (System.IO.File.Exists(fullFilename)) { pushStatus(currentThread, DLMEvent.loaded, "Downloading \"" + filename + "\" succeeded!", i + 1); finished = true; } else { pushStatus(currentThread, DLMEvent.working, "Retry download \"" + filename + "\"...", i + 1); } System.Threading.Thread.Sleep(500); // Sleep to make sure the file-lock has vanished. } } catch (Exception e) { pushStatus(currentThread, DLMEvent.working, e.InnerException + "\n------\n" + e.Message, i + 1); pushStatus(currentThread, DLMEvent.loaded, "Downloading \"" + filename + "\" failed!", i + 1); } TagLib.File tlFile = TagLib.Flac.File.Create(fullFilename); tlFile.Tag.Comment = "Created by Qabuze v" + typeof(frmMain).Assembly.GetName().Version; tlFile.Tag.Performers = new String[] { kvp_song.Value.artist }; tlFile.Tag.Title = kvp_song.Value.title; tlFile.Tag.Album = kvp_song.Value.album; tlFile.Tag.Genres = new String[] { kvp_song.Value.genre }; tlFile.Tag.Track = (uint)kvp_song.Value.track_number; tlFile.Tag.TrackCount = (uint)album.track_count; tlFile.Tag.Disc = (uint)kvp_song.Value.media_number; tlFile.Tag.AlbumArtists = new String[] { album.artist }; tlFile.Tag.Pictures = new TagLib.IPicture[] { new TagLib.Picture(foldername + @"\folder.jpg") }; #region old stuff /*Process proc = new Process * { * StartInfo = new ProcessStartInfo * { * FileName = "metaflac.exe", * Arguments = "--set-tag=\"ARTIST=" + kvp_song.Value.artist + "\" --set-tag=\"TITLE=" + kvp_song.Value.title + "\" --set-tag=\"ALBUM=" + kvp_song.Value.album + "\" --set-tag=\"GENRE=" + kvp_song.Value.genre + "\" --set-tag=\"TRACKNUMBER=" + kvp_song.Value.track_number + "\" --set-tag=\"ORGANIZATION=" + album.label + "\" --set-tag=\"TRACKTOTAL=" + album.track_count + "\" --set-tag=\"ALBUMARTIST=" + album.artist + "\" --set-tag=\"DISKNUMBER=" + kvp_song.Value.media_number + "\" --set-tag=\"COMMENT=" + "Created by Quabuze v" + typeof(frmMain).Assembly.GetName().Version + "\" --import-picture-from=\"" + foldername + "\\folder.jpg" + "\" \"" + fullFilename + "\"", + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + } + }; + int amtRetries = 0; + string buffer = ""; + bool mayContinue = false; + do + { + proc.Refresh(); + buffer = ""; + pushStatus(currentThread, DLMEvent.working, "Now tagging \"" + filename + "\" ...", i + 1); + try + { + proc.Start(); + while (!proc.StandardOutput.EndOfStream) + { + buffer += proc.StandardOutput.ReadLine() + "\n"; + } + while (!proc.StandardError.EndOfStream) + { + buffer += proc.StandardError.ReadLine() + "\n"; + } + Console.Write(buffer); + pushStatus(currentThread, DLMEvent.working, "Exitcode of metaflac: " + proc.ExitCode.ToString()); + amtRetries++; + mayContinue = (proc.ExitCode != 0 && amtRetries < 5); + } + catch (Exception) + { + pushStatus(currentThread, DLMEvent.taggingFailed, "Tagging \"" + filename + "\" FAILED!", i + 1); + mayContinue = false; + } + + } while (mayContinue); */ #endregion try { tlFile.Save(); pushStatus(currentThread, DLMEvent.tagged, "Tagging \"" + filename + "\" done!", i + 1); pushStatus(currentThread, DLMEvent.done, "Done with \"" + filename + "\"!", i + 1); } catch (Exception) { pushStatus(currentThread, DLMEvent.taggingFailed, "Tagging \"" + filename + "\" FAILED!", i + 1); } //pb.Increment(1); } QabuzeDLMThreadStatus _ct = getStatusByThread(currentThread); if (_ct.songsFailed > 0) { pushStatus(currentThread, DLMEvent.failed, "failure!"); } else { pushStatus(currentThread, DLMEvent.done, "done!"); } activeThreads--; finishedThreads++; #endif }