private LabelSearchResult ReadLabelSearchResult(JObject source)
        {
            LabelSearchResult labelResult = new LabelSearchResult();

            foreach (var item in source)
            {
                switch (item.Key)
                {
                case "id":
                case "thumb":
                case "title":
                case "type":
                case "uri":
                case "resource_url":
                    break;

                default:
                    this.ThrowIfStrict("Unknown key: " + item.Key);
                    break;
                }
            }
            return(labelResult);
        }
Exemple #2
0
        protected virtual Item GetParentItem(Label label, Item accountItem)
        {
            if (string.IsNullOrEmpty(label.ParentId))
            {
                return(accountItem.Children["Labels"]);
            }

            var expression = ContentSearchUtil.GetAncestorFilter <LabelSearchResult>(accountItem, TemplateIDs.Label);

            expression.And(i => i.Id == label.ParentId);

            LabelSearchResult searchResults = ContentSearchUtil.FindOne(Constants.IndexName, expression.And(i => i.Id == label.ParentId));

            if (searchResults != null)
            {
                Item item = searchResults.GetItem();
                if (item != null)
                {
                    return(item);
                }
            }

            return(accountItem.Axes.SelectSingleItem(string.Format("./Labels//*[@@templateid='{0}' and @Id='{1}']", TemplateIDs.Label, label.ParentId)));
        }
Exemple #3
0
 private LabelSearchResult ReadLabelSearchResult(JObject source)
 {
     LabelSearchResult labelResult = new LabelSearchResult();
     foreach (var item in source)
     {
         switch (item.Key)
         {
             case "id":
             case "thumb":
             case "title":
             case "type":
             case "uri":
             case "resource_url":
                 break;
             default:
                 this.ThrowIfStrict("Unknown key: " + item.Key);
                 break;
         }
     }
     return labelResult;
 }
Exemple #4
0
        public async Task <OperationResult <IEnumerable <LabelSearchResult> > > PerformLabelSearch(string labelName, int resultsCount)
        {
            LabelSearchResult data = null;

            try
            {
                var cacheKey = $"uri:discogs:labelsearch:{ labelName.ToAlphanumericName() }";
                data = await CacheManager.GetAsync <LabelSearchResult>(cacheKey, async() =>
                {
                    var request = BuildSearchRequest(labelName, 1, "label");

                    var client       = new RestClient("https://api.discogs.com/database");
                    client.UserAgent = WebHelper.UserAgent;

                    var response = await client.ExecuteAsync <DiscogsResult>(request);

                    if (response.ResponseStatus == ResponseStatus.Error)
                    {
                        if (response.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            throw new AuthenticationException("Unauthorized");
                        }
                        throw new Exception(string.Format("Request Error Message: {0}. Content: {1}.",
                                                          response.ErrorMessage, response.Content));
                    }

                    var responseData = response.Data.results != null && response.Data.results.Any()
                        ? response.Data.results.First()
                        : null;
                    if (responseData != null)
                    {
                        request           = BuildLabelRequest(responseData.id);
                        var c2            = new RestClient("https://api.discogs.com/");
                        c2.UserAgent      = WebHelper.UserAgent;
                        var labelResponse = await c2.ExecuteTaskAsync <DiscogsLabelResult>(request);
                        var label         = labelResponse.Data;
                        if (label != null)
                        {
                            var urls                 = new List <string>();
                            var images               = new List <string>();
                            var alternateNames       = new List <string>();
                            string labelThumbnailUrl = null;
                            urls.Add(label.uri);
                            if (label.urls != null)
                            {
                                urls.AddRange(label.urls);
                            }
                            if (label.images != null)
                            {
                                images.AddRange(label.images.Where(x => x.type != "primary").Select(x => x.uri));
                                var primaryImage = label.images.FirstOrDefault(x => x.type == "primary");
                                if (primaryImage != null)
                                {
                                    labelThumbnailUrl = primaryImage.uri;
                                }
                                if (string.IsNullOrEmpty(labelThumbnailUrl))
                                {
                                    labelThumbnailUrl = label.images.First(x => !string.IsNullOrEmpty(x.uri)).uri;
                                }
                            }

                            data = new LabelSearchResult
                            {
                                LabelName      = label.name,
                                DiscogsId      = label.id.ToString(),
                                Profile        = label.profile,
                                AlternateNames = alternateNames,
                                LabelImageUrl  = labelThumbnailUrl,
                                Urls           = urls,
                                ImageUrls      = images
                            };
                        }
                    }
                    return(null);
                }, "uri:metadata");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }

            return(new OperationResult <IEnumerable <LabelSearchResult> >
            {
                IsSuccess = data != null,
                Data = new[] { data }
            });
        }