package main

import (
	"fmt"
	"strconv"
	"strings"

	"time"

	"github.com/PuerkitoBio/goquery"
	"gonum.org/v1/plot"
	"gonum.org/v1/plot/plotter"

	//	"gonum.org/v1/plot/plotutil"
	"gonum.org/v1/plot/vg"
)

var BookNameDB []string
var CommentDB []string
var UrlArry []string

type Data struct {
	BookName string
	Comments int
}

func GetNetDate(url string) []string {
	var UrlArry []string
	var Num int = 20
	First_page_tag := "tag/散文"
	UrlArry = append(UrlArry, url+First_page_tag)
	New_page_tag := "tag/散文?start=" + strconv.Itoa(Num) + "&type=T"
	UrlArry = append(UrlArry, url+New_page_tag)
Loop:
	for {
		Num = Num + 20
		New_page_tag := "tag/散文?start=" + strconv.Itoa(Num) + "&type=T"
		rsp, _ := goquery.NewDocument("https://book.douban.com/" + New_page_tag)
		Flag := false
		rsp.Find("p:contains(没有找到符合条件的图书)").Each(func(i int, selection *goquery.Selection) {
			if selection.Text() == "没有找到符合条件的图书" {
				Flag = true
			}
		})
		if Flag == true {
			return UrlArry
			break Loop
		}
		UrlArry = append(UrlArry, url+New_page_tag)
		fmt.Println("dindin")
	}
	return UrlArry
}
func main() {
	go func() {
		var i int = 1
		for {
			time.Sleep(1 * time.Second)
			i = i + 1
			fmt.Println(i)
		}
	}()
	UrlArry := GetNetDate("https://book.douban.com/")
	for _, v := range UrlArry {
		fmt.Println(v)
		DouBanrsp, _ := goquery.NewDocument(v)
		DouBanrsp.Find("a").Each(func(i int, selection *goquery.Selection) {

			if BookTitle, ok := selection.Attr("title"); ok == true {
				BookNameDB = append(BookNameDB, "<<"+BookTitle+">>")
			}
		})
		DouBanrsp.Find("span").Each(func(i int, selection *goquery.Selection) {

			if Value, ok := selection.Attr("class"); ok == true {
				if Value == "pl" {
					CommentDB = append(CommentDB, strings.Replace(strings.Replace(selection.Text(), "\n", "", -1), " ", "", -1))
				}
			}
		})
	}
	IntCommentDB := make([]int, len(CommentDB))
	for i, value := range CommentDB {
		StrComment := strings.Replace(strings.Replace(value, "人评价)", "", -1), "(", "", -1)
		IntCommentDB[i], _ = strconv.Atoi(StrComment)
	}
	SaveData := make([]Data, len(BookNameDB))
	for i, _ := range SaveData {
		SaveData[i].BookName = BookNameDB[i]
		SaveData[i].Comments = IntCommentDB[i]
	}
	for _, v := range SaveData {
		fmt.Println(v)
	}
}

func Graphy() {
	p, _ := plot.New()
	p.Title.Text = "Histogram"

	bins := plotter.XYs{
		{10, 10},
		{5, 5},
	}
	label := []string{"a", "b"}
	bins1 := plotter.XYLabels{
		bins,
		label,
	}
	h, _ := plotter.NewHistogram(bins1, 2)
	p.Add(h)

	p.Save(4*vg.Inch, 4*vg.Inch, "histogram.png")
}
01-09 23:16