diff --git a/proto/category.pb.go b/proto/category.pb.go index 2237d88..2593277 100644 --- a/proto/category.pb.go +++ b/proto/category.pb.go @@ -29,12 +29,12 @@ const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package // - 游戏 game // - 移动应用 app // - 评论 review +// - 舞台剧 drama // - 小组 group (not supported) // - 日记 note (not supported) // - 图片 album (not supported) // - 小站 site (not supported) // - 同城活动 activity (not supported) -// - 舞台剧 drama (not supported) // - 豆品 thing (not supported) type Category int32 @@ -46,6 +46,7 @@ const ( Category_music Category = 4 Category_app Category = 5 Category_review Category = 6 + Category_drama Category = 7 ) var Category_name = map[int32]string{ @@ -56,6 +57,7 @@ var Category_name = map[int32]string{ 4: "music", 5: "app", 6: "review", + 7: "drama", } var Category_value = map[string]int32{ @@ -66,6 +68,7 @@ var Category_value = map[string]int32{ "music": 4, "app": 5, "review": 6, + "drama": 7, } func (x Category) String() string { @@ -83,16 +86,16 @@ func init() { func init() { proto.RegisterFile("proto/category.proto", fileDescriptor_1bfb247fa9b1cc73) } var fileDescriptor_1bfb247fa9b1cc73 = []byte{ - // 162 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x2c, 0x8e, 0xcd, 0xaa, 0xc2, 0x30, - 0x10, 0x46, 0xef, 0xb5, 0x3f, 0xb6, 0x03, 0xc2, 0x10, 0x7c, 0x03, 0x71, 0x23, 0xb4, 0x59, 0xf8, - 0x06, 0xfa, 0x12, 0xe2, 0x6e, 0x92, 0x86, 0x1a, 0x4a, 0x98, 0x92, 0x4e, 0x2a, 0x7d, 0x7b, 0x31, - 0xb8, 0x3a, 0x7c, 0xe7, 0xdb, 0x1c, 0x38, 0xce, 0x91, 0x85, 0xb5, 0x25, 0x71, 0x23, 0xc7, 0xad, - 0xcf, 0x53, 0x55, 0x19, 0x97, 0x07, 0x34, 0xf7, 0xdf, 0xa1, 0x0e, 0xd0, 0x9a, 0xc8, 0x34, 0x58, - 0x5a, 0x04, 0xff, 0x54, 0x03, 0xa5, 0x61, 0x9e, 0xf0, 0x5f, 0xb5, 0x50, 0x05, 0x5e, 0xbd, 0xc3, - 0xdd, 0x57, 0x8e, 0x14, 0x1c, 0x16, 0x59, 0xa6, 0xc5, 0x5b, 0x2c, 0xd5, 0x1e, 0x0a, 0x9a, 0x67, - 0xac, 0x14, 0x40, 0x1d, 0xdd, 0xea, 0xdd, 0x1b, 0xeb, 0xdb, 0xf9, 0x79, 0x1a, 0xbd, 0xbc, 0x92, - 0xe9, 0x2d, 0x07, 0xed, 0x65, 0xe9, 0xc2, 0xd6, 0x0d, 0x24, 0xa4, 0x07, 0x4e, 0x86, 0x26, 0x9d, - 0x03, 0x4c, 0x9d, 0x71, 0xfd, 0x04, 0x00, 0x00, 0xff, 0xff, 0x02, 0x83, 0xdc, 0xa7, 0xa6, 0x00, - 0x00, 0x00, + // 164 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x2c, 0x8e, 0x4d, 0x0a, 0xc2, 0x30, + 0x10, 0x46, 0xd5, 0xfe, 0x0f, 0x08, 0x43, 0xf0, 0x06, 0xe2, 0x46, 0x68, 0xb3, 0xf0, 0x06, 0x7a, + 0x0b, 0x77, 0x93, 0x1f, 0x6a, 0x28, 0x61, 0x4a, 0x9a, 0x54, 0x7a, 0x7b, 0x69, 0x70, 0xf5, 0xf8, + 0xde, 0xdb, 0x7c, 0x70, 0x99, 0x03, 0x47, 0x96, 0x9a, 0xa2, 0x1d, 0x39, 0x6c, 0x43, 0x9e, 0xa2, + 0xca, 0xb8, 0x6b, 0x68, 0x5f, 0xff, 0x20, 0xce, 0xd0, 0xa9, 0xc0, 0x64, 0x34, 0x2d, 0x11, 0x0f, + 0xa2, 0x85, 0x52, 0x31, 0x4f, 0x78, 0x14, 0x1d, 0x54, 0x9e, 0x57, 0x67, 0xf1, 0xb4, 0xcb, 0x91, + 0xbc, 0xc5, 0x22, 0xcb, 0xb4, 0x38, 0x8d, 0xa5, 0x68, 0xa0, 0xa0, 0x79, 0xc6, 0x4a, 0x00, 0xd4, + 0xc1, 0xae, 0xce, 0x7e, 0xb1, 0xde, 0xbb, 0x09, 0xe4, 0x09, 0x9b, 0xe7, 0xed, 0x7d, 0x1d, 0x5d, + 0xfc, 0x24, 0x35, 0x68, 0xf6, 0xd2, 0xc5, 0xa5, 0xf7, 0x5b, 0x6f, 0x28, 0x92, 0x34, 0x9c, 0x14, + 0x4d, 0x32, 0x7f, 0x51, 0x75, 0xc6, 0xe3, 0x17, 0x00, 0x00, 0xff, 0xff, 0x60, 0xa6, 0xb8, 0x02, + 0xb1, 0x00, 0x00, 0x00, } diff --git a/proto/category.proto b/proto/category.proto index 5c43c31..1f99a89 100644 --- a/proto/category.proto +++ b/proto/category.proto @@ -13,12 +13,12 @@ option go_package = "github.com/its-my-data/doubak/proto"; // - 游戏 game // - 移动应用 app // - 评论 review +// - 舞台剧 drama // - 小组 group (not supported) // - 日记 note (not supported) // - 图片 album (not supported) // - 小站 site (not supported) // - 同城活动 activity (not supported) -// - 舞台剧 drama (not supported) // - 豆品 thing (not supported) enum Category { broadcast = 0; @@ -28,4 +28,5 @@ enum Category { music = 4; app = 5; review = 6; + drama = 7; } diff --git a/task/collector.go b/task/collector.go index 395ad9c..642abcf 100644 --- a/task/collector.go +++ b/task/collector.go @@ -11,6 +11,7 @@ import ( "log" "path/filepath" "regexp" + "sort" "strconv" "strings" "time" @@ -27,6 +28,7 @@ const PeopleURL = DoubanURL + "people/" const MoviePeopleURL = MovieURL + "people/" const BookPeopleURL = BookURL + "people/" const MusicPeopleURL = MusicURL + "people/" +const LocationPeopleURL = DoubanURL + "location/people/" const startingPage = 1 const startingItemId = 0 @@ -101,6 +103,9 @@ func (task *Collector) Execute() error { case proto.Category_music.String(): task.crawlMusicListDispatcher() task.crawlItemDetails(proto.Category_music, "div.item > div.info > ul > li.title > a:nth-child(1)") + case proto.Category_drama.String(): + task.crawlDramaListDispatcher() + task.crawlItemDetails(proto.Category_drama, "div.item > div.info > ul > li.title > a:nth-child(1)") default: return errors.New("Category not implemented " + c) } @@ -409,6 +414,50 @@ func (task *Collector) crawlMusicLists(totalItems int, tag string, urlAction str return task.crawlItemLists(proto.Category_music, totalItems, pageStep, tag, urlTemplate) } +func (task *Collector) crawlDramaListDispatcher() error { + // The drama page does not have an entry (https://www.douban.com/location/people//drama/). + // However, each page contains the following parts: + // - To-watch dramas. + // - Watched dramas. + + // Drama list starts with item ID (which is 0). Each page has 15 items. Example: + // https://www.douban.com/location/people/mewcatcher/drama/collect?sort=time&start=0&filter=all&mode=grid&tags_sort=count + nToWatch := 0 + nWatched := 0 + c := util.NewColly() + c.OnHTML("div.article > div.mod > h2", func(e *colly.HTMLElement) { + secText := e.Text + re := regexp.MustCompile("[0-9]+") + nParsed, _ := strconv.Atoi(re.FindString(secText)) + + switch { + case strings.Contains(secText, "想看"): + nToWatch = nParsed + log.Println("Found to-watch dramas:", nToWatch) + case strings.Contains(secText, "看过"): + nWatched = nParsed + log.Println("Found watched dramas:", nWatched) + default: + log.Println("Ignoring:", util.MergeSpaces(&secText)) + } + }) + c.Visit(LocationPeopleURL + task.user + "/drama/") + + if err := task.crawlDramaLists(nWatched, "watched", "collect"); err != nil { + return err + } + if err := task.crawlDramaLists(nToWatch, "towatch", "wish"); err != nil { + return err + } + return nil +} + +func (task *Collector) crawlDramaLists(totalItems int, tag string, urlAction string) error { + const pageStep = 15 + urlTemplate := fmt.Sprintf("https://www.douban.com/location/people/%s/drama/%s?sort=time&start=%%d&filter=all&mode=grid&tags_sort=count", task.user, urlAction) + return task.crawlItemLists(proto.Category_drama, totalItems, pageStep, tag, urlTemplate) +} + // TODO: implement more crawlers. // crawlItemLists downloads an item list universally. @@ -452,7 +501,8 @@ func (task *Collector) crawlItemLists(cat proto.Category, totalItems int, pageSt } func (task *Collector) crawlItemDetails(cat proto.Category, selector string) error { - var urls []string + // Run statistical URL counter. + urlCounter := make(map[string]int) inputFileNamePattern := fmt.Sprintf("*_%s_*.html", cat) files := util.GetFilePathListWithPattern(task.outputDir, inputFileNamePattern) for _, fn := range files { @@ -466,10 +516,17 @@ func (task *Collector) crawlItemDetails(cat proto.Category, selector string) err if !exists { log.Fatal("Found item without link", sel.Text()) } - urls = append(urls, url) + urlCounter[url]++ }) } + // Convert map to sorted slices to make resuming idempotent. + var urls []string + for url := range urlCounter { + urls = append(urls, url) + } + sort.Strings(urls) + // Hack around to continue progress. Set to the last downloaded progress count (1-based, 0 by default). // This hack will continue with the next URL in the queue. const iResume = 0 @@ -542,7 +599,7 @@ func (task *Collector) getItemMatcherPattern(cat proto.Category) string { switch cat { case proto.Category_book: return "class=\"subject-item\"" - case proto.Category_movie, proto.Category_music: + case proto.Category_movie, proto.Category_music, proto.Category_drama: return "class=\"item\"" case proto.Category_game: return "class=\"common-item\"" diff --git a/util/crawlers.go b/util/crawlers.go index 6665ecf..b0638af 100644 --- a/util/crawlers.go +++ b/util/crawlers.go @@ -19,7 +19,7 @@ const RequestInterval = 1 * time.Second // TODO: add a rate limiter. func NewQueue() *queue.Queue { q, err := queue.New( - 1, // Number of consumer threads + 1, // Number of consumer threads &queue.InMemoryQueueStorage{MaxSize: 10000}, // Use default queue storage ) if err != nil { @@ -42,7 +42,7 @@ func NewColly() *colly.Collector { c := colly.NewCollector( colly.MaxDepth(1), - colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"), + colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.61"), ) c.OnError(func(r *colly.Response, err error) { @@ -64,6 +64,7 @@ func NewColly() *colly.Collector { r.Headers.Set("Cookie", cookies) } + r.Headers.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9") r.Headers.Set("Referer", "https://www.douban.com/") r.Headers.Set("Host", "https://www.douban.com/") })