コード例 #1
0
ファイル: Utils.cs プロジェクト: zackmark29/Qabuze
        internal static string escapeMetaString(string p, QabuzeAlbum album, QabuzeSong song)
        {
            p = escapeAlbumString(@p, album);
            p = escapeSongString(@p, song);

            //remove illegal characters:
            p = Regex.Replace(@p, "/", "", RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, ":", "", RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "'", "", RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "<", "", RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, ">", "", RegexOptions.IgnoreCase);
            p = @p.Replace("*", "");
            p = @p.Replace("|", "");
            p = @p.Replace("?", "");
            p = @p.Replace("[", "");
            p = @p.Replace("]", "");
            p = @p.Replace("\"", "");

            if ((QabuzeAPI.Config.getInstance().outputFolder.Length + p.Length) >= 245)
            {
                p = p.Substring(0, (245 - QabuzeAPI.Config.getInstance().outputFolder.Length)) + ".flac";
            }

            return(p);
        }
コード例 #2
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            album = QabuzeAlbum.queryAlbum(txtAlbumId.Text);
            if (album != null)
            {
                TimeSpan t        = TimeSpan.FromSeconds((int)album.duration);
                string   duration = string.Format("{0:D2}:{1:D2}:{2:D2}",
                                                  t.Hours,
                                                  t.Minutes,
                                                  t.Seconds);

                pbArtwork.ImageLocation = album.coverURL;
                lblArtist.Text          = album.artist;
                lblAlbum.Text           = album.title;
                lblGenre.Text           = album.genre;
                lblLabel.Text           = album.label;
                lblDuration.Text        = duration;
                lblEstSize.Text         = "< " + (((t.Hours * 60) + t.Minutes) * 11) + " MiB *"; //Estimate uses 16 bit 44.1 KHz PCM. FLAC size WILL be lower!
                if (album.release_timestamp == null)
                {
                    lblRelease.Text = "no date";
                }
                else
                {
                    lblRelease.Text = Utils.UnixTimeStampToDateTime((int)album.release_timestamp).ToShortDateString();
                }
                lblTracks.Text       = album.track_count.ToString();
                lblAvailability.Text = (album.availableForStreaming ? "Yes (until " : "No (available ");
                string   until = album.availableUntil.ToShortDateString(), from = album.availableFrom.ToShortDateString();
                DateTime dateToDisplay = new DateTime(9999, 12, 30, 0, 0, 0); //Use this to accomodate for Localization-issues regarding date-format
                until = until.Replace(dateToDisplay.ToShortDateString(), "forever");
                from  = from.Replace(dateToDisplay.ToShortDateString(), "at no time");
                lblAvailability.Text     += (album.availableForStreaming ? until : from) + ")";
                lblAvailability.ForeColor = (album.availableForStreaming ? Color.DarkGreen : Color.DarkRed);
                #if (WITHDOWNLOAD)
                button2.Enabled = album.availableForStreaming;
                #endif

                artist = QabuzeArtist.queryArtist(album.artistID.ToString());
                if (artist != null)
                {
                    txtArtist_Bio.Text     = artist.biography;
                    pbArtist.ImageLocation = artist.imageURL;
                    Console.WriteLine(artist.biography);
                }

                //Console.WriteLine(album.songs[1].Value.getDownloadLink(true));
            }
        }
コード例 #3
0
ファイル: Qabuze.cs プロジェクト: zackmark29/Qabuze
        public static QabuzeAlbum QueryAlbumById(string id)
        {
            List <KeyValuePair <string, string> > data = new List <KeyValuePair <string, string> >();

            data.Add(new KeyValuePair <string, string>("album_id", id));
            for (int credIndex = 0; credIndex < QabuzeAPI.Config.getInstance().accounts.Count; credIndex++)
            {
                QabuzeAlbum tmp = (QabuzeAlbum)QabuzeAPI.PerformRequest(QabuzeAPI.instance.BuildRequest("album/get", data, false, credIndex));
                if (tmp != null)
                {
                    return(tmp);
                }
            }
            return(null);
        }
