Пример #1
0
        private async Task MarkEpWatched(string episodeId)
        {
            try
            {
                BangumiApi.Init(
                    Constants.ClientId,
                    Constants.ClientSecret,
                    Constants.RedirectUrl,
                    ApplicationData.Current.LocalFolder.Path,
                    ApplicationData.Current.LocalCacheFolder.Path,
                    EncryptionHelper.EncryptionAsync,
                    EncryptionHelper.DecryptionAsync);

                await BangumiApi.BgmApi.UpdateProgress(episodeId, Api.Models.EpStatusType.watched);
            }
            catch (Exception e)
            {
                ToastNotificationHelper.Toast("操作失败", e.Message, "重试", _queryString);
            }
        }
Пример #2
0
        /// <summary>
        /// The Run method is the entry point of a background task.
        /// </summary>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background " + taskInstance.Task.Name + " Starting...");

            // Query BackgroundWorkCost
            // Guidance: If BackgroundWorkCost is high, then perform only the minimum amount
            // of work in the background task and return immediately.
            var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;

            // Associate a cancellation handler with the background task.
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

            // Get the deferral object from the task instance, and take a reference to the taskInstance;
            _deferral     = taskInstance.GetDeferral();
            _taskInstance = taskInstance;

            try
            {
                BangumiApi.Init(
                    Constants.ClientId,
                    Constants.ClientSecret,
                    Constants.RedirectUrl,
                    ApplicationData.Current.LocalFolder.Path,
                    ApplicationData.Current.LocalCacheFolder.Path,
                    EncryptionHelper.EncryptionAsync,
                    EncryptionHelper.DecryptionAsync);

                await BangumiApi.BgmOAuth.CheckToken();

                if (SettingHelper.EnableBangumiAirToast)
                {
                    // 初始化 BangumiData 对象
                    BangumiData.Init(Path.Combine(ApplicationData.Current.LocalFolder.Path, "bangumi-data"),
                                     SettingHelper.UseBiliApp);

                    if (!BangumiApi.BgmCache.IsUpdatedToday)
                    {
                        // 加载缓存,后面获取新数据后比较需要使用
                        var cachedWatchings = BangumiApi.BgmCache.Watching();

                        // 加载新的收视进度
                        var newWatching = await BangumiApi.BgmApi.Watching();

                        var subjectTasks  = new List <Task <SubjectLarge> >();
                        var progressTasks = new List <Task <Progress> >();
                        // 新的收视进度与缓存的不同或未缓存的条目
                        var watchingsNotCached = BangumiApi.BgmCache.IsUpdatedToday ?
                                                 newWatching.Where(it => cachedWatchings.All(it2 => !it2.EqualsExT(it))).ToList() :
                                                 newWatching;
                        using (var semaphore = new SemaphoreSlim(10))
                        {
                            foreach (var item in watchingsNotCached)
                            {
                                await semaphore.WaitAsync();

                                subjectTasks.Add(BangumiApi.BgmApi.SubjectEp(item.SubjectId.ToString())
                                                 .ContinueWith(t =>
                                {
                                    semaphore.Release();
                                    return(t.Result);
                                }));
                                await semaphore.WaitAsync();

                                progressTasks.Add(BangumiApi.BgmApi.Progress(item.SubjectId.ToString())
                                                  .ContinueWith(t =>
                                {
                                    semaphore.Release();
                                    return(t.Result);
                                }));
                            }
                            await Task.WhenAll(subjectTasks);

                            await Task.WhenAll(progressTasks);
                        }
                        BangumiApi.BgmCache.IsUpdatedToday = true;
                        await BangumiApi.BgmCache.WriteToFile();
                    }

                    ToastNotificationHelper.RemoveAllScheduledToasts();
                    foreach (var item in CachedWatchProgress())
                    {
                        await item.ScheduleToast();
                    }
                }
            }
            catch (BgmUnauthorizedException e)
            {
                // 取消所有后台任务
                foreach (var cur in BackgroundTaskRegistration.AllTasks)
                {
                    cur.Value.Unregister(true);
                }
                ToastNotificationHelper.Toast("后台任务", "用户认证过期,后台任务已取消。");
                Debug.WriteLine(e.StackTrace);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
            finally
            {
                _deferral.Complete();
            }

            IEnumerable <WatchProgress> CachedWatchProgress()
            {
                foreach (var watching in BangumiApi.BgmCache.Watching())
                {
                    var subject  = BangumiApi.BgmCache.Subject(watching.SubjectId.ToString());
                    var progress = BangumiApi.BgmCache.Progress(watching.SubjectId.ToString());

                    var item = WatchProgress.FromWatching(watching);
                    item.ProcessEpisode(subject);
                    item.ProcessProgress(progress);
                    if (subject == null || progress == null)
                    {
                        // 标记以重新加载
                        watching.Subject.Eps = -1;
                    }
                    yield return(item);
                }
            }
        }