Beispiel #1
0
 private void AddTagsToFile(String fileName, TrackDto track)
 {
     using (TagLib.File f = TagLib.File.Create(fileName))
     {
         f.Tag.Title      = track.Title;
         f.Tag.Performers = new[] { track.Artist };
         f.Tag.Year       = (uint)track.AlbumYear;
         f.Tag.Album      = track.AlbumTitle;
         f.Save();
     }
 }
Beispiel #2
0
        private void DownloadWorker(BlockingCollection <TrackDto> queue, PageInfo pageInfo, String path)
        {
            bool     hasTracks = true;
            TrackDto track     = null;

            while (hasTracks)
            {
                if (cts.Token.IsCancellationRequested)
                {
                    cts.Token.ThrowIfCancellationRequested();
                }

                try
                {
                    track = queue.Take();
                }
                catch (InvalidOperationException e)
                {
                    hasTracks = false;
                }

                if (hasTracks && track != null)
                {
                    String dest = GetFilePath(pageInfo, track, path);
                    if (fileSystem.IsDownloadedTrack(dest, track))
                    {
                        OnDownloadProgress(this, new TrackDownloadProgressEventArgs(track, track.FileSize));
                    }
                    else
                    {
                        String url = apiClient.GetTrackUrl(track.Id);
                        fileSystem.CreateDirectoryForFilePath(dest);
                        fileDownloader.Download(url, dest, (o, e) =>
                        {
                            if (!CallHandlerOnlyOnFinish || e.BytesDownloaded >= track.FileSize)
                            {
                                OnDownloadProgress(this, new TrackDownloadProgressEventArgs(track, e.BytesDownloaded));
                            }
                        });

                        AddTagsToFile(dest, track);
                    }
                }
            }
        }
Beispiel #3
0
        private String GetFilePath(PageInfo pageInfo, TrackDto track, String path)
        {
            StringBuilder sb = new StringBuilder(128);

            sb.Append(path);
            sb.Append(Path.DirectorySeparatorChar);
            if (pageInfo.Type == PageType.Artist)
            {
                sb.Append(track.AlbumYear);
                sb.Append(" - ");
                sb.Append(fileSystem.ReplaceIllegalChars(track.AlbumTitle));
                sb.Append(Path.DirectorySeparatorChar);
            }

            if (pageInfo.Type == PageType.Artist || pageInfo.Type == PageType.Album)
            {
                if (track.Position != null)
                {
                    if (track.Position.Volume != 0)
                    {
                        sb.Append(track.Position.Volume);
                    }
                    sb.Append(String.Format("{0:D2}", track.Position.Index));
                    sb.Append(" - ");
                }
                sb.Append(fileSystem.ReplaceIllegalChars(track.Title));
            }
            else
            {
                sb.Append(fileSystem.ReplaceIllegalChars(track.Artist));
                sb.Append(" - ");
                sb.Append(fileSystem.ReplaceIllegalChars(track.Title));
            }

            sb.Append(".mp3");

            return(sb.ToString());
        }
Beispiel #4
0
 public TrackDownloadProgressEventArgs(TrackDto track, long bytesDownloaded)
 {
     Track           = track;
     BytesDownloaded = bytesDownloaded;
 }
Beispiel #5
0
        public bool IsDownloadedTrack(string path, TrackDto track)
        {
            FileInfo file = new FileInfo(path);

            return(file.Exists && file.Length >= track.FileSize);
        }