feature: open with URL

This commit is contained in:
2020-05-09 17:39:01 +08:00
parent 33fe1c8cc1
commit dffe0bb3a4
11 changed files with 251 additions and 34 deletions

View File

@@ -18,15 +18,16 @@ namespace Pixiview.Utils
public static readonly string PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
public static readonly string CacheFolder = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
private const string pagesFolder = "pages";
private const string imageFolder = "img-original";
private const string previewFolder = "img-master";
private const string thumbFolder = "img-thumb";
private const string userFolder = "user-profile";
private const string illustFile = "illust.json";
private const string favoriteFile = "favorites.json";
private const string pagesFolder = "pages";
private const string preloadsFolder = "preloads";
private const string thumbFolder = "img-thumb";
private const string userFolder = "user-profile";
private static readonly object sync = new object();
public static bool NetworkAvailable
@@ -93,7 +94,7 @@ namespace Pixiview.Utils
}
}
private static T LoadObject<T>(string file, string url, string referer = null, bool force = false)
private static T LoadObject<T>(string file, string url, string referer, bool force = false, Func<string, string> action = null)
{
string content = null;
if (!force && file != null && File.Exists(file))
@@ -111,7 +112,10 @@ namespace Pixiview.Utils
{
var response = Download(url, headers =>
{
headers.Referrer = referer == null ? Configs.Referer : new Uri(referer);
if (referer != null)
{
headers.Referrer = new Uri(referer);
}
headers.Add("user-agent", Configs.UserAgent);
headers.Add("accept", Configs.AcceptJson);
headers.Add("cookie", Configs.Cookie);
@@ -123,7 +127,20 @@ namespace Pixiview.Utils
}
using (response)
{
content = response.Content.ReadAsStringAsync().Result;
try
{
content = response.Content.ReadAsStringAsync().Result;
if (action != null)
{
content = action(content);
}
}
catch (Exception ex)
{
App.DebugError("load.strea", $"failed to read stream, error: {ex.Message}");
return default;
}
if (file != null)
{
try
@@ -217,18 +234,52 @@ namespace Pixiview.Utils
var result = LoadObject<IllustData>(
file,
Configs.UrlIllustList,
Configs.Referer,
force: force);
if (result.error)
{
App.DebugPrint($"error when load illust data: {result.message} ({force})");
}
return result;
}
public static IllustPreloadBody LoadIllustPreloadData(string id, bool force = false)
{
var file = Path.Combine(CacheFolder, preloadsFolder, $"{id}.json");
var result = LoadObject<IllustPreloadBody>(
file,
string.Format(Configs.UrlIllust, id),
null,
force: force,
action: content =>
{
var index = content.IndexOf(Configs.SuffixPreload);
if (index > 0)
{
index += Configs.SuffixPreloadLength;
var end = content.IndexOf('\'', index);
if (end > index)
{
content = content.Substring(index, end - index);
}
}
return content;
});
return result;
}
public static IllustPageData LoadIllustPageData(string id, bool force = false)
{
var file = Path.Combine(PersonalFolder, pagesFolder, $"{id}.json");
var file = Path.Combine(CacheFolder, pagesFolder, $"{id}.json");
var result = LoadObject<IllustPageData>(
file,
string.Format(Configs.UrlIllustPage, id),
string.Format(Configs.UrlIllust, id),
force: force);
if (result.error)
{
App.DebugPrint($"error when load page data: {result.message} ({force})");
}
return result;
}
@@ -239,7 +290,12 @@ namespace Pixiview.Utils
string.Format(Configs.UrlIllustUserAll, userId),
string.Format(Configs.UrlIllustUser, userId),
force: force);
if (list.error)
{
App.DebugPrint($"error when load user data: {list.message} ({force})");
}
// TODO
var ids = string.Join("&ids%5B%5D=", list.body.illusts.Keys.Take(20));
var result = LoadObject<IllustUserData>(
@@ -247,6 +303,10 @@ namespace Pixiview.Utils
string.Format(Configs.UrlIllustUserArtworks, userId, ids, 1),
string.Format(Configs.UrlIllustUser, userId),
force: force);
if (result.error)
{
App.DebugPrint($"error when load user illust data: {result.message} ({force})");
}
return result;
}
@@ -330,7 +390,7 @@ namespace Pixiview.Utils
App.DebugPrint($"download, url: {url}");
var response = Download(url, headers =>
{
headers.Referrer = Configs.Referer;
headers.Referrer = new Uri(Configs.Referer);
headers.Add("user-agent", Configs.UserAgent);
headers.Add("accept", Configs.AcceptImage);
});
@@ -409,11 +469,13 @@ namespace Pixiview.Utils
public static class Configs
{
public static readonly WebProxy Proxy = new WebProxy("router.tsanie.us", 8088);
public static readonly Uri Referer = new Uri("https://www.pixiv.net/");
public const int MaxThreads = 3;
public const string Referer = "https://www.pixiv.net/";
public const string UrlIllustList = "https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh";
public const string UrlIllust = "https://www.pixiv.net/artworks/{0}";
public const string SuffixPreload = " id=\"meta-preload-data\" content='";
public const int SuffixPreloadLength = 33; // SuffixPreload.Length
public const string UrlIllustUserAll = "https://www.pixiv.net/ajax/user/{0}/profile/all?lang=zh";
public const string UrlIllustUserArtworks = "https://www.pixiv.net/ajax/user/{0}/profile/illusts?ids%5B%5D={1}&work_category=illustManga&is_first_page={2}&lang=zh";
public const string UrlIllustUser = "https://www.pixiv.net/users/{0}/artworks";