Skip to content

Commit

Permalink
- fixed Pixiv Login
Browse files Browse the repository at this point in the history
- changed ArtifyModel to a Singleton
- refactoring
  • Loading branch information
sentouki committed Sep 23, 2021
1 parent 901b5a8 commit b011415
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 89 deletions.
4 changes: 2 additions & 2 deletions ArtAPI/PixivAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public PixivAPI()
{
Client.DefaultRequestHeaders.Referrer = new Uri("https://www.pixiv.net");
Client.DefaultRequestHeaders.UserAgent.Clear();
Client.DefaultRequestHeaders.UserAgent.ParseAdd("PixivAndroidApp/5.0.115 (Android 6.0; ArtifyApp)");
Client.DefaultRequestHeaders.Host = "oauth.secure.pixiv.net";
Client.DefaultRequestHeaders.UserAgent.ParseAdd("PixivAndroidApp/5.0.219 (Android 11; Artify)");
//Client.DefaultRequestHeaders.Host = "oauth.secure.pixiv.net";
}

public override async Task<Uri> CreateUrlFromName(string artistName)
Expand Down
47 changes: 29 additions & 18 deletions Artify/Models/ArtifyModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -18,13 +17,12 @@ public enum InputType
ArtistNameOrID,
}
/// <summary>
/// a wrapper around the ArtAPI
/// a wrapper singleton around the ArtAPI
/// </summary>
public class ArtifyModel
{
private readonly string SettingsDirPath, SettingsFilePath;
private RequestArt _platform;
private readonly Dictionary<string, Regex> URLpattern = new Dictionary<string, Regex>()
private readonly string _settingsDirPath, _settingsFilePath;
private readonly Dictionary<string, Regex> _urlPattern = new Dictionary<string, Regex>()
{
{"general", new Regex(@"(https?://)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(\/?[a-zA-Z0-9]*\/?)*") },
{"artstation", new Regex(@"(https://)?(www\.)?artstation\.com/[0-9a-zA-Z]+/?") },
Expand All @@ -38,6 +36,9 @@ public class ArtifyModel
{ "pixiv", () => new PixivAPI() },
{ "deviantart", () => new DeviantArtAPI()}
};
private static ArtifyModel _instance;
public static ArtifyModel Instance => _instance ??= new ArtifyModel();
private RequestArt _platform;
public RequestArt Platform
{
get => _platform;
Expand Down Expand Up @@ -71,25 +72,25 @@ public int ConcurrentTasks

private string _selectedPlatform;
public Settings settings = new Settings();
public ArtifyModel()
private ArtifyModel()
{
SettingsDirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), ".artify");
SettingsFilePath = Path.Combine(SettingsDirPath, "settings.json");
_settingsDirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), ".artify");
_settingsFilePath = Path.Combine(_settingsDirPath, "settings.json");
if (CheckSettingsDir())
LoadSettings();
else CreateSettingsDir();
}

