add twitcasting test

This commit is contained in:
eson 2020-08-04 14:13:39 +08:00
parent 826d15876a
commit 6158976986
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1 @@
package main

View File

@ -0,0 +1,56 @@
package main
import (
"github.com/474420502/focus/compare"
"github.com/474420502/focus/tree/heap"
"log"
"testing"
"github.com/474420502/requests"
"github.com/lestrrat-go/libxml2"
)
func TestMain(t *testing.T) {
searchurl := "https://twitcasting.tv/rankingindex.php"
queuedict := make(map[string]bool)
queue := heap.New(compare.String)
queue.Put(searchurl)
queuedict[searchurl] = true
for surl, ok := queue.Pop(); ok; surl, ok = queue.Pop() {
ses := requests.NewSession()
resp, err := ses.Get(surl.(string)).Execute()
if err != nil {
panic(err)
}
doc, err := libxml2.ParseHTML(resp.Content())
if err != nil {
panic(err)
}
defer doc.Free()
result, err := doc.Find("//*[contains(@class, 'tag')]/@href")
if err != nil {
panic(err)
}
defer result.Free()
iter := result.NodeIter()
for iter.Next() {
log.Println(iter.Node().NodeValue())
wurl := "https://twitcasting.tv" + iter.Node().NodeValue()
if ok := queuedict[wurl]; !ok {
queue.Put(wurl)
queuedict[wurl] = true
}
}
doc.Find("//div[@class='tw-search-result-row']")
log.Println("finish remain", queue.Size())
}
}