コード例 #4
0
ファイル: Utils.cs プロジェクト: zackmark29/Qabuze
        internal static string escapeAlbumString(string p, QabuzeAlbum album)
        {
            p = Regex.Replace(@p, "%ALBUMARTIST%", album.artist, RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "%GENRE%", album.genre, RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "%ORGANIZATION%", album.label, RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "%ALBUM%", album.title, RegexOptions.IgnoreCase);
            p = Regex.Replace(@p, "%TRACKTOTAL%", album.track_count.ToString(), RegexOptions.IgnoreCase);
            if (album.release_timestamp == null)
            {
                p = Regex.Replace(@p, "%RELEASE%", "no date", RegexOptions.IgnoreCase);
            }
            else
            {
                p = Regex.Replace(@p, "%RELEASE%", UnixTimeStampToDateTime((int)album.release_timestamp).ToShortDateString(), RegexOptions.IgnoreCase);
            }

            return(p);
        }
コード例 #5
0
ファイル: QabuzeDLM.cs プロジェクト: zackmark29/Qabuze
 public static void DownloadAsync(QabuzeAlbum album)
 {
     ThreadPool.QueueUserWorkItem(o => DownloadAsyncWorker(album));
 }
コード例 #6
0
ファイル: QabuzeDLM.cs プロジェクト: zackmark29/Qabuze
        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
        }
