feature: refresh list & save to gallery

This commit is contained in:
2020-05-06 02:21:24 +08:00
parent 190615ab03
commit 8746d311d2
14 changed files with 335 additions and 45 deletions

View File

@@ -4,7 +4,6 @@ using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Essentials;
using Xamarin.Forms;
@@ -15,8 +14,9 @@ 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 imageFolder = "img-master";
private const string previewFolder = "img-preview";
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";
@@ -37,11 +37,10 @@ namespace Pixiview.Utils
}
}
public static async Task<IllustData> LoadIllustData()
private static T LoadObject<T>(string file, string url, string referer = null, bool force = false, IEnumerable<(string header, string value)> headers = null)
{
var file = Path.Combine(PersonalFolder, illustFile);
string content = null;
if (File.Exists(file))
if (!force && File.Exists(file))
{
try
{
@@ -49,44 +48,76 @@ namespace Pixiview.Utils
}
catch (Exception ex)
{
App.DebugError("illust.load", $"failed to read file: {file}, error: {ex.Message}");
App.DebugError("load", $"failed to read file: {file}, error: {ex.Message}");
}
}
if (content == null)
{
var response = Download(Configs.UrlIllust, headers: new[]
{
("cookie", Configs.Cookie),
("x-user-id", Configs.UserId)
});
var response = Download(url, referer, headers);
if (response == null)
{
return null;
return default;
}
using (response)
{
content = await response.Content.ReadAsStringAsync();
content = response.Content.ReadAsStringAsync().Result;
try
{
var folder = Path.GetDirectoryName(file);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
File.WriteAllText(file, content, Encoding.UTF8);
}
catch (Exception ex)
{
App.DebugError("illust.save", $"failed to save illust JSON object, error: {ex.Message}");
App.DebugError("save", $"failed to save illust JSON object, error: {ex.Message}");
}
}
}
try
{
return JsonConvert.DeserializeObject<IllustData>(content);
return JsonConvert.DeserializeObject<T>(content);
}
catch (Exception ex)
{
App.DebugError("illust.load", $"failed to parse illust JSON object, error: {ex.Message}");
return null;
App.DebugError("load", $"failed to parse illust JSON object, error: {ex.Message}");
return default;
}
}
public static IllustData LoadIllustData(bool force = false)
{
var file = Path.Combine(PersonalFolder, illustFile);
var result = LoadObject<IllustData>(
file,
Configs.UrlIllustList,
force: force,
headers: new[]
{
("cookie", Configs.Cookie),
("x-user-id", Configs.UserId)
});
return result;
}
public static IllustPageData LoadIllustPageData(string id, bool force = false)
{
var file = Path.Combine(PersonalFolder, pagesFolder, $"{id}.json");
var result = LoadObject<IllustPageData>(
file,
string.Format(Configs.UrlIllustPage, id),
string.Format(Configs.UrlIllust, id),
force: force,
headers: new[]
{
("cookie", Configs.Cookie),
("x-user-id", Configs.UserId)
});
return result;
}
public static ImageSource LoadIllustImage(string url)
{
return LoadImage(url, PersonalFolder, imageFolder);
@@ -110,15 +141,32 @@ namespace Pixiview.Utils
private static ImageSource LoadImage(string url, string working, string folder)
{
var file = Path.Combine(working, folder, Path.GetFileName(url));
if (!File.Exists(file))
ImageSource image;
if (File.Exists(file))
{
try
{
image = ImageSource.FromFile(file);
}
catch (Exception ex)
{
App.DebugError("image.load", $"failed to load image from file: {file}, error: {ex.Message}");
image = null;
}
}
else
{
image = null;
}
if (image == null)
{
file = DownloadImage(url, working, folder);
if (file != null)
{
return ImageSource.FromFile(file);
}
}
if (file != null)
{
return ImageSource.FromFile(file);
}
return null;
return image;
}
private static string DownloadImage(string url, string working, string folder)
@@ -216,7 +264,9 @@ namespace Pixiview.Utils
public const int MaxThreads = 3;
public const string UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36";
public const string UrlIllust = "https://www.pixiv.net/ajax/top/illust?mode=all&lang=zh";
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 UrlIllustPage = "https://www.pixiv.net/ajax/illust/{0}/pages?lang=zh";
public const string Cookie = "first_visit_datetime_pc=2019-10-29+22%3A05%3A30; p_ab_id=2; p_ab_id_2=6;" +
" p_ab_d_id=1155161977; a_type=0; b_type=1; d_type=2; module_orders_mypage=%5B%7B%22name%22%3A%22s" +
"ketch_live%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22tag_follow%22%2C%22visible%22%3Atrue" +