Skip to content

Commit

Permalink
- Modified ImageModel: added File Type property;
Browse files Browse the repository at this point in the history
- Images are now saved with the correct file type;
- Implemented CheckLocalImages: already downloaded images will not be downloaded again
  • Loading branch information
sentouki committed Jul 19, 2020
1 parent d59376a commit 9eaf767
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
3 changes: 2 additions & 1 deletion ArtAPI/ArtStationAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ private async Task GetAssets(string hash_id, string name)
.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()
ID = image["id"].ToString(),
FileType = image["image_url"].ToString().Split('?')[0].Split('/').Last().Split('.')[1]
});
}
}
Expand Down
10 changes: 7 additions & 3 deletions ArtAPI/DeviantArtAPI.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -60,9 +61,9 @@ protected override async Task GetImagesMetadataAsync(string apiUrl)
{
Url = image["content"]["src"].ToString(),
Name = image["title"].ToString(),
ID = image["deviationid"].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
});
}
if (responseJson["has_more"].ToString() == "False") return;
paginationOffset += Offset;
Expand All @@ -71,6 +72,7 @@ protected override async Task GetImagesMetadataAsync(string apiUrl)

public override async Task<bool> auth(string refreshToken)
{
if (IsLoggedIn) return true;
var data = new Dictionary<string, string>()
{
{"grant_type", "client_credentials" },
Expand All @@ -83,6 +85,8 @@ public override async Task<bool> auth(string refreshToken)
var response = await Client.PostAsync(AUTH_URL, content).ConfigureAwait(false);
var jsonResponse = JObject.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
if (jsonResponse["status"].ToString() == "error") return false;
if (Client.DefaultRequestHeaders.Contains("Authorization"))
Client.DefaultRequestHeaders.Remove("Authorization");
Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {jsonResponse["access_token"]}");
}
catch(HttpRequestException)
Expand Down
9 changes: 6 additions & 3 deletions ArtAPI/IRequestArt.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -131,6 +132,7 @@ protected async Task DownloadImagesAsync()
OnDownloadStateChanged(new DownloadStateChangedEventArgs(State.DownloadCanceled, "No images to download"));
Clear(); return;
}
CheckLocalImages();
OnDownloadStateChanged(new DownloadStateChangedEventArgs(State.DownloadRunning, TotalImageCount: TotalImageCount));
cts = new CancellationTokenSource();
using var ss = new SemaphoreSlim(ConcurrentTasks);
Expand Down Expand Up @@ -175,7 +177,7 @@ protected async Task DownloadAsync(ImageModel image, string savePath)
var specialChars = new List<string>() { @"\", "/", ":", "*", "?", "\"", "<", ">", "|" };
var imageName = image.Name;
specialChars.ForEach(c => imageName = imageName.Replace(c, "")); // remove all the nasty characters that can cause trouble
var imageSavePath = Path.Combine(savePath, $"{imageName}_{image.ID}.jpg");
var imageSavePath = Path.Combine(savePath, $"{imageName}_{image.ID}.{image.FileType}");
const int tries = NumberOfDLAttempts;
try
{
Expand Down Expand Up @@ -221,11 +223,12 @@ private void Clear()
ImagesToDownload.Clear();
}
/// <summary>
/// checks if there're already pictures saved locally and removes them from the list <see cref="ImagesToDownload"/>
/// checks if there are already pictures saved locally and removes them from the list <see cref="ImagesToDownload"/>
/// </summary>
private void CheckLocalImages()
{
throw new NotImplementedException();
var localImages = Directory.GetFiles(ArtistDirSavepath).Select(Path.GetFileNameWithoutExtension).ToArray();
ImagesToDownload.RemoveAll(image => localImages.Contains($"{image.Name}_{image.ID}"));
}

