From 5d5f7590e77b0f3e3f3ceeafbc885a16da887926 Mon Sep 17 00:00:00 2001 From: sentouki Date: Tue, 10 Nov 2020 17:50:49 +0100 Subject: [PATCH] - App will now download original sized images from Deviantart (Issue#1) - minor fixes and improvements --- ArtAPI/DeviantArtAPI.cs | 37 +++++++++++++++++++++++++++-------- Artify/Views/MainView.xaml | 2 +- Artify/Views/MainView.xaml.cs | 7 +++++++ README.md | 2 +- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/ArtAPI/DeviantArtAPI.cs b/ArtAPI/DeviantArtAPI.cs index a0817e7..b1632b8 100644 --- a/ArtAPI/DeviantArtAPI.cs +++ b/ArtAPI/DeviantArtAPI.cs @@ -13,7 +13,8 @@ private const string ClientID = "12774", ClientSecret = "597114f315705b9624c7c1d74ad729e1", AUTH_URL = "https://www.deviantart.com/oauth2/token", - GALLERY_URL = "https://www.deviantart.com/api/v1/oauth2/gallery/all?mature_content=true&limit=20&username={0}&offset="; + GALLERY_URL = "https://www.deviantart.com/api/v1/oauth2/gallery/all?mature_content=true&limit=20&username={0}&offset=", + ORIGINIMAGE_URL = "https://www.deviantart.com/api/v1/oauth2/deviation/download/{0}?mature_content=true"; private const int Offset = 20; @@ -54,21 +55,41 @@ protected override async Task GetImagesMetadataAsync(string apiUrl) var responseJson = JObject.Parse(rawResponse); var Gallery = (JContainer)responseJson["results"]; if (!(Gallery.HasValues)) return; // check if the user has any images in his gallery - foreach (var image in Gallery) + var tasks = Gallery.Select(async (image) => { - if (image["content"] == null) continue; + if (image["content"] == null) return; + var deviationID = image["deviationid"].ToString(); ImagesToDownload.Add(new ImageModel() { - Url = image["content"]["src"].ToString(), + Url = (await GetOriginImage(deviationID) is { } url) ? url : image["content"]["src"].ToString(), // try to get the origin image, use the scaled down image if fails Name = image["title"].ToString(), - ID = image["deviationid"].ToString(), - FileType = image["content"]["src"].ToString().Split('?')[0].Split('/').Last().Split('.')[1] // maybe not the best way but surely the the easiest one + 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; } } + /// + /// get the Url to the origin image, not the scaled down one + /// + private async Task GetOriginImage(string deviationID) + { + try + { + var rawResponse = await Client.GetStringAsync(string.Format(ORIGINIMAGE_URL, deviationID)) + .ConfigureAwait(false); + var responseJson = JObject.Parse(rawResponse); + return responseJson["src"].ToString(); + } + catch (HttpRequestException) + { + return null; + } + } public override async Task auth(string refreshToken) { @@ -89,7 +110,7 @@ public override async Task auth(string refreshToken) Client.DefaultRequestHeaders.Remove("Authorization"); Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jsonResponse["access_token"]}"); } - catch(HttpRequestException) + catch (HttpRequestException) { return false; } diff --git a/Artify/Views/MainView.xaml b/Artify/Views/MainView.xaml index ba4b4c8..9027e9f 100644 --- a/Artify/Views/MainView.xaml +++ b/Artify/Views/MainView.xaml @@ -121,7 +121,7 @@ - +