/// <summary> /// 显示用户选定类别收藏信息。 /// </summary> /// <param name="subjectCollection"></param> /// <param name="subjectType"></param> /// <returns></returns> public static async Task PopulateSubjectCollectionAsync(ObservableCollection <Collection> subjectCollection, SubjectTypeEnum subjectType) { try { //从文件反序列化 var PreCollection = JsonConvert.DeserializeObject <List <Collection> >(await FileHelper.ReadFromCacheFileAsync("JsonCache\\" + subjectType)); subjectCollection.Clear(); if (PreCollection != null) { foreach (var type in PreCollection) { subjectCollection.Add(type); } } var subjectCollections = await BangumiHttpWrapper.GetSubjectCollectionAsync(OAuthHelper.MyToken.UserId, subjectType); //清空原数据 subjectCollection.Clear(); foreach (var type in subjectCollections.Collects) { subjectCollection.Add(type); } //将对象序列化并存储到文件 await FileHelper.WriteToCacheFileAsync(JsonConvert.SerializeObject(subjectCollection), "JsonCache\\" + subjectType); } catch (Exception e) { Debug.WriteLine("显示用户选定类别收藏信息失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 使用 code 换取 Access Token。 /// </summary> /// <param name="codeString"></param> /// <returns></returns> private static async Task GetAccessToken(string code) { try { AccessToken token; // 重试最多三次 for (int i = 0; i < 3; i++) { Debug.WriteLine($"第{i + 1}次尝试获取Token。"); token = await BangumiHttpWrapper.GetAccessToken(code); if (token != null) { await WriteTokensAsync(token); break; } else { await Task.Delay(1000); } } } catch (Exception e) { Debug.WriteLine("获取AccessToken失败。"); throw e; } }
/// <summary> /// 查询授权信息。 /// </summary> private static async void CheckAccessToken() { try { var token = await BangumiHttpWrapper.CheckAccessToken(MyToken); await WriteTokensAsync(token); } catch (WebException ex) { HttpWebResponse response = (HttpWebResponse)ex.Response; Debug.WriteLine("response.StatusCode:" + response?.StatusCode); if (response?.StatusCode == HttpStatusCode.Unauthorized) { var msgDialog = new Windows.UI.Popups.MessageDialog("登录已过期,请重新登录!") { Title = "登录失效!" }; msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("确定")); await msgDialog.ShowAsync(); } } catch (Exception e) { Debug.WriteLine(e.Message); } }
/// <summary> /// 刷新授权有效期。 /// </summary> /// <returns></returns> public static async Task RefreshAccessToken() { try { AccessToken token; // 重试最多三次 for (int i = 0; i < 3; i++) { Debug.WriteLine($"第{i + 1}次尝试刷新Token。"); token = await BangumiHttpWrapper.RefreshAccessToken(MyToken); if (token != null) { // 刷新后存入内存 MyToken.Token = token.Token; MyToken.RefreshToken = token.RefreshToken; MyToken.UserId = token.UserId; // 将信息写入本地文件 await WriteTokensAsync(token); break; } else { await Task.Delay(1000); } } } catch (Exception e) { Debug.WriteLine("刷新AccessToken失败。"); throw e; } }
/// <summary> /// 获取搜索结果。 /// </summary> /// <param name="keyWord"></param> /// <param name="type"></param> /// <param name="start"></param> /// <param name="n"></param> /// <returns></returns> public static async Task <SearchResult> GetSearchResultAsync(string searchText, string type, int start, int n) { try { return(await BangumiHttpWrapper.GetSearchResultAsync(searchText, type, start, n)); } catch (Exception e) { Debug.WriteLine("获取搜索结果失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 获取用户指定条目收视进度。 /// </summary> /// <param name="subjectId"></param> /// <returns></returns> public static async Task <Progress> GetProgressesAsync(string subjectId) { try { return(await BangumiHttpWrapper.GetProgressesAsync(OAuthHelper.MyToken.UserId, OAuthHelper.MyToken.Token, subjectId)); } catch (Exception e) { Debug.WriteLine("获取用户指定条目收视进度失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 获取指定条目收藏信息。 /// </summary> /// <param name="subjectId"></param> /// <returns></returns> public static async Task <SubjectStatus2> GetCollectionStatusAsync(string subjectId) { try { return(await BangumiHttpWrapper.GetCollectionStatusAsync(OAuthHelper.MyToken.Token, subjectId)); } catch (Exception e) { Debug.WriteLine("获取指定条目收藏信息失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 获取指定条目详情。 /// </summary> /// <param name="subjectId"></param> /// <returns></returns> public static async Task <Subject> GetSubjectAsync(string subjectId) { try { return(await BangumiHttpWrapper.GetSubjectAsync(subjectId)); } catch (Exception e) { Debug.WriteLine("获取指定条目详情失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 显示时间表。 /// </summary> /// <param name="bangumiCollection"></param> /// <returns></returns> public static async Task PopulateBangumiCalendarAsync(ObservableCollection <BangumiTimeLine> bangumiCollection, bool force = false) { try { //从文件反序列化 var PreCalendar = JsonConvert.DeserializeObject <List <BangumiTimeLine> >(await FileHelper.ReadFromCacheFileAsync("JsonCache\\calendar")); bangumiCollection.Clear(); if (PreCalendar != null) { foreach (var item in PreCalendar) { bangumiCollection.Add(item); } // 非强制加载,若缓存与当天为同一星期几则不请求新数据。 if (!force && bangumiCollection[0].Weekday.Id == GetDayOfWeek() + 1) { return; } } var bangumiCalendarList = await BangumiHttpWrapper.GetBangumiCalendarAsync(); //清空原数据 bangumiCollection.Clear(); int day = GetDayOfWeek(); foreach (var bangumiCalendar in bangumiCalendarList) { if (bangumiCalendar.Weekday.Id <= day) { bangumiCollection.Add(bangumiCalendar); } else { bangumiCollection.Insert(bangumiCollection.Count - day, bangumiCalendar); } } //将对象序列化并存储到文件 await FileHelper.WriteToCacheFileAsync(JsonConvert.SerializeObject(bangumiCollection), "JsonCache\\calendar"); } catch (Exception e) { Debug.WriteLine("显示时间表失败。"); Debug.WriteLine(e.Message); throw e; } }
/// <summary> /// 批量更新收视进度。 /// 使用 HttpWebRequest 提交表单进行更新,更新收藏状态使用相同方法。 /// </summary> /// <param name="ep"></param> /// <param name="status"></param> /// <param name="epsId"></param> /// <returns></returns> public static async Task <bool> UpdateProgressBatchAsync(int ep, EpStatusEnum status, string epsId) { try { return(await BangumiHttpWrapper.UpdateProgressBatchAsync(OAuthHelper.MyToken.Token, ep, status, epsId)); } catch (Exception e) { var msgDialog = new Windows.UI.Popups.MessageDialog("更新收藏状态失败!\n" + e.Message) { Title = "错误!" }; msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("确定")); await msgDialog.ShowAsync(); return(false); } }
/// <summary> /// 更新收藏状态 /// </summary> /// <param name="subjectId"></param> /// <param name="collectionStatusEnum"></param> /// <param name="comment"></param> /// <param name="rating"></param> /// <param name="privace"></param> /// <returns>更新是否成功</returns> public static async Task <bool> UpdateCollectionStatusAsync(string subjectId, CollectionStatusEnum collectionStatusEnum, string comment = "", string rating = "", string privace = "0") { try { return(await BangumiHttpWrapper.UpdateCollectionStatusAsync(OAuthHelper.MyToken.Token, subjectId, collectionStatusEnum, comment, rating, privace)); } catch (Exception e) { var msgDialog = new Windows.UI.Popups.MessageDialog("更新收藏状态失败!\n" + e.Message) { Title = "错误!" }; msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("确定")); await msgDialog.ShowAsync(); return(false); } }
/// <summary> /// 显示用户收视进度列表。 /// </summary> /// <param name="watchingListCollection"></param> /// <returns></returns> public static async Task PopulateWatchingListAsync(ObservableCollection <WatchingStatus> watchingListCollection) { try { //从文件反序列化 var PreWatchings = JsonConvert.DeserializeObject <List <WatchingStatus> >(await FileHelper.ReadFromCacheFileAsync("JsonCache\\home")); if (PreWatchings != null) { foreach (var sub in PreWatchings) { // 将Collection中没有的添加进去 if (watchingListCollection.Where(e => e.subject_id == sub.subject_id).Count() == 0) { watchingListCollection.Add(sub); } } } //else //{ // watchingListCollection.Clear(); //} var watchingList = await BangumiHttpWrapper.GetWatchingListAsync(OAuthHelper.MyToken.UserId); var deletedItems = new List <WatchingStatus>(); //标记要删除的条目 foreach (var sub in watchingListCollection) { //根据最新的进度删除原有条目 if (watchingList.Where(e => e.SubjectId == sub.subject_id).Count() == 0) { deletedItems.Add(sub); } } foreach (var item in deletedItems) //删除条目 { watchingListCollection.Remove(item); } foreach (var watching in watchingList) { //若在看含有原来没有的条目则新增,之后再细化 var item = watchingListCollection.Where(e => e.subject_id == watching.SubjectId).FirstOrDefault(); if (item == null) { WatchingStatus watchingStatus = new WatchingStatus(); watchingStatus.name = watching.Subject.Name; watchingStatus.name_cn = watching.Subject.NameCn; watchingStatus.image = watching.Subject.Images.Common; watchingStatus.subject_id = watching.SubjectId; watchingStatus.url = watching.Subject.Url; watchingStatus.ep_color = "Gray"; watchingStatus.lasttouch = 0; watchingStatus.watched_eps = watching.EpStatus.ToString(); watchingStatus.eps_count = watching.Subject.EpsCount.ToString(); watchingListCollection.Add(watchingStatus); } } foreach (var watching in watchingList) { var item = watchingListCollection.Where(e => e.subject_id == watching.SubjectId).FirstOrDefault(); if (item != null) { item.isUpdating = true; // 首次更新 if (item.lasttouch == 0) { // 获取EP详细信息 var subject = await BangumiHttpWrapper.GetSubjectEpsAsync(item.subject_id.ToString()); var progress = await BangumiHttpWrapper.GetProgressesAsync(OAuthHelper.MyToken.UserId, OAuthHelper.MyToken.Token, item.subject_id.ToString()); item.eps = new List <SimpleEp>(); foreach (var ep in subject.Eps.OrderBy(c => c.Type)) { SimpleEp simpleEp = new SimpleEp(); simpleEp.id = ep.Id; simpleEp.sort = ep.Sort; simpleEp.status = ep.Status; simpleEp.type = ep.Type; simpleEp.name = ep.NameCn == "" ? ep.Name : ep.NameCn; item.eps.Add(simpleEp); } if (item.eps.Where(e => e.status == "NA").Count() == 0) { item.eps_count = "全" + item.eps.Count + "话"; } else { item.eps_count = "更新到第" + (item.eps.Count - item.eps.Where(e => e.status == "NA").Count()) + "话"; } if (progress != null) { item.next_ep = progress.Eps.Count + 1; item.watched_eps = "看到第" + progress.Eps.Count + "话"; if (progress.Eps.Count < (item.eps.Count - item.eps.Where(e => e.status == "NA").Count())) { item.ep_color = "#d26585"; } else { item.ep_color = "Gray"; } foreach (var ep in item.eps) //用户观看状态 { foreach (var p in progress.Eps) { if (p.Id == ep.id) { ep.status = p.Status.CnName; progress.Eps.Remove(p); break; } } } } else { item.next_ep = 1; item.watched_eps = "尚未观看"; item.ep_color = "#d26585"; } item.lasttouch = watching.LastTouch; } else { // 对条目有 修改 或 当天更新 或 未知更新星期 的进行更新 if (item.lasttouch != watching.LastTouch || watching.Subject.AirWeekday == GetDayOfWeek() + 1 || watching.Subject.AirWeekday == 0) { var subject = await BangumiHttpWrapper.GetSubjectEpsAsync(item.subject_id.ToString()); item.eps.Clear(); foreach (var ep in subject.Eps) { SimpleEp simpleEp = new SimpleEp(); simpleEp.id = ep.Id; simpleEp.sort = ep.Sort; simpleEp.status = ep.Status; simpleEp.type = ep.Type; simpleEp.name = ep.NameCn == "" ? ep.Name : ep.NameCn; item.eps.Add(simpleEp); } if (item.eps.Where(e => e.status == "NA").Count() == 0) { item.eps_count = "全" + item.eps.Count + "话"; } else { item.eps_count = "更新到第" + (item.eps.Count - item.eps.Where(e => e.status == "NA").Count()) + "话"; } var progress = await BangumiHttpWrapper.GetProgressesAsync(OAuthHelper.MyToken.UserId, OAuthHelper.MyToken.Token, item.subject_id.ToString()); if (progress != null) { if (item.eps.Count == progress.Eps.Count) { item.next_ep = 0; } else { item.next_ep = progress.Eps.Count + 1; } item.watched_eps = "看到第" + progress.Eps.Count + "话"; if (progress.Eps.Count < (item.eps.Count - item.eps.Where(e => e.status == "NA").Count())) { item.ep_color = "#d26585"; } else { item.ep_color = "Gray"; } foreach (var ep in item.eps) //用户观看状态 { foreach (var p in progress.Eps) { if (p.Id == ep.id) { ep.status = p.Status.CnName; progress.Eps.Remove(p); break; } else { } } } } else { item.next_ep = 1; item.watched_eps = "尚未观看"; item.ep_color = "#d26585"; } item.lasttouch = watching.LastTouch; } } item.isUpdating = false; } } } catch (Exception e) { foreach (var item in watchingListCollection.Where(i => i.isUpdating == true)) { item.isUpdating = false; } Debug.WriteLine("显示用户收视进度列表失败。"); Debug.WriteLine(e.Message); throw e; } }