public void CancelDownload()
Expand Down
47 changes: 27 additions & 20 deletions ArtAPI/PixivAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@ private async Task GetImageURLsWithoutLoginAsync(string illustProject)
{
Url = img_url["url_big"].ToString(),
Name = illustDetails["title"].ToString(),
ID = img_url["url_big"].ToString()
.Split('/')
.Last()
.Replace(".png", string.Empty)
.Replace(".jpg", string.Empty)
ID = img_url["url_big"].ToString().Split('/').Last().Split('.')[0],
FileType = img_url["url_big"].ToString().Split('/').Last().Split('.')[1]
});
}
}
Expand All @@ -143,7 +140,8 @@ private async Task GetImageURLsWithoutLoginAsync(string illustProject)
{
Url = illustDetails["url_big"].ToString(),
Name = illustDetails["title"].ToString(),
ID = illustDetails["id"].ToString()
ID = illustDetails["id"].ToString(),
FileType = illustDetails["url_big"].ToString().Split('/').Last().Split('.')[1]
});
}
}
Expand All @@ -157,34 +155,39 @@ private void GetImageURLsWithLogin(JObject responseJson)
{
foreach (var img_url in IllustDetails["meta_pages"])
{
ImagesToDownload.Add(new ImageModel()
lock (ImagesToDownload)
{
Url = img_url["image_urls"]["original"].ToString(),
Name = IllustDetails["title"].ToString(),
ID = img_url["image_urls"]["original"].ToString()
.Split('/')
.Last()
.Replace(".png", string.Empty)
.Replace(".jpg", string.Empty)
});
ImagesToDownload.Add(new ImageModel()
{
Url = img_url["image_urls"]["original"].ToString(),
Name = IllustDetails["title"].ToString(),
ID = img_url["image_urls"]["original"].ToString().Split('/').Last().Split('.')[0],
FileType = img_url["image_urls"]["original"].ToString().Split('/').Last().Split('.')[1]
});
}

}
}
else
{
ImagesToDownload.Add(new ImageModel()
lock (ImagesToDownload)
{
Url = IllustDetails["meta_single_page"]["original_image_url"].ToString(),
Name = IllustDetails["title"].ToString(),
ID = IllustDetails["id"].ToString()
});
ImagesToDownload.Add(new ImageModel()
{
Url = IllustDetails["meta_single_page"]["original_image_url"].ToString(),
Name = IllustDetails["title"].ToString(),
ID = IllustDetails["id"].ToString(),
FileType = IllustDetails["meta_single_page"]["original_image_url"].ToString().Split('/').Last().Split('.')[1]
});
}

}
}
}

public override async Task<bool> auth(string refreshToken)
{
if (IsLoggedIn) return true;
var clientTime = DateTime.UtcNow.ToString("s") + "+00:00";
var data = new Dictionary<string, string>()
{
Expand All @@ -208,6 +211,8 @@ public override async Task<bool> auth(string refreshToken)
var accessToken = jsonResponse["response"]["access_token"]?.ToString() ??
throw new Exception("Bad API Response");
RefreshToken = jsonResponse["response"]["refresh_token"].ToString();
if (Client.DefaultRequestHeaders.Contains("Authorization"))
Client.DefaultRequestHeaders.Remove("Authorization");
Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
}
catch (HttpRequestException)
Expand Down Expand Up @@ -243,6 +248,8 @@ public override async Task<string> login(string username, string password)
var accessToken = jsonResponse["response"]["access_token"]?.ToString() ??
throw new Exception("Bad API Response");
RefreshToken = jsonResponse["response"]["refresh_token"].ToString();
if (Client.DefaultRequestHeaders.Contains("Authorization"))
Client.DefaultRequestHeaders.Remove("Authorization");
Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
}
catch (HttpRequestException)
Expand Down
4 changes: 4 additions & 0 deletions ArtAPI/models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ public class ImageModel
/// platform-specific(Artstation, Deviantart, Pixiv...etc) ID of the image
/// </summary>
public string ID { get; set; }
/// <summary>
/// file type of the image (png, jpg, gif)
/// </summary>
public string FileType { get; set; } = "jpg";
}
}

0 comments on commit 9eaf767

Please sign in to comment.