コード例 #7
0
ファイル: Qabuze.cs プロジェクト: zackmark29/Qabuze
        public static Object PerformRequest(string url)
        {
            string response;

            try
            {
                response = (new WebClient()).DownloadString(url);
            }
            catch (WebException e) {
                HttpWebResponse _response = (System.Net.HttpWebResponse)e.Response;
                Console.WriteLine("URL \"" + url + "\" has come back with the following error: " + _response.StatusCode);
                return(null);
            }
            JObject obj = JObject.Parse(response);

            #region Album
            if (obj["tracks_count"] != null)
            {
                QabuzeAlbum objReturn;
                Console.WriteLine("Is Album");
                objReturn = new QabuzeAlbum();
                //QabuzeAlbum objReturn = new QabuzeAlbum();
                objReturn.title             = (string)obj["title"];
                objReturn.id                = (string)obj["id"];
                objReturn.genre             = (string)obj["genre"]["name"];
                objReturn.artist            = (string)obj["artist"]["name"];
                objReturn.artistID          = (int)obj["artist"]["id"];
                objReturn.description       = (string)obj["description"];
                objReturn.coverURL          = (string)obj["image"]["large"];
                objReturn.label             = (string)obj["label"]["name"];
                objReturn.track_count       = (int)obj["tracks_count"];
                objReturn.duration          = (int?)obj["duration"];
                objReturn.release_timestamp = (int?)obj["released_at"];
                int amt_tracks = (int)obj["tracks"]["total"];

                for (int i = 0; i < amt_tracks; i++)
                {
                    JObject track = (JObject)obj["tracks"]["items"][i];
                    try {
                        Console.WriteLine("Adding Track #" + i + " with ID " + track["id"] + "...");
                        QabuzeSong song = new QabuzeSong(
                            (string)track["id"],
                            (string)track["title"],
                            (string)track["performer"]["name"],
                            (string)obj["id"],
                            (int)track["duration"],
                            (int)track["track_number"],
                            (int)track["media_number"],
                            (string)obj["title"],
                            (string)obj["genre"]["name"]
                            );
                        KeyValuePair <int, QabuzeSong> kvp = new KeyValuePair <int, QabuzeSong>(i, song);
                        objReturn.songs.Add(kvp);
                    } catch (Exception e) {
                        Console.Error.WriteLine("Whoops. Track #" + i + " with ID " + track["id"] + " could not be added");
                    }
                }

                string start_date = "9999-12-31", end_date = "9999-12-31";

                /*  try
                 * {
                 *        start_date = (string)obj["rights"]["stream"][0]["start_date"];
                 *
                 * }
                 * catch (Exception)
                 * {*/
                try
                {
                    start_date = Utils.UnixTimeStampToDateTime((int)obj["streamable_at"]).ToString();
                }
                catch (Exception) { }

                /* }
                 * try
                 * {
                 *       end_date = (string)obj["rights"]["stream"][0]["end_date"];
                 *
                 * }
                 * catch (Exception) { }
                 */
                DateTime d_start = DateTime.Parse(start_date).ToUniversalTime();
                DateTime d_end   = DateTime.Parse(end_date).ToUniversalTime();
                DateTime d_curr  = DateTime.UtcNow;

                bool c_start = d_start > d_curr;
                bool c_end   = d_end > d_curr;

                if (c_start)
                {
                    objReturn.availableForStreaming = false;
                }
                else
                {
                    objReturn.availableForStreaming = true;
                }

                if (!c_end)
                {
                    objReturn.availableForStreaming = false;
                }

                Console.WriteLine("May stream from " + start_date + " until " + end_date);
                objReturn.availableFrom  = d_start;
                objReturn.availableUntil = d_end;

                return(objReturn);

                #endregion
                #region Album from Search
            }
            else if (obj["albums"] != null)
            {
                Console.WriteLine("Is Album Search");
                List <QabuzeAlbum> objReturn = new List <QabuzeAlbum>();
                //QabuzeAlbum objReturn = new QabuzeAlbum();
                int limit      = (int)obj["albums"]["limit"];
                int offset     = (int)obj["albums"]["offset"];
                int total      = (int)obj["albums"]["total"];
                int upperLimit = ((offset + limit) > total) ? total : (offset + limit);

                for (int i = 0; i < upperLimit - offset; i++)
                {
                    JObject jalbum = (JObject)obj["albums"]["items"][i];

                    QabuzeAlbum album = new QabuzeAlbum();
                    album.title       = (string)jalbum["title"];
                    album.id          = (string)jalbum["id"];
                    album.genre       = (string)jalbum["genre"]["name"];
                    album.artist      = (string)jalbum["artist"]["name"];
                    album.artistID    = (int?)jalbum["artist"]["id"];
                    album.description = (string)jalbum["description"];
                    album.coverURL    = (string)jalbum["image"]["large"];
                    album.label       = (string)jalbum["label"]["name"];
                    album.track_count = (int)jalbum["tracks_count"];

                    objReturn.Add(album);
                }
                return(objReturn);

                #endregion
                #region Track
            }
            else if (obj["track_number"] != null)
            {
                Console.WriteLine("Is Track");
                QabuzeSong objReturn = new QabuzeSong(
                    (string)obj["id"],
                    (string)obj["title"],
                    (string)obj["performer"]["name"],
                    (string)obj["album"]["id"],
                    (int)obj["duration"],
                    (int)obj["track_number"],
                    (int)obj["media_number"],
                    (string)obj["album"]["title"],
                    (string)obj["album"]["genre"]["name"]
                    );
                return(objReturn);

                #endregion
                #region Artist
            }
            else if (obj["albums_as_primary_artist_count"] != null)
            {
                Console.WriteLine("Is Artist");
                QabuzeArtist objReturn = new QabuzeArtist();

                objReturn.albums_as_primary_artist_count = (int)obj["albums_as_primary_artist_count"];
                objReturn.id       = (int)obj["id"];
                objReturn.name     = (string)obj["name"];
                objReturn.imageURL = (string)obj["image"]["mega"];
                if (obj["biography"] != null)
                {
                    objReturn.biography = (string)obj["biography"]["content"];
                }

                return(objReturn);
            }
            #endregion

            // Console.WriteLine(response);
            return(null);
        }