package engine

import (
	"learngo/crawler/fetcher"
	"log"
)

func Run(seeds ...Request) {
	var requests []Request // requet 队列
	// 请求加入队列
	for _, r := range seeds {
		requests = append(requests, r)
	}
	for len(requests) > 0 {
		r := requests[0]        // 获取第一个请求request
		requests = requests[1:] // 截取
		log.Printf(" parserUrl :%s", r.Url)
		body, error := fetcher.Fetch(r.Url)

		if error != nil {
			log.Printf("Fetcher :error fetcher url %s : %v", r.Url, error)
			continue

		}
		parserResult := r.ParserFunc(body) // body 传给解析器，返回解析结果
		requests = append(requests,
			parserResult.Requests...)
		//打印解析后的item 城市名称
		for _, item := range parserResult.Items {
			log.Printf(" got item %v", item)
		}
	}
}
