Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pixiv Illegal Characters #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions ArtAPI/PixivAPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -38,10 +38,24 @@ public override async Task<Uri> CreateUrlFromName(string artistName)
// input may be an ID or a name, so we check first if it's an ID
if (int.TryParse(artistName, out _))
return CreateUrlFromID(artistName);
_artistName = artistName; // this will be needed later to create a directory, so we don't need to look up the name twice
_artistName = CleanInput(artistName); // this will be needed later to create a directory, so we don't need to look up the name twice
return CreateUrlFromID(await GetArtistID(artistName));
}

static string CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
try
{
return System.Text.RegularExpressions.Regex.Replace(strIn, @"[^\w\.@-]", "",
System.Text.RegularExpressions.RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (System.Text.RegularExpressions.RegexMatchTimeoutException)
{
return String.Empty;
}
}
public Uri CreateUrlFromID(string userid)
{
return new Uri($@"https://www.pixiv.net/en/users/{userid}");
Expand Down Expand Up @@ -69,7 +83,7 @@ private async Task<string> GetArtistID(string artistName)
private async Task<string> GetArtistName(string artistID)
{
var response = await Client.GetStringAsyncM(string.Format(ArtistDetails, artistID)).ConfigureAwait(false);
return JObject.Parse(response)["body"]["user_details"]["user_name"].ToString();
return CleanInput(JObject.Parse(response)["body"]["user_details"]["user_name"].ToString());
}

public override async Task GetImagesAsync(Uri artistUrl)
Expand Down