Beispiel #1
0
 public void Add(Song song, int maxumumDistance)
 {
     if (song == null)
     {
         throw new ArgumentNullException("song");
     }
     var name = song.Name.ToList();
     if (_songs.All(x => MathUtils.LevenshteinDistance(x.Name.ToList(), name) >= maxumumDistance))
     {
         _songs.Add(song);
         song.OwnGroup = this;
     }
 }
Beispiel #2
0
        private void DownloadSongCore(Song song)
        {
            _semaphore.WaitOne();
            if(_operationCanceled)
            {
                _semaphore.Release();
                return;
            }

            var wc = new WebClient();
            wc.DownloadProgressChanged += OnDownloadProgressChanged;
            wc.DownloadFileCompleted += OnDownloadFileCompleted;
            var fullPath = GetSongFullPath(song);
            if (!File.Exists(fullPath))
            {
                var tempPath = GetSongTempPath(song);
                try
                {
                    if (File.Exists(tempPath))
                    {
                        File.Delete(tempPath);
                    }
                }
                catch (IOException ex)
                {
                    Trace.TraceError("{0}\n\n{1}", ex.Message, ex.StackTrace);
                }

                Monitor.Enter(this);
                _webClients.Add(wc);
                Monitor.Exit(this);
                wc.DownloadFileAsync(new Uri(song.Url), tempPath, song);
            }
            else
            {
                OnDownloadComplete(song);
            }
        }
Beispiel #3
0
        private void ProcessSongNode(HtmlNode node)
        {
            var boldGroupNameNode = node.SelectSingleNode(".//b");
            if(boldGroupNameNode == null)
                return;
            var groupName = DeleteSpecialSymbols(boldGroupNameNode.InnerText);
            if (string.IsNullOrEmpty(groupName))
            {
                return;
            }

            var chars = groupName.ToList();
            var group = _groupList.FirstOrDefault(
                x => MathUtils.LevenshteinDistance(x.Name.ToList(), chars) < Settings.Default.Storage.MaximumLevenshteinDistance);
            if (group == null)
            {
                group = new Group(groupName);
                _groupList.Add(group);
            }

            var title = DeleteSpecialSymbols(node.SelectSingleNode(".//span").InnerText);
            if (string.IsNullOrEmpty(title))
            {
                return;
            }
            Match match;

            var image = node.SelectSingleNode(".//img[@class='playimg']");
            if (image != null)
            {
                var onclick = image.GetAttributeValue("onclick", string.Empty).Replace("&#39;", "'");
                match = OperateRegexp.Match(onclick);
                if (match.Success)
                {
                    var song = new Song(title, match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value);
                    group.Add(song, Settings.Default.Storage.MaximumLevenshteinDistance);
                    return;
                }

                match = OperateWithLinkRegexp.Match(onclick);
                if (match.Success)
                {
                    var song = new Song(title, match.Groups[1].Value);
                    group.Add(song, Settings.Default.Storage.MaximumLevenshteinDistance);
                    return;
                }

                match = OperateWallWithLinkRegexp.Match(onclick);
                if (match.Success)
                {
                    var song = new Song(title, match.Groups[1].Value);
                    group.Add(song, Settings.Default.Storage.MaximumLevenshteinDistance);
                    return;
                }
            }

            var hiddenField = node.SelectSingleNode("./..//input[@type='hidden']");
            if(hiddenField != null)
            {
                var value = hiddenField.GetAttributeValue("value", string.Empty);
                match = HiddenFieldWithPath.Match(value);
                if(match.Success)
                {
                    var song = new Song(title, match.Groups[1].Value);
                    group.Add(song, Settings.Default.Storage.MaximumLevenshteinDistance);
                    return;
                }
            }
        }
Beispiel #4
0
        private void DownloadFileExcaptionHandler(Song song, Exception error)
        {
            var webException = error as WebException;
            if (webException != null)
            {
                switch (webException.Status)
                {
                    case WebExceptionStatus.ProtocolError:
                        var response = ((HttpWebResponse)webException.Response);
                        if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => DeleteSongWithMessageBox(song)));
                        }
                        break;
                    case WebExceptionStatus.UnknownError:

                        //This happends when the elthernet connection becomes unplugged
                        Dispatcher.Invoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate
                        {
                            if (CancelDownloadCommand.CanExecute(null, this))
                            {
                                CancelDownloadCommand.Execute(null, this);
                            }
                            return null;
                        }), null);

                        break;
                    case WebExceptionStatus.Timeout:
                        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => DeleteSongWithMessageBox(song)));
                        break;
                }
            }
        }
Beispiel #5
0
 private void DeleteSongWithMessageBox(Song song)
 {
     DeleteSong(song);
     if (SongList.Count == 0)
     {
         MessageBox.Show(this, ResourceStrings.MainWindowCompleteContent, ResourceStrings.MainWindowCompleteTitle,
             MessageBoxButton.OK);
         IsNotInDownloadState = true;
     }
 }
Beispiel #6
0
 private void DeleteSong(Song selected)
 {
     foreach (var group in _groupList)
     {
         group.Remove(selected);
     }
     SongList.Remove(selected);
     _songsListBox.SelectedIndex = _songsListBox.SelectedIndex;
 }
Beispiel #7
0
 private string CreateSongPath(Song song)
 {
     var lastPath = Settings.Default.Storage.DownloadFormat.Replace("%a", song.OwnGroup.Name).Replace("%t", song.Name);
     return Path.Combine(_outputPath, lastPath);
 }
Beispiel #8
0
 public void DownloadSong(Song song)
 {
     new Thread(() => DownloadSongCore(song)).Start();
 }
Beispiel #9
0
 private void OnDownloadComplete(Song song)
 {
     _semaphore.Release();
     if (FileDownloaded != null)
     {
         FileDownloaded(song);
     }
 }
Beispiel #10
0
 private string GetSongTempPath(Song song)
 {
     return GetSongFullPath(song) + ".temp";
 }
Beispiel #11
0
 private string GetSongFullPath(Song song)
 {
     var path = CreateSongPath(song);
     var folder = Path.GetDirectoryName(path);
     if (!Directory.Exists(folder))
     {
         Directory.CreateDirectory(folder);
     }
     return path;
 }
Beispiel #12
0
 public void Remove(Song song)
 {
     _songs.Remove(song);
 }