private bool CheckSettingsDir()
{
return Directory.Exists(SettingsDirPath) && File.Exists(SettingsFilePath);
return Directory.Exists(_settingsDirPath) && File.Exists(_settingsFilePath);
}
private void LoadSettings()
{
var fi = new FileInfo(SettingsFilePath) { Attributes = FileAttributes.Normal };
var fi = new FileInfo(_settingsFilePath) { Attributes = FileAttributes.Normal };
try
{
using var sr = new StreamReader(SettingsFilePath);
using var sr = new StreamReader(_settingsFilePath);
var stringJson = sr.ReadToEnd();
if (stringJson.Length > 0)
{
Expand All @@ -104,9 +105,9 @@ private void LoadSettings()
}
private void CreateSettingsDir()
{
var di = Directory.CreateDirectory(SettingsDirPath);
var di = Directory.CreateDirectory(_settingsDirPath);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
File.Create(SettingsFilePath).Close();
File.Create(_settingsFilePath).Close();
}
public void UpdateSettings()
{
Expand All @@ -116,8 +117,8 @@ public void UpdateSettings()
{
settings.pixiv_refresh_token = null;
}
var fi = new FileInfo(SettingsFilePath) { Attributes = FileAttributes.Normal };
using (var sw = new StreamWriter(SettingsFilePath))
var fi = new FileInfo(_settingsFilePath) { Attributes = FileAttributes.Normal };
using (var sw = new StreamWriter(_settingsFilePath))
{
new JsonSerializer().Serialize(sw, settings);
}
Expand All @@ -130,11 +131,11 @@ public void UpdateSettings()
/// <returns>true if it's the right url, false if it's a url but a wrong one, null if neither (e.g. artist name)</returns>
private bool? CheckUrl(string input)
{
if (!URLpattern["general"].IsMatch(input))
if (!_urlPattern["general"].IsMatch(input))
{
return null;
}
return URLpattern[_selectedPlatform].IsMatch(input);
return _urlPattern[_selectedPlatform].IsMatch(input);
}
/// <summary>
/// check the user input
Expand Down Expand Up @@ -166,7 +167,7 @@ public async Task<InputType> CheckUserInput(string input)

public void SelectPlatform(string platformName)
{
Platform = ArtPlatform[platformName](); // create object of the selected platform
Platform = ArtPlatform[platformName](); // create an object of the selected platform
_selectedPlatform = platformName;
Platform.SavePath = SavePath;
Platform.ClientTimeout = ClientTimeout;
Expand All @@ -183,5 +184,15 @@ public async Task<bool> Auth()
}
return false;
}

public async Task PixivLogin(string code)
{
var pixiv = (PixivAPI)Platform;
if (await pixiv.Login(code) is { } result)
{
settings.pixiv_refresh_token = result;
UpdateSettings();
}
}
}
}
27 changes: 10 additions & 17 deletions Artify/ViewModels/ArtifyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Artify.ViewModels
{
public class ArtifyViewModel : BaseViewModel
{
private readonly ArtifyModel artifyModel;
private State _currentState;
#region public properties
public string RunDownloadButtonContent { get; set; } = "Download";
Expand All @@ -30,23 +29,17 @@ public class ArtifyViewModel : BaseViewModel
#region ctor
public ArtifyViewModel()
{
artifyModel = new ArtifyModel();
RunDownloadCommand = new RelayCommand(RunDownload);
SelectPlatformCommand = new RelayCommand<string>(SelectPlatform);
ShutdownCommand = new RelayCommand<IShutDown>(Shutdown);
}
#endregion
private void Shutdown(IShutDown view)
{
artifyModel.UpdateSettings();
ArtifyModel.Instance.UpdateSettings();
CancelDownload();
view.AppShutDown();
}

public SettingsViewModel CreateSettingsVM()
{
return new SettingsViewModel(artifyModel);
}
#region ArtAPI event handler
private void Platform_DownloadStateChanged(object sender, DownloadStateChangedEventArgs e)
{
Expand Down Expand Up @@ -124,11 +117,11 @@ private async void SelectPlatform(string platform)
CancelDownload();
}
SelectedPlatform = platform;
artifyModel.SelectPlatform(platform);
artifyModel.Platform.DownloadProgressChanged += UpdateProgress;
artifyModel.Platform.DownloadStateChanged += Platform_DownloadStateChanged;
artifyModel.Platform.LoginStatusChanged += PlatformOnLoginStatusChanged;
IsLoggedIn = await artifyModel.Auth();
ArtifyModel.Instance.SelectPlatform(platform);
ArtifyModel.Instance.Platform.DownloadProgressChanged += UpdateProgress;
ArtifyModel.Instance.Platform.DownloadStateChanged += Platform_DownloadStateChanged;
ArtifyModel.Instance.Platform.LoginStatusChanged += PlatformOnLoginStatusChanged;
IsLoggedIn = await ArtifyModel.Instance.Auth();
}

private async void RunDownload()
Expand All @@ -142,7 +135,7 @@ private async void RunDownload()
#endif
try
{
switch (await artifyModel.CheckUserInput(UserInput))
switch (await ArtifyModel.Instance.CheckUserInput(UserInput))
{
case InputType.Empty:
InputErrorMessage = "Input field cannot be empty!";
Expand All @@ -157,10 +150,10 @@ private async void RunDownload()
IsInputValid = false;
break;
case InputType.URL:
await artifyModel.Platform.GetImagesAsync(UserInput);
await ArtifyModel.Instance.Platform.GetImagesAsync(UserInput);
break;
case InputType.ArtistNameOrID:
await artifyModel.Platform.GetImagesAsync(await artifyModel.Platform.CreateUrlFromName(UserInput));
await ArtifyModel.Instance.Platform.GetImagesAsync(await ArtifyModel.Instance.Platform.CreateUrlFromName(UserInput));
break;
}
}
Expand All @@ -187,7 +180,7 @@ private async void RunDownload()

private void CancelDownload()
{
artifyModel.Platform?.CancelDownload();
ArtifyModel.Instance.Platform?.CancelDownload();
}
#endregion
}
Expand Down
61 changes: 23 additions & 38 deletions Artify/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
using System.Threading.Tasks;
using ArtAPI;
using ArtAPI;
using ArtAPI.misc;
using Artify.Models;

