Exemplo n.º 1
0
        protected override async Task GetImagesMetadataAsync(string apiUrl)
        {
            var paginationOffset = 0;

            while (true)
            {
                var rawResponse = await Client.GetStringAsyncM(apiUrl + paginationOffset).ConfigureAwait(false);

                var responseJson = JObject.Parse(rawResponse);
                var Gallery      = (JContainer)responseJson["results"];
                if (!(Gallery.HasValues))
                {
                    return;                           // check if the user has any images in his gallery
                }
                var tasks = Gallery.Select(async(image) =>
                {
                    if (image["content"] == null)
                    {
                        return;
                    }
                    var deviationID = image["deviationid"].ToString();
                    var url         = image["content"]["src"].ToString();
                    if (_scaledDownImgUrlPattern.IsMatch(url))               // Check if it's a URL to a scaled down image in order to avoid unnecessary api calls (too many calls may result in a 'Error 401 API token expired')
                    {
                        if (await GetOriginImage(deviationID) is { } _url)   // try to get the origin image, use the scaled down image if fails
                        {
                            url = _url;
                        }
                    }
                    lock (ImagesToDownload)
                    {
                        ImagesToDownload.Add(new ImageModel()
                        {
                            Url      = url,
                            Name     = image["title"].ToString(),
                            ID       = deviationID,
                            FileType = image["content"]["src"].ToString().Split('?')[0].Split('/').Last()
                                       .Split('.')[1] // maybe not the best way but surely the the easiest one
                        });
                    }
                });
                await Task.WhenAll(tasks);

                if (responseJson["has_more"].ToString() == "False")
                {
                    return;
                }
                paginationOffset += Offset;
            }
        }
Exemplo n.º 2
0
        // get all the images from a project
        private async Task GetAssets(string hash_id, string name)
        {
            var response = await Client.GetStringAsyncM(string.Format(AssetsUrl, hash_id)).ConfigureAwait(false);

            var assetsJson = JObject.Parse(response);

            foreach (var image in assetsJson["assets"] as JContainer)
            {
                lock (ImagesToDownload)
                {
                    ImagesToDownload.Add(new ImageModel()
                    {
                        Url = image["image_url"].ToString().Replace("/large/", "/4k/")
                              .Replace("/covers/", "/images/")
                              .Replace("/video_clips/", "/images/")
                              .Replace("/videos/", "/images/")
                              .Replace("/panos/", "/images/"),                                  // workaround to download 4k images, maybe there's a better way, I might change this later
                        Name     = name,
                        ID       = image["id"].ToString(),
                        FileType = image["image_url"].ToString().Split('?')[0].Split('/').Last().Split('.')[1]
                    });
                }
            }
        }