-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
43 lines (39 loc) · 833 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package DHTCrawl
import (
"fmt"
"github.com/zeebo/bencode"
"net/http"
"time"
)
const (
Url = "http://bt.box.n0808.com"
)
type MetaData struct {
Hash Hash
MetaInfo *MetadataResult `bencode:"info"`
}
func HttpDownload(hash Hash) (*MetadataResult, error) {
hex := hash.Hex()
url := fmt.Sprintf("%s/%s/%s/%s.torrent", Url, hex[:2], hex[38:], hex)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Referer", Url)
client := http.Client{
Timeout: time.Duration(time.Second * 2),
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dec := bencode.NewDecoder(resp.Body)
metadata := new(MetaData)
err = dec.Decode(metadata)
if err != nil {
return nil, err
}
metadata.MetaInfo.Hash = hash
return metadata.MetaInfo, nil
}