如何用Colab下載BT (How to use Colab download BitTorrent)

如何用Colab下載BT (How to use Colab download BitTorrent)

如果你有用過BT下載東西(謎片),那不知道你有沒有聽過BT代抓呢?就是用遠端主機幫你下載BT,下載完畢後再從遠端主機下載到你的電腦裡,這樣不但可以幫你省電XD,也可以藉由遠端主機的頻寬不浪費你的網路(雖然下載回本機還是會佔頻寬啦),也可以讓你硬碟記憶體等減少讀取(不過現在電腦都很強,基本上這些零件蠻難壞的)!

註:此為網路教學研究,請勿作不符合規定的事項。

Colab介紹

Colaboratory是一個免費的Jupyter筆記本環境,不需要進行任何設置就可以使用,並且完全在雲端運行。

通過協作,您可以編寫和執行程式碼,保存和共享分析結果,以及利用強大的計算資源,所有這些都可通過瀏覽器免費使用。

程式碼介紹

我是某天從Github看到FKLC大神分享的「Torrent-To-Google-Drive-Downloader」專案,然後看了相關API修改增加一些下載的方式,雖然有點土炮改程式碼修改的有點醜,不過真的還蠻有趣的!

先載入你的雲端硬碟(Mount your Google drive)

1
2
from google.colab import drive
drive.mount('/content/drive')

上傳你的種子檔(Upload torrent file)

1
2
from google.colab import files
uploaded = files.upload()

下載python torrent客戶端(Download python torrent clinet)

1
!apt install python3-libtorrent

開始下載(Start download)

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
49
50
51
52
import libtorrent as lt
import time
import sys

ses = lt.session()
ses.listen_on(6881, 6891)

ses.add_extension('ut_metadata')
ses.add_extension('ut_pex')
ses.add_extension('metadata_transfer')
ses.add_dht_router('router.bittorrent.com', 6881)
ses.add_dht_router('router.utorrent.com', 6881)
ses.add_dht_router('router.bitcomet.com', 6881)
ses.add_dht_router('dht.transmissionbt.com', 6881)
ses.add_dht_router("dht.aelitis.com", 6881)
ses.start_dht()
ses.start_lsd()
ses.start_upnp()
ses.start_natpmp()

info = lt.torrent_info(list(uploaded.keys())[0])
h = ses.add_torrent({"ti": info, "save_path": "/content/drive/My Drive/Torrent"})
print("starting", h.name())

while not h.is_seed():
s = h.status()

state_str = [
"queued",
"checking",
"downloading metadata",
"downloading",
"finished",
"seeding",
"allocating",
"checking fastresume",
]
print(
"\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d seeds: %d) %s"
% (
s.progress * 100,
s.download_rate / 1000,
s.upload_rate / 1000,
s.num_peers,
s.num_seeds,
state_str[s.state],
)
)
sys.stdout.flush()
time.sleep(60)

print(h.name(), "complete")

如果你有磁力連結的話請用這個版本的程式碼(If you only magnet link, then use the code)

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
49
50
import libtorrent as lt
import time

ses = lt.session()
ses.listen_on(6881, 6891)

ses.add_extension('ut_metadata')
ses.add_extension('ut_pex')
ses.add_extension('metadata_transfer')
ses.add_dht_router('router.bittorrent.com', 6881)
ses.add_dht_router('router.utorrent.com', 6881)
ses.add_dht_router('router.bitcomet.com', 6881)
ses.add_dht_router('dht.transmissionbt.com', 6881)
ses.add_dht_router("dht.aelitis.com", 6881)
ses.start_dht()
ses.start_lsd()
ses.start_upnp()
ses.start_natpmp()

params = {
'save_path': '/content/drive/My Drive/Torrent',
'storage_mode': lt.storage_mode_t(2),
'paused': False,
'auto_managed': True,
'duplicate_is_error': True}

"""
下方填入磁力連結
Modify magnetlink string
"""
link = 'magnetlink'
link = link.lower()

handle = lt.add_magnet_uri(ses, link, params)


print ('downloading metadata...')
while (not handle.has_metadata()):
time.sleep(1)
print ('got metadata, starting torrent download...')
while (handle.status().state != lt.torrent_status.seeding):
s = handle.status()
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating']
print ("%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d ) %s" % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]))
time.sleep(60)

print(handle.name(), "complete")

如果你種子檔放在雲端硬碟則用該程式碼(If your torrent in google drive, then use the code)

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
49
50
51
52
53
54
55
56
57
58
import libtorrent as lt
import time
import sys

ses = lt.session()
ses.listen_on(6881, 6891)

ses.add_extension('ut_metadata')
ses.add_extension('ut_pex')
ses.add_extension('metadata_transfer')
ses.add_dht_router('router.bittorrent.com', 6881)
ses.add_dht_router('router.utorrent.com', 6881)
ses.add_dht_router('router.bitcomet.com', 6881)
ses.add_dht_router('dht.transmissionbt.com', 6881)
ses.add_dht_router("dht.aelitis.com", 6881)
ses.start_dht()
ses.start_lsd()
ses.start_upnp()
ses.start_natpmp()

# 修改檔案路徑
# modify file localion
e = lt.bdecode(open("/content/drive/My Drive/1.torrent", 'rb').read())

info = lt.torrent_info(e)
h = ses.add_torrent({"ti": info, "save_path": "/content/drive/My Drive/Torrent"})
print("starting", h.name())

while not h.is_seed():
s = h.status()

state_str = [
"queued",
"checking",
"downloading metadata",
"downloading",
"finished",
"seeding",
"allocating",
"checking fastresume",
]
print(
"\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d seeds: %d) %s"
% (
s.progress * 100,
s.download_rate / 1000,
s.upload_rate / 1000,
s.num_peers,
s.num_seeds,
state_str[s.state],
)
)
sys.stdout.flush()
time.sleep(60)

print(h.name(), "complete")

e.close()

Colab限制

  1. 程式碼最多執行下載時間為12小時,若下載不完可能再重複執行程式碼才可(Google drive會有暫存檔,除非超冷門的檔案)

  2. 程式下載空間有限制依你的雲端硬碟空間為主,但無限容量的硬碟若總容量大於1.5TB可能會發生錯誤(我目前測到的怪異Bug)

後記

雖然運用Google的資源很爽,不過Colab也有一定限制!但下載熱門檔真的很方便,下載就直接到Google drive,省得一道工!

下週主題「food」!

Imgur

0%