File size: 2,750 Bytes
3b90d58
29af131
3b90d58
 
 
 
29af131
3b90d58
29af131
 
3b90d58
29af131
 
 
 
 
 
 
 
 
 
 
 
 
 
3b90d58
29af131
3b90d58
29af131
3b90d58
 
29af131
3b90d58
29af131
 
 
 
 
 
 
3b90d58
 
29af131
 
 
 
 
 
3b90d58
29af131
3b90d58
29af131
3b90d58
 
29af131
3b90d58
29af131
 
 
 
3b90d58
 
29af131
3b90d58
 
29af131
3b90d58
 
29af131
 
3b90d58
 
 
29af131
 
 
 
 
 
 
 
 
 
 
 
3b90d58
 
29af131
3b90d58
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import libtorrent as lt
import os
import time

ses = lt.session()
ses.listen_on(6881, 6891)
downloads = []

# Path folder Hasil Download
download_folder = '/home/user/app/Torrent'

# Membuat folder hasil download jika belum ada
if not os.path.exists(download_folder):
    os.makedirs(download_folder)
    print("Folder 'Hasil Download' berhasil dibuat")
else:
    print("Folder 'Hasil Download' sudah ada")

def torrent_info(link, search):
    params = {
        'save_path': download_folder,
        'storage_mode': lt.storage_mode_t.storage_mode_sparse,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    handle = lt.add_magnet_uri(ses, link, params)
    ses.start_dht()

    print("Mendapatkan metadata dari magnet link...")
    while (not handle.has_metadata()):
        time.sleep(1)
    print("Metadata diterima.")

    info = handle.get_torrent_info()
    file_list = info.files()
    info_array = []
    for i, file in enumerate(file_list):
        if file.path.endswith(search):
            info_array.append(f"{i}. {file.path}")
    return info_array

def torrent_download(link, selected_files):
    params = {
        'save_path': download_folder,
        'storage_mode': lt.storage_mode_t.storage_mode_sparse,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    handle = lt.add_magnet_uri(ses, link, params)
    ses.start_dht()

    print("Mendapatkan metadata dari magnet link...")
    while (not handle.has_metadata()):
        time.sleep(1)
    print("Metadata diterima.")

    info = handle.get_torrent_info()
    file_list = info.files()
    
    file_priorities = [0] * len(file_list)
    for index in selected_files:
        file_priorities[index] = 1

    handle.prioritize_files(file_priorities)

    while (not handle.is_seed()):
        s = handle.status()
        state_str = ['queued', 'checking', 'downloading metadata',
                     'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
        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]))

        # Status bar
        progress_bar = '#' * int(s.progress * 100)
        remaining_space = ' ' * (100 - len(progress_bar))
        status_bar = '[%s%s] %.2f%%' % (progress_bar, remaining_space, s.progress * 100)
        print(status_bar)

        if state_str[s.state] == 'finished':
            break

        time.sleep(1)

    # Return the path of the downloaded files
    downloaded_files = []
    for index in selected_files:
        downloaded_files.append(os.path.join(download_folder, file_list[index].path))
    return downloaded_files