/// <summary> /// 获取TOP 50推荐列表 /// </summary> /// <returns></returns> public async Task <ConcurrentQueue <string> > getRallist() { string rall; ConcurrentQueue <string> queue = new ConcurrentQueue <string>(); HttpUtil top50 = new HttpUtil(RALL_URL, HttpUtil.Contype.JSON); rall = await top50.GetDataAsync(); if (!rall.Equals("ERROR")) { dynamic o = JObject.Parse(rall); JArray arr = o.contents; foreach (JToken j in arr) { queue.Enqueue(j["illust_id"].ToString()); } } else { string title = "获取插画队列时发生错误"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); } return(queue); }
/// <summary> /// 使用PixivCS API进行插画下载 /// </summary> /// <param name="img"></param> /// <returns>是否成功下载插画</returns> public async Task <bool> downloadImgV2(ImageInfo img) { try { using (Stream resStream = await(await new PixivAppAPI(baseAPI).RequestCall("GET", img.imgUrl, new Dictionary <string, string>() { { "Referer", "https://app-api.pixiv.net/" } })). Content.ReadAsStreamAsync()) { StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(img.imgId, CreationCollisionOption.OpenIfExists); using (Stream writer = await file.OpenStreamForWriteAsync()) { await resStream.CopyToAsync(writer); return(true); } } } catch (Exception) { //使UI线程调用lambda表达式内的方法 /*await MainPage.mp.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => * { * //UI code here * MessageDialog dialog = new MessageDialog(""); * dialog.Content = "获取插画时发生未知错误"; * await dialog.ShowAsync(); * });*/ string title = "下载插画时发生未知错误"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); return(false); } }
/// <summary> /// 使用PixivCS API的选择方法 /// </summary> /// <returns></returns> public async Task <ImageInfo> SelectArtWorkV2() { await ListUpdateV2(); ImageInfo img = null; if (likeV2 != null && likeV2.Count != 0) { while (true) { likeV2.TryDequeue(out img); if (img != null && img.WHratio >= 1.33 && !img.isR18) { if (!await pixiv.downloadImgV2(img)) //当获取插画失败时返回null { img = null; } break; } } } else { //使UI线程调用lambda表达式内的方法 /*await MainPage.mp.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => * { * //UI code here * MessageDialog dialog = new MessageDialog("更新推荐列表失败"); * await dialog.ShowAsync(); * });*/ string title = "获取pixiv推荐列表失败"; string content = " "; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); } return(img); }
/// <summary> /// 插画下载方法 /// </summary> /// <param name="userid">插画作者ID</param> /// <param name="imgid">插画ID</param> /// <returns>插画存储地址</returns> public async Task <string> ImageDownloadAsync(string imgid) { //应访问一次插画展示页使pixiv记录一次浏览数以尊重他人成果 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(referrer); request.Method = "GET"; request.Accept = contype[(int)dataType]; request.Headers["Cookie"] = cookie; request.Headers["Referer"] = "https://www.pixiv.net/discovery"; await request.GetResponseAsync(); //正式开始获取插画原图 request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = contype[(int)dataType]; request.Headers["Cookie"] = cookie; request.Headers["Referer"] = referrer; try { //判断文件夹中是否已存在该插画,若存在则直接返回该插画 if (await ApplicationData.Current.LocalFolder.TryGetItemAsync(imgid + ".jpg") != null) { return(imgid); } else { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); if (response.StatusCode == HttpStatusCode.OK) { using (Stream res = response.GetResponseStream()) { StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(imgid + ".jpg", CreationCollisionOption.ReplaceExisting); using (Stream writer = await file.OpenStreamForWriteAsync()) { await res.CopyToAsync(writer); return(imgid); } } } else { string title = "获取插画时连接失败"; string content = "尝试从pixiv下载插画时连接超时,请检查网络连接"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); return("ERROR"); } } } catch (Exception) { string title = "获取插画时连接中断"; string content = "在从pixiv服务器下载插画时连接中断,请检查网络连接"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); return("ERROR"); } }
/// <summary> /// 使用Web模拟登录的选择方式 /// </summary> /// <returns></returns> public async Task <ImageInfo> SelectArtWorkV1() { if (pixiv.token == null || pixiv.token.Equals("")) { if (c.token.Equals("")) { if (!await pixiv.getToken(c.cookie)) //getToken不成功,返回null { return(null); } else //getToken成功,将token写入配置文件 { c.cookie = pixiv.cookie; c.token = pixiv.token; } } else //配置文件中已有token,直接调用(可能出现token过期情况) { pixiv.cookie = c.cookie; pixiv.token = c.token; } } await ListUpdateV1(); ImageInfo img = null; if (like != null && like.Count != 0) { while (true) { string id = ""; if (like.TryDequeue(out id)) { img = await pixiv.getImageInfo(id); } if (img != null && img.WHratio >= 1.33 && !img.isR18) { if (!await pixiv.downloadImg(img)) //当获取插画失败时返回null { img = null; } break; } } } else { //使UI线程调用lambda表达式内的方法 /*await MainPage.mp.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => * { * //UI code here * MessageDialog dialog = new MessageDialog("更新推荐列表失败"); * await dialog.ShowAsync(); * });*/ string title = "获取pixiv推荐列表失败"; string content = "请检查网络连接,并尝试清理cookie与token"; ToastManagement tm = new ToastManagement(title, content, ToastManagement.ErrorMessage); tm.ToastPush(60); } return(img); }