/
torrent.go
48 lines (42 loc) · 1.11 KB
/
torrent.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
44
45
46
47
48
// Copyright (c) 2020-2021 Andrew Archibald
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package ia
import (
"fmt"
"path"
"path/filepath"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/storage"
)
// DownloadTorrents downloads the named Internet Archive items via
// torrent.
func DownloadTorrents(ids []string, dir string) error {
conf := torrent.NewDefaultClientConfig()
conf.DataDir = dir
conf.DefaultStorage = storage.NewMMap(dir)
c, err := torrent.NewClient(conf)
if err != nil {
return err
}
for i, id := range ids {
url := "https://archive.org/download/" + id + "/" + id + "_archive.torrent"
fmt.Printf("(%d/%d) Adding %s\n", i+1, len(ids), id)
filename := filepath.Join(dir, path.Base(url))
if err := DownloadFile(url, filename); err != nil {
return err
}
t, err := c.AddTorrentFromFile(filename)
if err != nil {
return err
}
t.DownloadAll()
if i%15 == 14 {
c.WaitAll()
}
}
c.WaitAll()
return nil
}