|
6 | 6 | from datetime import datetime
|
7 | 7 | from colab_leecher.utility.helper import status_bar
|
8 | 8 | from colab_leecher.utility.variables import BotTimes, Messages, Paths
|
9 |
| - |
| 9 | +from pymegatools import Megatools,MegaError |
10 | 10 |
|
11 | 11 | async def megadl(link: str, num: int):
|
12 | 12 |
|
13 | 13 | global BotTimes, Messages
|
14 | 14 | BotTimes.task_start = datetime.now()
|
| 15 | + mega= Megatools() |
| 16 | + try: |
| 17 | + await mega.async_download(link,progress=pro_for_mega,path= Paths.down_path) |
| 18 | + except MegaError as e: |
| 19 | + logging.error(f"An Error occured : {e}") |
15 | 20 |
|
16 |
| - # TODO: Check the type of link and set the commands accordingly. i.e, Check if private or public, file or folder |
17 |
| - |
18 |
| - # Create a command to run megadl with the link |
19 |
| - command = [ |
20 |
| - "megadl", |
21 |
| - "--no-ask-password", |
22 |
| - "--path", |
23 |
| - Paths.down_path, |
24 |
| - link, |
25 |
| - ] |
26 |
| - |
27 |
| - process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
28 |
| - |
29 |
| - while True: |
30 |
| - output = process.stdout.readline() # Read output line by line # type: ignore |
31 |
| - if output == b'' and process.poll() is not None: |
32 |
| - break # Exit loop when process terminates and output is empty |
33 |
| - |
34 |
| - try: |
35 |
| - await extract_info(output.strip().decode("utf-8")) |
36 |
| - except Exception as e: |
37 |
| - logging.error(f"{e}") |
38 |
| - |
39 |
| - |
40 |
| -async def extract_info(line): |
41 | 21 |
|
42 |
| - # Split the line by colon and space |
43 |
| - parts = line.split(": ") |
44 |
| - subparts = [] |
45 |
| - # Split the second part by space |
46 |
| - if len(parts) > 1: |
47 |
| - subparts = parts[1].split() |
48 |
| - |
| 22 | +async def pro_for_mega(stream, process): |
| 23 | + line =stream[-1] |
49 | 24 | file_name = "N/A"
|
50 |
| - progress = "N/A" |
| 25 | + percentage = 0 |
51 | 26 | downloaded_size = "N/A"
|
52 | 27 | total_size = "N/A"
|
53 | 28 | speed = "N/A"
|
54 |
| - |
55 |
| - if len(subparts) > 10: |
56 |
| - |
57 |
| - try: |
58 |
| - # Get the file name from the first part |
59 |
| - file_name = parts[0] |
60 |
| - Messages.download_name = file_name |
61 |
| - # Get the progress from the first subpart |
62 |
| - progress = subparts[0][:-1] |
63 |
| - # Get the downloaded size and bytes from the subpart |
64 |
| - downloaded_size = f"{subparts[2]} {subparts[3]}" |
65 |
| - # downloaded_bytes = subparts[4][1:-1] |
66 |
| - # Get the total size from the subpart |
67 |
| - total_size = f"{subparts[7]} {subparts[8]}" |
68 |
| - # Get the speed from the subpart |
69 |
| - speed = f"{subparts[9][1:]} {subparts[10][:-1]}" |
70 |
| - except Exception: |
71 |
| - logging.error(f"Got this \n{parts}") |
72 |
| - |
| 29 | + try : |
| 30 | + ok =line.split(":") |
| 31 | + file_name = ok[0] |
| 32 | + ok=ok[1].split() |
| 33 | + percentage = float(ok[0][:-1]) |
| 34 | + downloaded_size = ok[2]+" "+ok[3] |
| 35 | + total_size = ok[7]+" "+ok[8] |
| 36 | + speed = ok[9][1:]+" "+ok[10][:-1] |
| 37 | + except Exception: |
| 38 | + pass |
| 39 | + Messages.download_name = file_name |
73 | 40 | Messages.status_head = f"<b>📥 DOWNLOADING FROM MEGA » </b>\n\n<b>🏷️ Name » </b><code>{file_name}</code>\n"
|
74 | 41 |
|
75 |
| - try: |
76 |
| - percentage = round(float(progress)) |
77 |
| - except Exception: |
78 |
| - percentage = 0 |
79 |
| - |
80 | 42 | await status_bar(
|
81 | 43 | Messages.status_head,
|
82 | 44 | speed,
|
|
0 commit comments