Beispiel #1
0
        public void Insert(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.Hrefs.ForEach(href => AddHrefToBook(bookId, href));
            book.Categorys.ForEach(category => AddCategoryToBook(bookId, category));
            book.Tags.ForEach(tag => AddTagToBook(bookId, tag));
        }
Beispiel #2
0
        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);

            return new DataContainer(dr.GUID, book);
        }
Beispiel #3
0
 public bool Equals(Book other)
 {
     return this.Id == other.Id;
 }
Beispiel #4
0
        public Book GetBook(int bookId)
        {
            using (var r = DB.ExecuteReaderOne("select * from book where book.id=?", 1))
            {
                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;
            }
        }