github.com/anacrolix/torrent@v1.61.0/tracker/http/scrape.go (about) 1 package httpTracker 2 3 import ( 4 "context" 5 "log" 6 "net/http" 7 "net/url" 8 9 "github.com/anacrolix/torrent/bencode" 10 "github.com/anacrolix/torrent/tracker/udp" 11 "github.com/anacrolix/torrent/types/infohash" 12 ) 13 14 type scrapeResponse struct { 15 Files files `bencode:"files"` 16 } 17 18 // Bencode should support bencode.Unmarshalers from a string in the dict key position. 19 type files = map[string]udp.ScrapeInfohashResult 20 21 func (cl Client) Scrape(ctx context.Context, ihs []infohash.T) (out udp.ScrapeResponse, err error) { 22 _url := cl.url_.JoinPath("..", "scrape") 23 query, err := url.ParseQuery(_url.RawQuery) 24 if err != nil { 25 return 26 } 27 for _, ih := range ihs { 28 query.Add("info_hash", ih.AsString()) 29 } 30 _url.RawQuery = query.Encode() 31 log.Printf("%q", _url.String()) 32 req, err := http.NewRequestWithContext(ctx, http.MethodGet, _url.String(), nil) 33 if err != nil { 34 return 35 } 36 resp, err := cl.hc.Do(req) 37 if err != nil { 38 return 39 } 40 defer resp.Body.Close() 41 var decodedResp scrapeResponse 42 err = bencode.NewDecoder(resp.Body).Decode(&decodedResp) 43 for _, ih := range ihs { 44 out = append(out, decodedResp.Files[ih.AsString()]) 45 } 46 return 47 }