コード例 #1
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
 private void SongListView_DragDrop(object sender, DragEventArgs e)
 {
     try
     {
         if (e.Data.GetDataPresent(DataFormats.StringFormat))
         {
             _current++;
             songArray[_current] = new PassArguments[10000];
             string data = (string)e.Data.GetData(DataFormats.StringFormat);
             data = data.Replace("http://open.spotify.com/track/", "");
             string[] strArrays = data.Split(new char[] { '\n' });
             _songs = _songs + strArrays.Length;
             songsArray[_current] = strArrays.Length;
             Log("Loading " + strArrays.Length + " songs...");
             foreach (string str in strArrays)
             {
                 if (str.Length > 1)
                 {
                     BackgroundWorker backgroundWorkerStart = new BackgroundWorker();
                     backgroundWorkerStart.DoWork += BackgroundWorkerStart_DoWork;
                     backgroundWorkerStart.RunWorkerAsync(new PassArguments
                     {
                         PassedSpotCode = str,
                         PassedSession  = _current
                     });
                 }
             }
         }
     }
     catch (Exception ex) { MessageBox.Show(this, ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); }
 }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        void YouTubeDownload(int num, int session)
        {
            PassArguments result = songArray[session][num];

//            while (true)
//            {
            try
            {
                List <YouTubeVideoQuality> urls        = result.YouTubeVideoQuality;
                YouTubeVideoQuality        highestQual = new YouTubeVideoQuality();

                foreach (var url in urls)
                {
                    if (url.Extention == "mp4")
                    {
                        highestQual = urls[0];
                        break;
                    }
                }
                string Url    = "";
                string saveTo = "";
                try
                {
                    YouTubeVideoQuality tempItem = highestQual;
                    Url    = tempItem.DownloadUrl;
                    saveTo = EscapeFilename(result.PassedFileName) + ".mp4";
                }
                catch (Exception ex)
                {
                    Log("[Error x11] " + ex.InnerException, true);
                }
                if (result.PassedFileName == null || result.PassedNum == null)
                {
                    MessageBox.Show("Somthing null");
                }
                EditList("Downloading...", result.PassedFileName, result.PassedNum, 2);
                var    folder = Path.GetDirectoryName(_tempDir + saveTo);
                string file   = Path.GetFileName(_tempDir + saveTo);

                var client  = new WebClient();
                Uri address = new Uri(Url);
                client.DownloadFile(address, folder + "\\" + file);
                EditList("Converting...", result.PassedFileName, result.PassedNum, 3);
                StartConvert(result.PassedFileName);
                MusicTags(num, session);
                _youtubeDownloadedNum++;
                Done(result.PassedFileName, num);
                _running--;
                if (_running < 0)
                {
                    _running = 0;
                }
//                    break;
            }
            catch (Exception ex)
            {
                Log("[Error x12] " + "|" + ex.Message + "| " + ex.InnerException + " | " + result.PassedFileName, true);
            }
//            }
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        private void BackgroundWorkerStart_DoWork(object sender, DoWorkEventArgs e)
        {
            PassArguments result = (PassArguments)e.Argument;

            e.Result = result;
            newProgressBar1.CustomText = "Loading...";
            SearchSpotify(result.PassedSpotCode, result.PassedSession);
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        public PassArguments GetSpotifyName(string query)//Uptaded to use new API
        {
            string getData = null;

            while (true)
            {
                try
                {
                    WebClient c = new WebClient();
                    getData = c.DownloadString("https://api.spotify.com/v1/tracks/" + query);
                    break;
                }
                catch (Exception)
                {
                    Thread.Sleep(500);
                }
            }

            JObject o        = JObject.Parse(getData);
            string  name     = o["name"].ToString();
            string  artist   = o["artists"][0]["name"].ToString();
            string  albumId  = o["artists"][0]["id"].ToString();
            string  album    = o["album"]["name"].ToString();
            string  imageURL = o["album"]["images"][0]["url"].ToString();
            double  lengthMs = double.Parse(o["duration_ms"].ToString());
            double  length   = MillisecondsTimeSpanToHms(double.Parse(o["duration_ms"].ToString()));

            name   = Encoding.UTF8.GetString(Encoding.Default.GetBytes(name));
            artist = Encoding.UTF8.GetString(Encoding.Default.GetBytes(artist));
            album  = Encoding.UTF8.GetString(Encoding.Default.GetBytes(album));

            PassArguments pass = new PassArguments
            {
                PassedSong     = name,
                PassedArtist   = artist,
                PassedAlbum    = album,
                PassedAlbumId  = albumId,
                PassedFileName = name + " - " + artist,
                PassedLength   = length,
                PassedLengthMs = lengthMs,
                PassedimageURL = imageURL
            };

            return(pass);
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        Bitmap GetAlbumArt(int num, int session)
        {
            PassArguments result = songArray[session][num];
            Bitmap        bitmap2;

            try
            {
                WebRequest  request        = WebRequest.Create(result.PassedimageURL);
                WebResponse response       = request.GetResponse();
                Stream      responseStream = response.GetResponseStream();
                bitmap2 = new Bitmap(responseStream);
            }
            catch (Exception ex)
            {
                Log("[Error: x8] " + ex.Message, true);
                bitmap2 = null;
            }

            return(bitmap2);
        }
コード例 #6
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        public void MusicTags(int num, int session)
        {
            PassArguments result = songArray[session][num];

            try
            {
                //===edit tags====
                TagLib.File f = TagLib.File.Create(_dir + EscapeFilename(result.PassedFileName) + ".mp3");
                f.Tag.Clear();
                f.Tag.AlbumArtists = new string[1] {
                    result.PassedArtist
                };
                f.Tag.Performers = new string[1] {
                    result.PassedArtist
                };
                f.Tag.Title = result.PassedSong;
                f.Tag.Album = result.PassedAlbum;
//                //                Log(result.passedFileName + " and " + result.passedAlbumID);
                Image   currentImage = GetAlbumArt(num, session);
                Picture pic          = new Picture();
                pic.Type        = PictureType.FrontCover;
                pic.MimeType    = System.Net.Mime.MediaTypeNames.Image.Jpeg;
                pic.Description = "Cover";
                MemoryStream ms = new MemoryStream();
                currentImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // <-- Error doesn't occur anymore
                ms.Position    = 0;
                pic.Data       = ByteVector.FromStream(ms);
                f.Tag.Pictures = new IPicture[1] {
                    pic
                };
                f.Save();
                ms.Close();
            }
            catch (Exception ex)
            {
                Log("[Error: x7] " + ex.Message + Environment.NewLine + Environment.NewLine + result.PassedFileName, true);
            }
        }
コード例 #7
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        private void StartDownloadMp3Clan(int num, int session)
        {
            PassArguments result = songArray[session][num];

            while (_running >= maxRunning)
            {
                Thread.Sleep(500);
            }
            _totalQuedNum--;
            _running++;
            _mp3ClanDownloadedNum++;

            EditList("Downloading...", result.PassedFileName, result.PassedNum, 2);

            Uri downloadUrl  = null;
            int errorTimesX6 = 0;

            while (true)
            {
                try
                {
                    downloadUrl = new Uri(result.PassedTrack.Mp3ClanUrl);
                    var fileName = _dir + "\\" + EscapeFilename(result.PassedFileName) + ".mp3";

                    int errorTimes = 0;
                    while (true)
                    {
                        using (var client = new WebClient())
                        {
                            client.DownloadFile(downloadUrl, fileName);
                            client.Dispose();
                        }

                        long fileSize = new FileInfo(fileName).Length;
                        if (fileSize < 1000)    //Possible improvement. get file size from online fore download and check that its a 5% acc to approve the download
                        {
                            errorTimes++;
                            if (errorTimes >= 3)
                            {
                                Log("[Infomation: x5] " + result.PassedFileName + " failed, re-downloading");
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    break;
                }
                catch (Exception ex)
                {
                    errorTimesX6++;
                    if (errorTimesX6 >= 3)
                    {
                        Log("[Infomation: x6] " + ex.Message + " | " + ex.InnerException + " | " + result.PassedFileName);
                        errorTimesX6 = 0;
                        Thread.Sleep(500);
                    }
                }
            }
            MusicTags(num, session);
            Done(result.PassedFileName, result.PassedNum);
            _running--;
            if (_running < 0)
            {
                _running = 0;
            }
        }
コード例 #8
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        public async void Search(int num, int session, int retry = 0, string videoID = null)
        {
            PassArguments result = songArray[session][num];

            while (_running >= maxRunning)
            {
                Thread.Sleep(500);
            }
            _totalQuedNum--;
            _running++;
            EditList("Loading...", result.PassedFileName, result.PassedNum, 0);

            string url            = "";
            var    youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey          = ApiKey,
                ApplicationName = this.GetType().ToString()
            });

            var searchListRequest = youtubeService.Search.List("snippet");

            if (retry == 0)
            {
                searchListRequest.Q = result.PassedFileName;
            }
            else if (retry == 1)
            {
                string newName = null;
                try
                {
                    newName = result.PassedSong.Substring(0, result.PassedSong.IndexOf("-")) + " - " + result.PassedArtist;
                }
                catch (Exception ex)
                {
                    newName = result.PassedFileName;
                }
                searchListRequest.Q = newName;
            }
            else if (retry == 2)
            {
                string newName = null;
                try
                {
                    newName = result.PassedSong.Substring(0, result.PassedSong.IndexOf("(")) + " - " + result.PassedArtist;
                }
                catch (Exception ex)
                {
                    newName = result.PassedFileName;
                }
                searchListRequest.Q = newName;
            }
            else if (retry == 3)
            {
                string newName = null;
                try
                {
                    newName = result.PassedSong.Substring(0, result.PassedSong.IndexOf("/")) + " - " + result.PassedArtist;
                }
                catch (Exception ex)
                {
                    newName = result.PassedFileName;
                }
                searchListRequest.Q = newName;
            }
            else if (retry == 4)
            {
                searchListRequest.Q = result.PassedFileName;
            }
            searchListRequest.MaxResults = 5;
            searchListRequest.Order      = SearchResource.ListRequest.OrderEnum.Relevance;

            var searchListResponse = await searchListRequest.ExecuteAsync();

            List <string> videos = new List <string>();

            string[] excludeStrings        = { "live", "cover" };
            string[] includeStrings        = { "hd", "official" };
            string[] includeChannelStrings = { "vevo" };
            double   highpersentage        = 99999999.0;

            for (int i = 0; i < excludeStrings.Length; i++)
            {
                if (result.PassedFileName.ToLower().Contains(excludeStrings[i]))
                {
                    excludeStrings = excludeStrings.Where(w => w != excludeStrings[i]).ToArray();
                }
            }

            foreach (var word in excludeStrings)
            {
                if ((result.PassedFileName).ToLower().Contains(word))
                {
                    excludeStrings = excludeStrings.Where(str => str != word).ToArray();
                }
            }

            bool keepgoing = true;

            foreach (var searchResult in searchListResponse.Items)
            {
                if (keepgoing)
                {
                    if (searchResult.Id.VideoId != null && "https://www.youtube.com/watch?v=" + searchResult.Id.VideoId != videoID)
                    {
                        if (retry == 4)
                        {
                            Log("[Infomation x13] Downloaded song may be incorrect for " + result.PassedFileName);
                            videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                            url       = "https://www.youtube.com/watch?v=" + searchResult.Id.VideoId;
                            keepgoing = false;
                            break;
                        }
                        if (excludeStrings.Any(searchResult.Snippet.Title.ToLower().Contains) ||
                            excludeStrings.Any(searchResult.Snippet.Description.ToLower().Contains))
                        {
//                        MessageBox.Show("ERROR IT CONTAINS BAD STUFF");
                        }
                        else
                        {
                            var searchListRequest2 = youtubeService.Videos.List("contentDetails");
                            searchListRequest2.Id = searchResult.Id.VideoId;
                            var searchListResponse2 = await searchListRequest2.ExecuteAsync();

                            foreach (var searchResult2 in searchListResponse2.Items)
                            {
                                string   durationTimeSpan = searchResult2.ContentDetails.Duration;
                                TimeSpan youTubeDuration  = XmlConvert.ToTimeSpan(durationTimeSpan);
                                double   durationMs       = (youTubeDuration).TotalMilliseconds;
                                double   persentage       = (durationMs / result.PassedLengthMs) * 100;

                                if (persentage >= 90 && persentage <= 110)
                                {
                                    double number = Math.Abs(durationMs - result.PassedLengthMs);
                                    if (number < highpersentage)
                                    {
//                                        Log(string.Format("{0:0.00}", persentage) + "% from the original and number is " + number + " | " + searchResult.Id.VideoId);
                                        videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                                        url            = "https://www.youtube.com/watch?v=" + searchResult.Id.VideoId;
                                        highpersentage = number;
                                    }

                                    if (includeChannelStrings.Any(searchResult.Snippet.ChannelTitle.ToLower().Contains) || searchResult.Snippet.ChannelTitle.ToLower().Contains(result.PassedArtist.Replace(" ", "").ToLower()))
                                    {
//                                        Log("using Official | " + searchResult.Id.VideoId);
                                        videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                                        url       = "https://www.youtube.com/watch?v=" + searchResult.Id.VideoId;
                                        keepgoing = false;
                                        break;
                                    }

                                    if (includeStrings.Any(searchResult.Snippet.Description.ToLower().Contains) || includeStrings.Any(searchResult.Snippet.Title.ToLower().Contains))
                                    {
//                                        Log("using Original " + string.Format("{0:0.00}", persentage) + "% from the original| " + searchResult.Id.VideoId);
                                        videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId));
                                        url       = "https://www.youtube.com/watch?v=" + searchResult.Id.VideoId;
                                        keepgoing = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (url != "")
            {
                songArray[session][num].PassedURL = url;
            }
            else
            {
                if (retry == 0)
                {
                    _running--;
                    _totalQuedNum++;
                    if (_running < 0)
                    {
                        _running = 0;
                    }
                    Search(num, session, 1);
                    return;
                }
                if (retry == 1)
                {
                    _running--;
                    _totalQuedNum++;
                    if (_running < 0)
                    {
                        _running = 0;
                    }
                    Search(num, session, 2);
                    return;
                }
                if (retry == 2)
                {
                    _running--;
                    _totalQuedNum++;
                    if (_running < 0)
                    {
                        _running = 0;
                    }
                    Search(num, session, 3);
                    return;
                }
                if (retry == 3)
                {
                    _running--;
                    _totalQuedNum++;
                    if (_running < 0)
                    {
                        _running = 0;
                    }
                    Search(num, session, 4);
                    return;
                }
                if (retry == 4)
                {
                    Done(result.PassedFileName, result.PassedNum, "NotFound", 5);//Youtube not found
                    Log("[Error x9] Video not found for: " + result.PassedFileName, true);
                    _running--;
                    if (_running < 0)
                    {
                        _running = 0;
                    }
                    return;
                }
                Log("[Error x10] " + result.PassedFileName, true);
                _running--;
                _totalQuedNum++;
                if (_running < 0)
                {
                    _running = 0;
                }
                return;
            }

            songArray[session][num].YouTubeVideoQuality = youTubeDownloader.GetYouTubeVideoUrls(result.PassedURL);
            if (songArray[session][num].YouTubeVideoQuality == null)
            {
//                Log("Cant download " + result.passedFileName + " because of age restriction on video");
                _running--;
                if (_running < 0)
                {
                    _running = 0;
                }
                _totalQuedNum++;
                Search(num, session, 0, url);
                return;
            }
            YouTubeDownload(num, session);
        }
コード例 #9
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        public void DownloadMP3Clan(int num, int session)
        {
            PassArguments result = songArray[session][num];

            EditList("Loading...", result.PassedFileName, result.PassedNum, 0);

            int                 highestBitrateNum   = 0;
            Mp3ClanTrack        highestBitrateTrack = null;
            List <Mp3ClanTrack> tracks = null;

            const string searchBaseUrl = "http://mp3clan.com/mp3_source.php?q={0}";
            var          searchUrl     = new Uri(string.Format(searchBaseUrl, Uri.EscapeDataString(result.PassedSong)));
            string       pageSource    = null;

            while (true)
            {
                try
                {
                    pageSource = new MyWebClient().DownloadString(searchUrl);
                    break;
                }
                catch (WebException e)
                {
                    if (e.Status == WebExceptionStatus.ProtocolError)
                    {
                        EditList("Queued", result.PassedFileName, result.PassedNum, 1); //In Que so when its less than 5 it will start
                        _youtubeNum++;
                        _totalQuedNum++;
                        var th = new Thread(unused => Search(num, session));
                        th.Start();
                        return;
                    }
                    Log("[Error: x3] " + e.Message + " | " + result.PassedFileName, true);
                }
            }


            IEnumerable <Mp3ClanTrack> trackResult;

            if (Mp3ClanTrack.TryParseFromSource(pageSource, out trackResult))
            {
                tracks = trackResult.ToList();
                foreach (var track in tracks)
                {
                    if (track.Artist.ToLower().Trim().Contains(result.PassedArtist.ToLower()) &&
                        track.Name.ToLower().Trim().Equals(result.PassedSong.ToLower()))
                    {
                        string bitrateString = null;
                        int    attempts      = 0;
                        while (true)
                        {
                            try
                            {
                                bitrateString = new MyWebClient().DownloadString("http://mp3clan.com/bitrate.php?tid=" + track.Mp3ClanUrl.Replace("http://mp3clan.com/app/get.php?mp3=", ""));
                                break;
                            }
                            catch (Exception ex)
                            {
                                attempts++;
                                if (attempts > 2)
                                {
                                    Log("[Infomation: x4] " + result.PassedFileName + " " + ex.Message);
                                    bitrateString = "0 kbps";
                                    break;
                                }
                            }
                        }

                        int bitrate = Int32.Parse(GetKbps(bitrateString));
                        if (bitrate >= 192)
                        {
                            if (bitrate > highestBitrateNum)
                            {
                                double persentage = (GetLength(bitrateString) / result.PassedLength) * 100;
//                                double durationMS = TimeSpan.FromMinutes(getLength(bitrateString)).TotalMilliseconds;
//                                double persentage = (durationMS/result.passedLengthMS)*100;
                                if (persentage >= 85 && persentage <= 115)
                                {
//                                    Log("Length acc: " + string.Format("{0:0.00}", persentage) + "%");
                                    highestBitrateNum   = bitrate;
                                    highestBitrateTrack = track;
                                }
                            }
                        }
                    }
                }
            }
            //=======For testing================
//            EditList("Queued", result.passedFileName, result.passedNum, 1);
//            youtubeNum++;
//            totalQuedNum++;
//            var th = new Thread(unused => Search(num));
//            th.Start();
            //==================================

            if (highestBitrateTrack == null)
            {//Youtube
                EditList("Queued", result.PassedFileName, result.PassedNum, 1);
                _youtubeNum++;
                _totalQuedNum++;
                var th = new Thread(unused => Search(num, session));
                th.Start();
            }
            else
            {//MP3Clan
                songArray[session][num].PassedTrack = highestBitrateTrack;
                EditList("Queued", result.PassedFileName, result.PassedNum, 1);
                _mp3ClanNum++;
                _totalQuedNum++;

                var th = new Thread(unused => StartDownloadMp3Clan(num, session));
                th.Start();
            }
        }
コード例 #10
0
ファイル: MainForm.cs プロジェクト: cilles/SpotDown
        private void SearchSpotify(string code, int session)
        {
            int           num         = 0;
            PassArguments spotifyName = GetSpotifyName(code);
            bool          add         = true;

            foreach (PassArguments[] songArrayInArray in songArray)
            {
                if (songArrayInArray != null)
                {
                    foreach (var songThing in songArrayInArray)
                    {
                        if (songThing != null)
                        {
                            if (songThing.PassedFileName.Equals(spotifyName.PassedSong + " - " + spotifyName.PassedArtist))
                            {
                                //File already in list
                                _songs--;
                                songsArray[_current]--;
                                add = false;
                                Log("[Attention] The song " + spotifyName.PassedSong + " - " + spotifyName.PassedArtist +
                                    " was already added.");
                            }
                        }
                    }
                }
            }
            if (File.Exists(_dir + EscapeFilename(spotifyName.PassedFileName) + ".mp3"))
            {
                //File already exsists/Downloaded
                _songs--;
                songsArray[_current]--;
                add = false;
            }

            try
            {
                if (add)
                {
                    {
                        SongListView.BeginUpdate();
                        string[] row          = { "Waiting", spotifyName.PassedSong + " - " + spotifyName.PassedArtist };
                        var      listViewItem = new ListViewItem(row);
                        listViewItem.ImageIndex = 1;
                        SongListView.Items.Add(listViewItem);
                        SetLabelVisible(false);
                        num = listViewItem.Index;
                        SongListView.EndUpdate();

                        songArray[session][num] = (new PassArguments
                        {
                            PassedSong = spotifyName.PassedSong,
                            PassedArtist = spotifyName.PassedArtist,
                            PassedNum = num,
                            PassedFileName = spotifyName.PassedSong + " - " + spotifyName.PassedArtist,
                            PassedAlbum = spotifyName.PassedAlbum,
                            PassedAlbumId = spotifyName.PassedAlbumId,
                            PassedLength = spotifyName.PassedLength,
                            PassedLengthMs = spotifyName.PassedLengthMs,
                            PassedimageURL = spotifyName.PassedimageURL
                        });
                    }
                }

//                if (SongListView.Items.Count == songs)
                int result = songArray[session].Count(s => s != null);
//                Log(result + " | " + songsArray[current]);
                if (result == songsArray[_current])
                {
                    Log(songsArray[_current] + " songs added. Total songs: " + _songs);
                    SearchSongArray(session);
                }
            }
            catch (Exception ex)
            {
                Log("[Error: x1] " + ex.Message + Environment.NewLine + num, true);
            }
        }