namespace Artify.ViewModels
{
public class SettingsViewModel : BaseViewModel
{
private readonly ArtifyModel _artifyModel;
public bool IsInputEnabled { get; set; } = true;
public bool OpenBrowser { get; set; } = false;
public bool OpenBrowser { get; set; }
public string LoginUrl { get; set; }
public string LoginNotification { get; set; }
public string SaveLocation
{
get => _artifyModel.SavePath;
set => _artifyModel.SavePath = value;
get => ArtifyModel.Instance.SavePath;
set => ArtifyModel.Instance.SavePath = value;
}
public int ClientTimeout
{
get => _artifyModel.ClientTimeout;
set => _artifyModel.ClientTimeout = value > 0 ? value : 1;
get => ArtifyModel.Instance.ClientTimeout;
set => ArtifyModel.Instance.ClientTimeout = value > 0 ? value : 1;
}
public int DownloadAttempts
{
get => _artifyModel.DownloadAttempts;
set => _artifyModel.DownloadAttempts = value > 0 ? value : 1;
get => ArtifyModel.Instance.DownloadAttempts;
set => ArtifyModel.Instance.DownloadAttempts = value > 0 ? value : 1;
}
public int ConcurrentTasks
{
get => _artifyModel.ConcurrentTasks;
set => _artifyModel.ConcurrentTasks = value > 0 ? value : 1;
get => ArtifyModel.Instance.ConcurrentTasks;
set => ArtifyModel.Instance.ConcurrentTasks = value > 0 ? value : 1;
}

public bool IsLoggedIn { get; set; }
public RelayCommand LoginCommand { get; }
public SettingsViewModel(ArtifyModel artifyM)
public SettingsViewModel()
{
_artifyModel = artifyM;
_artifyModel.Platform.LoginStatusChanged += Platform_LoginStatusChanged;
Platform_LoginStatusChanged(this, new LoginStatusChangedEventArgs(_artifyModel.Platform.LoginState));
LoginCommand = new RelayCommand(async () => await Login());
if (_artifyModel.Platform.CurrentState == State.DownloadRunning)
ArtifyModel.Instance.Platform.LoginStatusChanged += Platform_LoginStatusChanged;
Platform_LoginStatusChanged(this, new LoginStatusChangedEventArgs(ArtifyModel.Instance.Platform.LoginState));
LoginCommand = new RelayCommand(GetCode);
if (ArtifyModel.Instance.Platform.CurrentState == State.DownloadRunning)
{
IsInputEnabled = false;
}
}
~SettingsViewModel()
{
_artifyModel.Platform.LoginStatusChanged -= Platform_LoginStatusChanged;
ArtifyModel.Instance.Platform.LoginStatusChanged -= Platform_LoginStatusChanged;
}

private void Platform_LoginStatusChanged(object sender, LoginStatusChangedEventArgs e)
Expand All @@ -57,40 +53,29 @@ private void Platform_LoginStatusChanged(object sender, LoginStatusChangedEventA
{
case LoginStatus.LoggingIn:
IsInputEnabled = false;
LoginNotification = "Logging in ...";
break;
case LoginStatus.Authenticating:
IsInputEnabled = false;
LoginNotification = "Authenticating ...";
break;
case LoginStatus.LoggedIn:
IsInputEnabled = true;
LoginNotification = "";
IsLoggedIn = true;
OpenBrowser = false;
break;
case LoginStatus.Failed:
LoginNotification = "Authenticating failed";
IsInputEnabled = true;
OpenBrowser = false;
break;
}
}

private async Task Login()
/// <summary>
/// get the URL with code for the further login process and open the browser
/// </summary>
private void GetCode()
{
var pixiv = (PixivAPI) _artifyModel.Platform;
IsInputEnabled = false;
var pixiv = (PixivAPI) ArtifyModel.Instance.Platform;
LoginUrl = pixiv.Pkce();
OpenBrowser = true;
if (await _artifyModel.Platform.Login(UserName, UserPassword) is { } result)
{
_artifyModel.settings.pixiv_refresh_token = result;
_artifyModel.UpdateSettings();
}
else
{
LoginNotification = "Something went wrong";
}
IsInputEnabled = true;
}
}
}
11 changes: 5 additions & 6 deletions Artify/Views/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using ArtAPI;
using Artify.ViewModels;
using Artify.ViewModels.misc;

namespace Artify.Views
{
// ReSharper disable once RedundantExtendsListEntry
public partial class MainWindow : Window, IShutDown
{
private Storyboard
Expand Down Expand Up @@ -49,8 +49,7 @@ private void SettingsClick(object sender, RoutedEventArgs e)
{
popUp = new SettingsPopUp
{
Owner = this,
DataContext = _artifyVM.CreateSettingsVM()
Owner = this
};
popUp.ShowDialog();
}
Expand All @@ -67,13 +66,13 @@ private void ResizeButton_Click(object sender, RoutedEventArgs e)
WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
}

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == System.Windows.Input.Key.Escape) && SelectionMenu.Visibility == Visibility.Hidden)
if ((e.Key == Key.Escape) && SelectionMenu.Visibility == Visibility.Hidden)
{
ShowSelectionMenu();
}
else if ((e.Key == System.Windows.Input.Key.Escape) && SelectionMenu.Visibility == Visibility.Visible && SelectedPlatformIcon.Source != null)
else if ((e.Key == Key.Escape) && SelectionMenu.Visibility == Visibility.Visible && SelectedPlatformIcon.Source != null)
{
HideSelectionMenu();
}
Expand Down
Loading

0 comments on commit b011415

Please sign in to comment.