public override DataContainer ParseHandler(DataResponse dr) { var html = dr.Text; //书的名称 var name = parseTitle.Match(html).Groups[1].Value; var baseInfoHtml = parseBaseInfoBody.Match(html).Value; //出版日期, 没有出版日期说明不是书籍页面 #region 日期处理 var dateMatch = parsePublishDate.Match(baseInfoHtml).Groups; var dateString = string.IsNullOrWhiteSpace(dateMatch[1].Value) ? dateMatch[2].Value : dateMatch[1].Value; if (string.IsNullOrWhiteSpace(dateString)) { throw new ParseErrorException("This is not a Book Page"); } DateTime publishDate; try { publishDate = DateTime.Parse(dateString); } catch (Exception) { throw new ParseErrorException("This is not a Book Page"); } #endregion //链接 var hrefs = parseHref.Matches(baseInfoHtml).ForEach((Match m) => new Hyperlink(m.Groups[1].Value, m.Groups[2].Value)); //介绍 var introduction = parseIntroduction.Match(baseInfoHtml).Groups[1].Value; var metaHtml = parseMetaBody.Match(html).Value; //分类 var categorys = parseCategory.Matches(metaHtml).ForEach((Match m) => m.Groups[1].Value); //标签 var tags = parseTag.Matches(metaHtml).ForEach((Match m) => m.Groups[1].Value); //生成Book对象 var book = new Book(name, publishDate, hrefs.ElementAt(0).Href) { Introduction = introduction }; book.Hrefs.AddRange(hrefs.Skip(1)); book.Categorys.AddRange(categorys); book.Tags.AddRange(tags); Logger.Info($"Parse book: {book.Name}"); return new DataContainer(dr.GUID, book); }
public Book GetBook(long bookId) { using (var r = DB.ExecuteReaderOne("select * from book where book.id=?", bookId)) { if (r == null) { return null; } var book = new Book(r.GetString(1), r.GetDateTime(2), r.GetString(3)) { Id = r.GetInt64(0) }; book.Hrefs.AddRange(GetHrefs(book.Id)); book.Categorys.AddRange(GetCategorys(book.Id)); book.Tags.AddRange(GetTags(book.Id)); return book; } }
public long Insert(Book book) { //添加book的基本信息 var bookId = DB.ExecuteScalar<long>("insert into book values(NULL,?,date(?),?,?); select last_insert_rowid()", book.Name, book.PublishDate, book.OfficialHref, book.Introduction); //添加book的标签、分类和超链接 book.Hrefs.ForEach(href => AddHrefToBook(bookId, href)); book.Categorys.ForEach(category => AddCategoryToBook(bookId, category)); book.Tags.ForEach(tag => AddTagToBook(bookId, tag)); Logger.Debug($"Insert book {book.Name} successful"); return bookId; }
public bool Equals(Book other) { return this.Id == other.Id; }