package main

import (
	"context"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/runtime"
	"github.com/chromedp/chromedp"
	"io/ioutil"
	"log"
	"math"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"strings"
	_ "sync"
	"time"
)


type fuser struct {
	Nickname string
	Avatar string
	gender string
}
//var url = flag.String("l","", "微博链接")

func open(url string, index int) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err) // 这里的err其实就是panic传入的内容
		}
	}()
	options := []chromedp.ExecAllocatorOption{
		chromedp.Flag("headless",true),
		chromedp.Flag("blink-settings","imageEnable=false"),
		chromedp.UserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko)`),
	}
	c,_ := chromedp.NewExecAllocator(context.Background(),options...)
	chromeCtx, cancel := chromedp.NewContext(c,chromedp.WithLogf(log.Printf))
	_ = chromedp.Run(chromeCtx, make([]chromedp.Action, 0, 1)...)
	timeOutCtx, cancel := context.WithTimeout(chromeCtx, 6000* time.Second)
	defer cancel()
	var htmlContent string
	var totalString string;
	fmt.Println("准备打开页面", url)
	chromedp.Run(timeOutCtx,
		chromedp.Navigate(url),
		chromedp.WaitVisible(`div[node-type="comment_list"]`),
		chromedp.OuterHTML(`span[node-type=comment_btn_text] > span > em:last-child`, &totalString, chromedp.ByQuery),
		chromedp.ActionFunc(func(ctx context.Context) error {
			totalString = strings.Replace(totalString, "<em>", "", 1)
			totalString = strings.Replace(totalString, "</em>", "", 1);
			totalNum,_ := strconv.ParseInt(totalString,10, 64)
			var totalPage int;
			if (totalNum > 0)  {
				totalPage = f2i(math.Floor(float64(totalNum/15)))
				if totalPage > 30 {
					totalPage = 30
				}
			} else {
				totalPage = 30
			}
			chromedp.ScrollIntoView("div.WB_footer", chromedp.ByQuery).Do(ctx);
			time.Sleep(2 * time.Second)
			chromedp.ScrollIntoView("div.WB_footer", chromedp.ByQuery).Do(ctx);
			for  i:= 0 ; i < totalPage ; i++ {
				fmt.Println("开始翻页...", i)
				chromedp.Query(`a[action-type="click_more_comment"]`, chromedp.AtLeast(0), chromedp.After(func(ctx context.Context, id runtime.ExecutionContextID, node ...*cdp.Node) error {
					chromedp.Click(node).Do(ctx)
					return nil;
				}))

				time.Sleep(1 * time.Second)
			}
			return nil
		}),
		chromedp.OuterHTML(`document.querySelector("body")`, &htmlContent, chromedp.ByJSPath),
	)
	getUserInfo(htmlContent, index)
}

func f2i(f float64) int {
	i, _ := strconv.Atoi(fmt.Sprintf("%1.0f", f))
	return i
}

func saveUser(users []*fuser, index int) {
	path := "/home/code001/data/users.txt"
	avatars :=  "/home/code001/data/avatar/"

	file,_ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
	for _,user := range users {
		file.Write([]byte(user.Nickname + " " + user.gender + "\r\n"))
		saveImage(user.Avatar, avatars + user.Nickname + ".jpeg")
	}
	file.Close();
//	wg.Done()
}

func saveImage(src string, path string) {
	file,_ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
	res,_ := http.Get(src)
	defer res.Body.Close()
	data,_ := ioutil.ReadAll(res.Body)
	file.Write(data)
	file.Close()
}

func getUserInfo(html string, index int) {
	doc,_ := goquery.NewDocumentFromReader(strings.NewReader(html))
	var users []*fuser;
	doc.Find("WB_face")
	doc.Find("div.list_ul > div").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		img := s.Find("div.WB_face > a > img");
		nickname,_ :=  img.Attr("alt");
		avatar,_ :=  img.Attr("src");
		if nickname != "" && avatar != "" {
			users = append(users, &fuser{
				Nickname: nickname,
				Avatar:   avatar,
				gender:   "未知",
			})
		}
	})
	saveUser(users , index);
}


//var wg *sync.WaitGroup

func init() {
	//wg = new(sync.WaitGroup)
}

func RandomInt(start int,end int) int{
	rand.Seed(time.Now().UnixNano())
	random:=rand.Intn(end-start)
	random = start + random
	return random
}

func main() {
	//flag.Parse();
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err) // 这里的err其实就是panic传入的内容
		}
	}()
	urls := []string {
		"https://weibo.com/1618051664/JF9nSmV3n?filter=hot&page_source=hot&root_comment_id=0",
		"https://weibo.com/5634100124/JDIIu47rJ?filter=hot&root_comment_id=0&type=comment",
		"https://weibo.com/1609953534/JF9s1b6wW?filter=hot&root_comment_id=0&type=comment",
		"https://weibo.com/2656274875/JFdQC8TIz?filter=hot&root_comment_id=0",
		"https://weibo.com/1618051664/JF8TP0Kv4?filter=hot&root_comment_id=0",

	}

	for i := 0; i < len(urls); i++ {
	//	wg.Add(1)
		 open(urls[i], RandomInt(1000000, 9000000));
	}
//	wg.Wait()
	fmt.Println("finished")
}

