Example #1
0
        public List <MbSong> GetMbSongs()
        {
            string[]      files      = null;
            List <MbSong> allMbSongs = new List <MbSong>();

            if (_mbApiInterface.Library_QueryFiles("domain=library"))
            {
                // Old (deprecated)
                //public char[] filesSeparators = { '\0' };
                //files = _mbApiInterface.Library_QueryGetAllFiles().Split(filesSeparators, StringSplitOptions.RemoveEmptyEntries);
                _mbApiInterface.Library_QueryFilesEx("domain=library", ref files);
            }
            else
            {
                files = new string[0];
            }

            foreach (string path in files)
            {
                MbSong thisSong = new MbSong();
                thisSong.Filename = path;
                thisSong.Artist   = _mbApiInterface.Library_GetFileTag(path, Plugin.MetaDataType.Artist);
                thisSong.Title    = _mbApiInterface.Library_GetFileTag(path, Plugin.MetaDataType.TrackTitle);
                allMbSongs.Add(thisSong);
            }
            return(allMbSongs);
        }
Example #2
0
        public ObsessedForm(Plugin.MusicBeeApiInterface pApi)
        {
            // C:\MusicBee\AppData\LastFmScrobbles.log | Scrobble log location
            InitializeComponent();

            // --------Initializing lastfm api--------
            string  apikey    = app.Default.API_KEY;    //ConfigurationManager.AppSettings["API_KEY"];
            string  secretkey = app.Default.SECRET_KEY; //ConfigurationManager.AppSettings["SECRET_KEY"];
            Session session   = new Session(apikey, secretkey);
            string  usr       = pApi.Setting_GetLastFmUserId();

            // -------- URL Construction --------
            string root = "http://ws.audioscrobbler.com/2.0/?";

            string m_topTracks = "method=user.gettoptracks&user="******"method=user.gettopalbums&user="******"method=user.getrecenttracks&user="******"&limit=200";
            string pages          = "&page=";
            int    pageNum        = 1;
            string tail           = "&api_key=" + apikey + "&format=json";
            // ---------------------------------------

            string url = root + m_recentTracks + limit + pages + pageNum + tail;

            var request = (HttpWebRequest)WebRequest.Create(url);

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();


            // Use QueryFilesEx to get the fileURLs
            pApi.Library_QueryFilesEx("domain=SelectedFiles", out String[] files); // files contains the fileURL for a selected track(s)

            string[] lines = System.IO.File.ReadAllLines(@"C:\MusicBee\AppData\LastFmScrobble.log");

            string artist = pApi.Library_GetFileTag(files[0], Plugin.MetaDataType.Artist);
            string title  = pApi.Library_GetFileTag(files[0], Plugin.MetaDataType.TrackTitle);
            int    plays  = Int32.Parse(pApi.Library_GetFileTag(files[0], (Plugin.MetaDataType) 14));

            string added = pApi.Library_GetFileProperty(files[0], Plugin.FilePropertyType.DateAdded); //Date the selected track was added to the library


            string track = artist + " - " + title;                 // Consolidation for matching in the scrobble log

            List <DateTime> history = filter(lines, track, plays); // history contains the history of scrobbles for a given track represented in unix time.

            if (history.Count <= 1)
            {
                label1.Text = "Not enough scrobbles for this track";
            }
            else
            {
                label1.Text = responseString + "";// +" | "+obsession(history, added, title, artist, plays);
            }
        }
        // Synchronise the playlists defined in the settings file to Google Music
        public void SyncPlaylistsToGMusic(List <MbPlaylist> mbPlaylistsToSync)
        {
            _syncRunning = true;
            AutoResetEvent waitForEvent = new AutoResetEvent(false);


            if (_dataFetched)
            {
                // Get the MusicBee playlists
                foreach (MbPlaylist playlist in mbPlaylistsToSync)
                {
                    // Use LINQ to check for a playlist with the same name
                    // If there is one, clear it's contents, otherwise create one
                    // Unless it's been deleted, in which case pretend it doesn't exist.
                    // I'm not sure how to undelete a playlist, or even if you can
                    GMusicPlaylist thisPlaylist   = _allPlaylists.FirstOrDefault(p => p.Name == playlist.Name && p.Deleted == false);
                    String         thisPlaylistID = "";
                    if (thisPlaylist != null)
                    {
                        List <GMusicPlaylistEntry> allPlsSongs = thisPlaylist.Songs;
                        // This simply signals the wait handle when done
                        api.OnDeleteFromPlaylistComplete = delegate(MutatePlaylistResponse response)
                        {
                            waitForEvent.Set();
                        };
                        api.DeleteFromPlaylist(allPlsSongs);

                        // Wait until the deletion is done
                        waitForEvent.WaitOne();
                        thisPlaylistID = thisPlaylist.ID;
                    }
                    else
                    {
                        // Set the callback
                        api.OnCreatePlaylistComplete = delegate(MutateResponse response)
                        {
                            thisPlaylistID = response.ID;
                            waitForEvent.Set();
                        };
                        // Create the playlist
                        api.CreatePlaylist(playlist.Name);
                        // Wait until creation is done
                        waitForEvent.WaitOne();
                    }

                    // Create a list of files based on the MB Playlist
                    string[] playlistFiles = null;
                    if (_mbApiInterface.Playlist_QueryFiles(playlist.mbName))
                    {
                        // Old method:
                        //  playlistFiles = _mbApiInterface.Playlist_QueryGetAllFiles().Split(filesSeparators, StringSplitOptions.RemoveEmptyEntries);

                        bool success = _mbApiInterface.Playlist_QueryFilesEx(playlist.mbName, ref playlistFiles);
                        if (!success)
                        {
                            throw new Exception("Couldn't get playlist files");
                        }
                    }
                    else
                    {
                        playlistFiles = new string[0];
                    }

                    List <GMusicSong> songsToAdd = new List <GMusicSong>();
                    // And get the title and artist of each file, and add it to the GMusic playlist
                    foreach (string file in playlistFiles)
                    {
                        string     title  = _mbApiInterface.Library_GetFileTag(file, Plugin.MetaDataType.TrackTitle);
                        string     artist = _mbApiInterface.Library_GetFileTag(file, Plugin.MetaDataType.Artist);
                        GMusicSong gSong  = _allSongs.FirstOrDefault(item => (item.Artist == artist && item.Title == title));
                        if (gSong != null)
                        {
                            songsToAdd.Add(gSong);
                        }
                    }

                    api.OnAddToPlaylistComplete = delegate(MutatePlaylistResponse response)
                    {
                        waitForEvent.Set();
                    };
                    api.AddToPlaylist(thisPlaylistID, songsToAdd);
                    waitForEvent.WaitOne();
                }

                _syncRunning = false;
                // Signal to anyone calling that we're done
                if (OnSyncComplete != null)
                {
                    OnSyncComplete(this, new EventArgs());
                }
            }
            else
            {
                throw new Exception("Not fetched data yet");
            }
        }