示例#1
0
func (t *Townparser) ParsePageCount() (int, error) {
	resp, err := t.Tc.Get(t.Url)
	if err != nil {
		log.Error("%s %s", TAG, err.Error())
		return 0, err
	}
	defer resp.Body.Close()

	bv, _ := ioutil.ReadAll(resp.Body)
	node, err := goquery.ParseString(toUtf8(bv))
	if err != nil {
		return 0, err
	}

	var count int
	var err2 error
	count = 0
	node.Find("a").Each(func(index int, element *goquery.Node) {
		if count != 0 {
			return
		}
		nodes := goquery.Nodes{element}
		val := nodes.Attr("title")
		if val != "" {
			if strings.Contains(val, "Letzte Seite") {
				url := nodes.Attr("href")
				if url != "" {
					aStr := strings.Split(url, "=")
					count, err2 = strconv.Atoi(aStr[len(aStr)-1])
				}
			}
		}
	})
	if err2 != nil {
		return 0, err2
	}
	return count, nil
}
示例#2
0
//parse the http resp from Townclient
func (t *Townparser) ParseReleases(flush bool) error {
	log.Info("%s parsing %v", TAG, t.Url)
	if flush {
		t.Site = nil
	}

	if t.Site == nil {
		resp, err := t.Tc.Get(t.Url)
		if err != nil {
			log.Error("%s %s", TAG, err.Error())
			return err
		}
		defer resp.Body.Close()

		bv, _ := ioutil.ReadAll(resp.Body)
		t.Site = bv
	}
	node, err := goquery.ParseString(toUtf8(t.Site))
	if err != nil {
		return err
	}

	node.Find("#threadslist tbody tr").Each(func(index int, element *goquery.Node) {
		nodes := goquery.Nodes{element}
		var rel Release
		nodes.Find("td").Each(func(index int, element *goquery.Node) {
			nodes := goquery.Nodes{element}
			class := nodes.Attr("class")
			if strings.Contains(class, "alt") {
				switch index {
				case 0:
					rel = Release{}
				case 1:
					rel.Image = element.Child[1].Attr[2].Val
				case 2:
					nodes.Find("a").Each(func(i int, se *goquery.Node) {
						nodes2 := goquery.Nodes{se}
						switch i {
						case 1:
							text := nodes2.Text()
							if strings.Contains(text, "/") {
								text = strings.Join(strings.Split(text, "/"), ",")
							}
							text = strings.ToLower(text)
							rel.addTag(text)
						case 2:
							rel.Name = nodes2.Text()
							rel.Url = nodes2.Attr("href")
							rel.Checksum = t.encodeName(rel.Url)
						}
					})
				case 6:
					rel.Time = time.Now().Unix()
					rel.fillRelease()
					if rel.Checksum != "" {
						rel.Hits = 0
						rel.Rating = 0
						t.downloadImage(rel.Image, rel.Checksum)
						t.addRelease(rel)
					}
				}
			}
		})

	})

	return nil
}