Skip to content

Commit

Permalink
- added TimeOut, ConcurrentTasks and DownloadAttempts public properti…
Browse files Browse the repository at this point in the history
…es to ArtAPI

- Added new event for login/authenticating process
- outsourced the settings properties into a separate SettingsViewModel
- refactoring
  • Loading branch information
sentouki committed Dec 2, 2020
1 parent 086e327 commit 132cedd
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 207 deletions.
8 changes: 7 additions & 1 deletion ArtAPI/ArtStationAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public sealed class ArtStationAPI : RequestArt
private const string ApiUrl = @"https://artstation.com/users/{0}/projects?page=";
private const string AssetsUrl = @"https://www.artstation.com/projects/{0}.json";

public ArtStationAPI()
{
IsLoggedIn = true;
LoginState = LoginStatus.LoggedIn;
}

public override Task<Uri> CreateUrlFromName(string artistName)
{
return Task.FromResult(new Uri(string.Format($@"https://www.artstation.com/{artistName}")));
Expand Down Expand Up @@ -55,7 +61,7 @@ protected override async Task GetImagesMetadataAsync(string apiUrl)
var t = Task.WhenAll(allPages.Select(project => Task.Run(async () => { await GetAssets(project["hash_id"].ToString(), project["title"].ToString()).ConfigureAwait(false); })).ToArray());
try { await t.ConfigureAwait(false); }
catch (Exception e)
{ OnDownloadStateChanged(new DownloadStateChangedEventArgs(State.DownloadCanceled, e.Message)); return; }
{ OnDownloadStateChanged(new DownloadStateChangedEventArgs(State.DownloadCanceled, e.Message)); }
}

// get all the images from a project
Expand Down
7 changes: 4 additions & 3 deletions ArtAPI/DeviantArtAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ private const string

private const int Offset = 20;

public bool IsLoggedIn { get; private set; }

private readonly Regex _scaledDownImgUrlPattern = new Regex(@"(w_[0-9]{3,4}),(h_[0-9]{3,4}),(q_[0-9]{2})", RegexOptions.Compiled);

public override Task<Uri> CreateUrlFromName(string artistName)
Expand Down Expand Up @@ -104,8 +102,9 @@ private async Task<string> GetOriginImage(string deviationID)
}
}

public override async Task<bool> auth(string refreshToken)
public override async Task<bool> Auth(string refreshToken)
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Authenticating));
var data = new Dictionary<string, string>()
{
{"grant_type", "client_credentials" },
Expand All @@ -124,8 +123,10 @@ public override async Task<bool> auth(string refreshToken)
}
catch (HttpRequestException)
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Failed));
return false;
}
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.LoggedIn));
return IsLoggedIn = true;
}
}
Expand Down
55 changes: 45 additions & 10 deletions ArtAPI/General.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
#pragma warning disable CA5351

namespace ArtAPI
{
public enum State
{
DownloadPreparing,
DownloadRunning,
DownloadCompleted,
DownloadCanceled,
ExceptionRaised,
}

public static class General
{
/// <summary>
/// for hashing strings, used for Pixiv API authentication
/// </summary>
/// <returns>hashed string</returns>
public static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
Expand All @@ -33,6 +28,36 @@ public static string CreateMD5(string input)
return sb.ToString();
}
}
/// <summary>
/// special characters which cannot be used for file and directory names
/// </summary>
private static readonly List<string> SpecialChars = new List<string>() { @"\", "/", ":", "*", "?", "\"", "<", ">", "|" };
/// <summary>
/// remove all the nasty characters that can cause trouble
/// </summary>
/// <returns>normalized file name</returns>
public static string NormalizeFileName(string filename)
{
SpecialChars.ForEach(c => filename = filename.Replace(c, ""));
return filename;
}

}
public enum State
{
DownloadPreparing,
DownloadRunning,
DownloadCompleted,
DownloadCanceled,
ExceptionRaised,
}
public enum LoginStatus
{
NotLoggedIn,
LoggingIn,
Authenticating,
LoggedIn,
Failed
}

#region defining EventArgs
Expand Down Expand Up @@ -65,5 +90,15 @@ public DownloadProgressChangedEventArgs(int progress)
CurrentProgress = progress;
}
}

public class LoginStatusChangedEventArgs : EventArgs
{
public LoginStatus Status { get; }

public LoginStatusChangedEventArgs(LoginStatus status)
{
Status = status;
}
}
#endregion
}
13 changes: 10 additions & 3 deletions ArtAPI/PixivAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ private const string
ArtistDetails = @"https://www.pixiv.net/touch/ajax/user/details?id={0}";

private string _artistName;
public bool IsLoggedIn { get; private set; }

public string RefreshToken { get; private set; }

Expand Down Expand Up @@ -202,9 +201,10 @@ private void GetImageURLsWithLogin(JObject responseJson)
}
}

public override async Task<bool> auth(string refreshToken)
public override async Task<bool> Auth(string refreshToken)
{
if (IsLoggedIn) return true;
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Authenticating));
var clientTime = DateTime.UtcNow.ToString("s") + "+00:00";
var data = new Dictionary<string, string>()
{
Expand All @@ -228,6 +228,7 @@ public override async Task<bool> auth(string refreshToken)
var jsonResponse = JObject.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
if (jsonResponse.ContainsKey("has_error"))
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Failed));
return false;
}
var accessToken = jsonResponse["response"]["access_token"]?.ToString() ??
Expand All @@ -239,13 +240,16 @@ public override async Task<bool> auth(string refreshToken)
}
catch (HttpRequestException)
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Failed));
return false;
}
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.LoggedIn));
return IsLoggedIn = true;
}

public override async Task<string> login(string username, string password)
public override async Task<string> Login(string username, string password)
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.LoggingIn));
var clientTime = DateTime.UtcNow.ToString("s") + "+00:00";
var data = new Dictionary<string, string>()
{
Expand All @@ -270,6 +274,7 @@ public override async Task<string> login(string username, string password)
var jsonResponse = JObject.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
if (jsonResponse.ContainsKey("has_error"))
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Failed));
return null;
}
var accessToken = jsonResponse["response"]["access_token"]?.ToString() ??
Expand All @@ -281,9 +286,11 @@ public override async Task<string> login(string username, string password)
}
catch (HttpRequestException)
{
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.Failed));
return null;
}
IsLoggedIn = true;
OnLoginStatusChanged(new LoginStatusChangedEventArgs(LoginStatus.LoggedIn));
return RefreshToken;
}
}
Expand Down
Loading

0 comments on commit 132cedd

Please sign in to comment.