GilangAlRusliadi commited on
Commit
29af131
1 Parent(s): 9784d97
Files changed (2) hide show
  1. Torrent.py +60 -34
  2. app.py +8 -0
Torrent.py CHANGED
@@ -1,65 +1,91 @@
1
  import libtorrent as lt
 
2
  import time
3
 
4
  ses = lt.session()
5
  ses.listen_on(6881, 6891)
 
6
 
7
- params = {
8
- 'save_path': '/home/user/app/Torrent',
9
- 'storage_mode': lt.storage_mode_t(2),
10
- 'paused': False,
11
- 'auto_managed': True,
12
- 'duplicate_is_error': True
13
- }
14
 
15
- def torrent_info(link, extension):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  handle = lt.add_magnet_uri(ses, link, params)
 
17
 
18
- print('Mendownload metadata...')
19
  while (not handle.has_metadata()):
20
  time.sleep(1)
21
- print('Metadata didownload, memulai download torrent...')
22
 
23
- torinfo = handle.get_torrent_info()
24
- file_priorities = [0] * torinfo.num_files()
25
- torrent_info = []
26
- for idx, file in enumerate(torinfo.files()):
27
- if file.path.endswith(extension):
28
- print(f'{idx}. {file.path}')
29
- torrent_info.append(file.path)
30
- file_priorities[idx] = 1
31
- handle.prioritize_files(file_priorities)
32
- return torrent_info
33
 
34
  def torrent_download(link, selected_files):
 
 
 
 
 
 
 
35
  handle = lt.add_magnet_uri(ses, link, params)
 
36
 
37
- # Menunggu metadata selesai didownload
38
  while (not handle.has_metadata()):
39
  time.sleep(1)
 
40
 
41
- # Mengatur prioritas file sesuai dengan selected_files
42
- torinfo = handle.get_torrent_info()
43
- file_priorities = [0] * torinfo.num_files()
 
44
  for index in selected_files:
45
  file_priorities[index] = 1
 
46
  handle.prioritize_files(file_priorities)
47
 
48
- # Mendownload file .mp4 yang dipilih
49
- while (handle.status().state != lt.torrent_status.seeding):
50
  s = handle.status()
51
  state_str = ['queued', 'checking', 'downloading metadata',
52
- 'downloading', 'finished', 'seeding', 'allocating']
53
- print('%.2f%% selesai (download: %.1f kb/s upload: %.1f kB/s peers: %d) %s' %
54
  (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000,
55
  s.num_peers, state_str[s.state]))
56
- time.sleep(5)
57
 
58
- # Mencari file yang telah selesai didownload dan mengembalikan path-nya
 
 
 
 
 
 
 
 
 
 
 
59
  downloaded_files = []
60
  for index in selected_files:
61
- file_path = torinfo.files().file_path(index)
62
- downloaded_files.append(file_path)
63
  return downloaded_files
64
-
65
-
 
1
  import libtorrent as lt
2
+ import os
3
  import time
4
 
5
  ses = lt.session()
6
  ses.listen_on(6881, 6891)
7
+ downloads = []
8
 
9
+ # Path folder Hasil Download
10
+ download_folder = '/home/user/app/Torrent'
 
 
 
 
 
11
 
12
+ # Membuat folder hasil download jika belum ada
13
+ if not os.path.exists(download_folder):
14
+ os.makedirs(download_folder)
15
+ print("Folder 'Hasil Download' berhasil dibuat")
16
+ else:
17
+ print("Folder 'Hasil Download' sudah ada")
18
+
19
+ def torrent_info(link, search):
20
+ params = {
21
+ 'save_path': download_folder,
22
+ 'storage_mode': lt.storage_mode_t.storage_mode_sparse,
23
+ 'paused': False,
24
+ 'auto_managed': True,
25
+ 'duplicate_is_error': True
26
+ }
27
  handle = lt.add_magnet_uri(ses, link, params)
28
+ ses.start_dht()
29
 
30
+ print("Mendapatkan metadata dari magnet link...")
31
  while (not handle.has_metadata()):
32
  time.sleep(1)
33
+ print("Metadata diterima.")
34
 
35
+ info = handle.get_torrent_info()
36
+ file_list = info.files()
37
+ info_array = []
38
+ for i, file in enumerate(file_list):
39
+ if file.path.endswith(search):
40
+ info_array.append(f"{i}. {file.path}")
41
+ return info_array
 
 
 
42
 
43
  def torrent_download(link, selected_files):
44
+ params = {
45
+ 'save_path': download_folder,
46
+ 'storage_mode': lt.storage_mode_t.storage_mode_sparse,
47
+ 'paused': False,
48
+ 'auto_managed': True,
49
+ 'duplicate_is_error': True
50
+ }
51
  handle = lt.add_magnet_uri(ses, link, params)
52
+ ses.start_dht()
53
 
54
+ print("Mendapatkan metadata dari magnet link...")
55
  while (not handle.has_metadata()):
56
  time.sleep(1)
57
+ print("Metadata diterima.")
58
 
59
+ info = handle.get_torrent_info()
60
+ file_list = info.files()
61
+
62
+ file_priorities = [0] * len(file_list)
63
  for index in selected_files:
64
  file_priorities[index] = 1
65
+
66
  handle.prioritize_files(file_priorities)
67
 
68
+ while (not handle.is_seed()):
 
69
  s = handle.status()
70
  state_str = ['queued', 'checking', 'downloading metadata',
71
+ 'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
72
+ print('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' %
73
  (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000,
74
  s.num_peers, state_str[s.state]))
 
75
 
76
+ # Status bar
77
+ progress_bar = '#' * int(s.progress * 100)
78
+ remaining_space = ' ' * (100 - len(progress_bar))
79
+ status_bar = '[%s%s] %.2f%%' % (progress_bar, remaining_space, s.progress * 100)
80
+ print(status_bar)
81
+
82
+ if state_str[s.state] == 'finished':
83
+ break
84
+
85
+ time.sleep(1)
86
+
87
+ # Return the path of the downloaded files
88
  downloaded_files = []
89
  for index in selected_files:
90
+ downloaded_files.append(os.path.join(download_folder, file_list[index].path))
 
91
  return downloaded_files
 
 
app.py CHANGED
@@ -1,3 +1,11 @@
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from streamlit_option_menu import option_menu
3
  from Torrent import torrent_info, torrent_download
 
1
+ import os
2
+
3
+ os.system('pip install jedi')
4
+ os.system('python -m pip install --upgrade pip setuptools wheel')
5
+ os.system('python -m pip install lbry-libtorrent')
6
+ os.system('apt-get install ffmpeg -y')
7
+ os.system('apt install python3-libtorrent')
8
+
9
  import streamlit as st
10
  from streamlit_option_menu import option_menu
11
  from Torrent import torrent_info, torrent_download