Example #1
0
func (k Kickass) Search(query string) ([]Torrent, error) {
	req, err := http.NewRequest("GET", k.constructSearchURL(query), nil)
	if err != nil {
		return nil, err
	}

	var result kickassSearchResult

	err = sources.GetXML(req, &result)
	if e, ok := err.(sources.RequestError); ok {
		if e.ResponseCode == 404 {
			log.WithFields(log.Fields{
				"search_engine": k.Name(),
				"url":           req.URL,
			}).Debug("No torrents found.")
			return nil, nil
		}
		return nil, err
	}
	if err != nil {
		return nil, err
	}

	searchItems := result.Channel.Items

	var torrents []Torrent
	for _, searchItem := range searchItems {
		url, err := searchItem.torrentURL(k.torCacheURL)
		if err != nil {
			return nil, err
		}
		torrent := Torrent{
			URL:      url,
			Filename: searchItem.FileName,
			Title:    searchItem.Title,
			seeds:    searchItem.Seeds,
		}
		torrents = append(torrents, torrent)
	}

	return torrents, nil
}
Example #2
0
func (t TorrentCD) Search(query string) ([]Torrent, error) {
	req, err := http.NewRequest(
		"GET",
		fmt.Sprintf(t.URL+"/torrents/xml?q=%s", url.QueryEscape(query)),
		nil,
	)

	var result torrentCDSearchResult
	err = sources.GetXML(req, &result)
	if err == io.EOF { // the result contain non XML when nothing is found
		log.WithFields(log.Fields{
			"search_engine": t.Name(),
			"url":           req.URL,
		}).Debug("No torrents found.")
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	var torrents []Torrent
	for _, item := range result.Channel.Items {
		url, err := item.torrentURL()
		if err != nil {
			return nil, err
		}

		torrent := Torrent{
			URL:      url,
			Title:    item.Title,
			Filename: item.Filename(),
			seeds:    item.Seed,
		}
		torrents = append(torrents, torrent)
	}

	return torrents, nil
}