Example #1
0
        public static DeezerJsonFilter BuildFilterFromQuery(string query)
        {
            DeezerJsonFilter filter = null;

            try
            {
                // A filter string was specified by the user, so try to parse its fields ...
                // Instead of old-school parse using string.Split or regex, we use JSON, it is cooler :)
                string jsonSearchBody = query;

                jsonSearchBody = jsonSearchBody.Replace("artist:", ",\"artist\":");
                jsonSearchBody = jsonSearchBody.Replace("album:", ",\"album\":");
                jsonSearchBody = jsonSearchBody.Replace("track:", ",\"track\":");
                jsonSearchBody = jsonSearchBody.Trim(",".ToCharArray());

                string jsonSearch = string.Format("{{{0}}}", jsonSearchBody);

                filter = JsonConvert.DeserializeObject <DeezerJsonFilter>(jsonSearch);
            }
            catch
            {
                filter = null;
            }

            return(filter);
        }
Example #2
0
        protected override List <OnlineMediaItem> Search(OnlineContentSearchParameters searchParams, ManualResetEvent abortEvent)
        {
            List <OnlineMediaItem> results = new List <OnlineMediaItem>();

            if (abortEvent.WaitOne(5))
            {
                return(results);
            }

            if (HasValidConfig)
            {
                string userAccessToken   = ProTONEConfig.DeezerUserAccessToken;
                string applicationId     = ProTONEConfig.DeezerApplicationId;
                string deezerApiEndpoint = ProTONEConfig.DeezerApiEndpoint;

                DeezerRuntime dzr = new DeezerRuntime(deezerApiEndpoint, userAccessToken, applicationId);
                if (_dzr != dzr)
                {
                    _dzr = dzr;
                }

                searchParams.Filter = OnlineContentSearchFilter.None;

                bool isFilteredSearch = false;

                isFilteredSearch |= searchParams.SearchText.Contains("artist:");
                isFilteredSearch |= searchParams.SearchText.Contains("track:");
                isFilteredSearch |= searchParams.SearchText.Contains("album:");

                DeezerJsonFilter filter = null;

                if (isFilteredSearch)
                {
                    filter = BuildFilterFromQuery(searchParams.SearchText);
                }

                List <Track> tracks = _dzr.ExecuteSearch(searchParams.SearchText, abortEvent);

                if (tracks != null)
                {
                    foreach (var t in tracks)
                    {
                        try
                        {
                            DeezerTrackItem dti = new DeezerTrackItem
                            {
                                Album    = (t.Album != null) ? t.Album.Title : String.Empty,
                                Artist   = (t.Artist != null) ? t.Artist.Name : string.Empty,
                                Title    = t.Title ?? string.Empty,
                                Url      = string.Format("dzmedia:///track/{0}", t.Id),
                                Duration = t.Duration
                            };

                            bool shouldAddTrack = true;

                            if (DeezerJsonFilter.IsNullOrEmpty(filter) == false)
                            {
                                if (shouldAddTrack && string.IsNullOrEmpty(filter.Artist) == false)
                                {
                                    shouldAddTrack &= dti.Artist.ToLowerInvariant().Contains(filter.Artist);
                                }

                                if (shouldAddTrack && string.IsNullOrEmpty(filter.Album) == false)
                                {
                                    shouldAddTrack &= dti.Album.ToLowerInvariant().Contains(filter.Album);
                                }

                                if (shouldAddTrack && string.IsNullOrEmpty(filter.Title) == false)
                                {
                                    shouldAddTrack &= dti.Title.ToLowerInvariant().Contains(filter.Title);
                                }
                            }

                            if (shouldAddTrack)
                            {
                                results.Add(dti);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.LogException(ex);
                        }
                    }
                }
            }

            if (abortEvent.WaitOne(5))
            {
                return(results);
            }

            SortResults(ref results);

            return(results);
        }
Example #3
0
 public static bool IsNullOrEmpty(DeezerJsonFilter flt)
 {
     return(flt == null || flt.IsEmpty);
 }