private async Task <bool> _RowAction(IGRRow _Row) { GRRow <BookDisplay> Row = (GRRow <BookDisplay>)_Row; BookInstruction Payload = ( BookInstruction )Row.Source.Payload; if (Payload != null) { // Save the book here Payload.SaveInfo(); // Reload the BookDisplay as Entry might changed from SaveInfo Row.Source = new BookDisplay(Payload.Entry); } SpiderBook Item = await SpiderBook.CreateSAsync(Row.Source.Entry.ZoneId, Row.Source.Entry.ZItemId, Payload?.BookSpiderDef); Item.PropertyChanged += Item_PropertyChanged; XParameter Metadata = Item.PSettings.Parameter("METADATA") ?? new XParameter("METADATA"); Metadata.SetValue(new XKey("payload", Row.Source.Entry.Meta[AppKeys.GLOBAL_SSID])); Item.PSettings.SetParameter(Metadata); if (!Item.ProcessSuccess && Item.CanProcess) { await ItemProcessor.ProcessLocal(Item); } Item.PropertyChanged -= Item_PropertyChanged; (( BookDisplayPageExt )Extension).OpenItem(_Row); return(true); }
private async void LoadInstruction(BookInstruction B, bool useCache) { if (!BookInstruction.OpLocks.AcquireLock(B.GID, out AsyncLocks <string, bool> .QueueToken QT)) { await QT.Task; } SpiderBook SBook = await SpiderBook.CreateSAsync(B.ZoneId, B.ZItemId, B.BookSpiderDef); if (Shared.Storage.FileExists(SBook.MetaLocation)) { B.LastCache = Shared.Storage.FileTime(SBook.MetaLocation).LocalDateTime; } if (useCache && (B.Packed == true || Shared.BooksDb.Volumes.Any(x => x.Book == B.Entry))) { if (B.Packed != true) { B.PackSavedVols(SBook.PSettings); } } else { bool UpdateFailed = true; await SBook.Process(); if (SBook.Processed && SBook.ProcessSuccess) { B.LastCache = DateTime.Now; BookInstruction BUpdate = SBook.GetBook(); if (BUpdate.Packable && BUpdate.Packed != true) { BUpdate.PackVolumes(SBook.GetPPConvoy()); B.Update(BUpdate); UpdateFailed = false; } } // Cannot download content, use cache if available if (UpdateFailed && Shared.BooksDb.Volumes.Any(x => x.Book == B.Entry)) { Logger.Log(ID, "Spider failed to produce instructions, using cache instead", LogType.WARNING); B.PackSavedVols(SBook.PSettings); } } QT.TrySetResult(true); OnComplete(B); }
private async void PinSpider(object sender, RoutedEventArgs e) { string TileId = await PageProcessor.PinToStart(ThisBook); if (!string.IsNullOrEmpty(TileId)) { PinManager PM = new PinManager(); PM.RegPin(ThisBook, TileId, true); SpiderBook SpDef = await SpiderBook.CreateSAsync(ThisBook.ZItemId); await PageProcessor.RegLiveSpider(SpDef, ( BookInstruction )ThisBook, TileId); } }
private async void Edit_Click(object sender, RoutedEventArgs e) { object DataContext = (( FrameworkElement )sender).DataContext; if (DataContext is GRRow <BookDisplay> BkRow) { BookItem BkItem = ItemProcessor.GetBookItem(BkRow.Source.Entry); SpiderBook SBk = await SpiderBook.CreateSAsync(BkItem.ZoneId, BkItem.ZItemId, null); if (SBk.CanProcess) { ControlFrame.Instance.NavigateTo(PageId.PROC_PANEL, () => new ProcPanelWrapper(SBk.MetaLocation)); } } }
private async void PinToStart_Click(object sender, RoutedEventArgs e) { if ((( FrameworkElement )sender).DataContext is GRRow <BookDisplay> BkRow) { BookItem BkItem = ItemProcessor.GetBookItem(BkRow.Source.Entry); string TileId = await PageProcessor.PinToStart(BkItem); if (!string.IsNullOrEmpty(TileId)) { PinManager PM = new PinManager(); PM.RegPin(BkItem, TileId, true); if (BkItem is BookInstruction BInst) { SpiderBook SBook = await SpiderBook.CreateSAsync(BkItem.ZoneId, BkItem.ZItemId, null); await PageProcessor.RegLiveSpider(SBook, BInst, TileId); } } } }
private async void PredefineFile(string ZoneId, string ZItemId) { SelectedBook = await SpiderBook.CreateSAsync(ZoneId, ZItemId, null); FileName.Text = SelectedBook.MetaLocation; }
private async Task <Book> MigrateBookSpider(string SRoot, string ZoneId, string ZItemId) { if (!Shared.Storage.FileExists(SRoot + "METADATA.xml")) { return(null); } XRegistry MetaDefs = new XRegistry("<metadata />", SRoot + "METADATA.xml"); XParameter MetaParam = MetaDefs.Parameter("METADATA"); BookInstruction BkInst = new BookInstruction(ZoneId, ZItemId); BkInst.Title = MetaParam.GetValue("Title"); BkInst.Info.Author = MetaParam.GetValue("Author"); BkInst.Info.Press = MetaParam.GetValue("Press"); BkInst.Info.LastUpdateDate = MetaParam.GetValue("LastUpdateDate"); BkInst.Info.TotalHitCount = MetaParam.GetValue("TotalHitCount"); BkInst.Info.DailyHitCount = MetaParam.GetValue("TodayHitCount"); BkInst.Info.PushCount = MetaParam.GetValue("PushCount"); BkInst.Info.FavCount = MetaParam.GetValue("FavCount"); BkInst.Info.Length = MetaParam.GetValue("Length"); BkInst.Info.LatestSection = MetaParam.GetValue("LatestSection"); BkInst.Info.LongDescription = MetaParam.GetValue("Intro"); Book Entry = BkInst.Entry; await Shared.BooksDb.LoadCollectionAsync(Entry, x => x.Volumes, x => x.Index); Entry.Volumes.Clear(); XRegistry TOCDefs = new XRegistry("<metadata />", SRoot + "/" + "toc.txt"); int vi = 0; XParameter VolParam = TOCDefs.Parameter("VolInst" + vi); while (VolParam != null) { Volume Vol = new Volume() { Book = Entry, Title = VolParam.GetValue("Title"), Index = VolParam.GetSaveInt("Index"), Chapters = new List <Chapter>() }; Vol.Meta["ProcId"] = VolParam.GetValue("ProcId"); Vol.Meta[AppKeys.GLOBAL_VID] = Utils.Md5(Vol.Title); XParameter PParam = VolParam.Parameter("0"); for (int p = 1; PParam != null; p++) { Vol.Meta["P" + PParam.Id] = PParam.GetValue("Value"); PParam = VolParam.Parameter(p.ToString()); } string MVolHash = Utils.Md5(Vol.Title); int ei = 0; XParameter ChParam = VolParam.Parameter("EpInst" + ei); while (ChParam != null) { Chapter Ch = new Chapter() { Book = Entry, Volume = Vol, Title = ChParam.GetValue("Title"), Index = ChParam.GetSaveInt("Index") }; Ch.Meta["ProcId"] = ChParam.GetValue("ProcId"); Ch.Meta[AppKeys.GLOBAL_CID] = Utils.Md5(Ch.Title); string MChHash = Utils.Md5(Ch.Title); PParam = ChParam.Parameter("0"); for (int p = 1; PParam != null; p++) { Ch.Meta["P" + PParam.Id] = PParam.GetValue("Value"); PParam = ChParam.Parameter(p.ToString()); } Vol.Chapters.Add(Ch); string ChLocation = SRoot + MVolHash + "/" + MChHash + ".txt"; if (Shared.Storage.FileExists(ChLocation)) { ChapterContent ChCont = new ChapterContent() { Chapter = Ch, }; ChCont.Data.BytesValue = Shared.Storage.GetBytes(ChLocation); Shared.BooksDb.ChapterContents.Add(ChCont); } ChParam = VolParam.Parameter("EpInst" + (++ei)); } Entry.Volumes.Add(Vol); VolParam = TOCDefs.Parameter("VolInst" + (++vi)); } SpiderBook SBk = await SpiderBook.CreateSAsync(ZoneId, ZItemId, MetaDefs.Parameter("Procedures")); // Preserve the process state XParameter ProcState = MetaDefs.Parameter("ProcessState"); if (ProcState != null) { SBk.PSettings.SetParameter(ProcState); } SBk.PSettings.Save(); return(Entry); }
private async Task UpdateSpiders() { try { XReg.SetParameter(TASK_START, CustomAnchor.TimeKey); XReg.Save(); IEnumerable <XParameter> Updates; List <string> Exists = new List <string>(); if (Retrying) { Updates = XReg.Parameters(AppKeys.BTASK_SPIDER).Where(x => { int r = x.GetSaveInt(AppKeys.BTASK_RETRY); return(0 < r && r < MaxRetry); }); } else { Updates = XReg.Parameters(AppKeys.BTASK_SPIDER).Where(x => { int r = x.GetSaveInt(AppKeys.BTASK_RETRY); if (r == 0 || MaxRetry <= r) { return(true); } else { // Consider Retry Timer dead if LastUpdate is 20 < minutes DateTime LastRun = DateTime.FromFileTimeUtc(x.GetSaveLong(AppKeys.LBS_TIME)); return(30 < DateTime.Now.Subtract(LastRun).TotalMinutes); } }); } foreach (XParameter UpdateParam in Updates) { string TileId = UpdateParam.GetValue("tileId"); if (!SecondaryTile.Exists(TileId)) { UpdateParam.SetValue(new XKey[] { new XKey(AppKeys.SYS_EXCEPTION, "App Tile is missing") , CustomAnchor.TimeKey }); XReg.SetParameter(UpdateParam); continue; } string[] Keys = UpdateParam.Id.Split('.'); if (Keys.Length != 3) { XReg.RemoveParameter(UpdateParam.Id); continue; } SpiderBook SBook = await SpiderBook.CreateSAsync(Keys[0], Keys[2], null); if (!SBook.CanProcess) { XReg.RemoveParameter(UpdateParam.Id); continue; } SBook.MarkUnprocessed(); string OHash = null, NHash = null; SBook.GetBook()?.Entry.Meta.TryGetValue("TOCHash", out OHash); await ItemProcessor.ProcessLocal(SBook); if (SBook.ProcessSuccess) { BookInstruction NBook = SBook.GetBook(); NBook?.Entry.Meta.TryGetValue("TOCHash", out NHash); if (NBook.Packed == true && (NBook.NeedUpdate || OHash != NHash)) { await LiveTileService.UpdateTile(CanvasDevice, NBook, TileId); } UpdateParam.SetValue(new XKey[] { new XKey(AppKeys.SYS_EXCEPTION, false) , new XKey(AppKeys.BTASK_RETRY, 0) , CustomAnchor.TimeKey }); } else { CreateRetryTimer(); int NRetries = UpdateParam.GetSaveInt(AppKeys.BTASK_RETRY); UpdateParam.SetValue(new XKey[] { new XKey(AppKeys.SYS_EXCEPTION, true) , new XKey(AppKeys.BTASK_RETRY, NRetries + 1) , CustomAnchor.TimeKey }); } XReg.SetParameter(UpdateParam); XReg.Save(); } XReg.SetParameter(TASK_END, CustomAnchor.TimeKey); XReg.Save(); } catch (Exception ex) { try { XReg.SetParameter(AppKeys.SYS_EXCEPTION, new XKey(AppKeys.SYS_MESSAGE, ex.Message)); XReg.Save(); } catch (Exception) { } } }