hannabaker commited on
Commit
4a07c4f
·
verified ·
1 Parent(s): 5bec39a

Create providers/mega_provider.py

Browse files
Files changed (1) hide show
  1. providers/mega_provider.py +78 -0
providers/mega_provider.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+
5
+ class MegaProvider:
6
+ def __init__(self, email=None, password=None):
7
+ self.email = email
8
+ self.password = password
9
+
10
+ def validate_credentials(self, email, password):
11
+ """Validate Mega credentials"""
12
+ megarc_path = tempfile.mktemp()
13
+ try:
14
+ with open(megarc_path, 'w') as f:
15
+ f.write(f"[Login]\nUsername = {email}\nPassword = {password}\n")
16
+
17
+ result = subprocess.run(
18
+ ['megals', '--config', megarc_path, '/Root'],
19
+ capture_output=True,
20
+ text=True,
21
+ timeout=10
22
+ )
23
+
24
+ return result.returncode == 0
25
+ finally:
26
+ if os.path.exists(megarc_path):
27
+ os.remove(megarc_path)
28
+
29
+ def upload(self, file_path, progress_callback=None):
30
+ """Upload file to Mega"""
31
+ megarc_path = tempfile.mktemp()
32
+ try:
33
+ with open(megarc_path, 'w') as f:
34
+ f.write(f"[Login]\nUsername = {self.email}\nPassword = {self.password}\n")
35
+
36
+ cmd = [
37
+ 'megaput',
38
+ '--config', megarc_path,
39
+ '--path', '/Root',
40
+ file_path
41
+ ]
42
+
43
+ process = subprocess.Popen(
44
+ cmd,
45
+ stdout=subprocess.PIPE,
46
+ stderr=subprocess.PIPE,
47
+ text=True,
48
+ bufsize=1
49
+ )
50
+
51
+ last_progress = 0
52
+ while True:
53
+ line = process.stdout.readline()
54
+ if not line and process.poll() is not None:
55
+ break
56
+
57
+ if line and progress_callback:
58
+ # Parse progress
59
+ import re
60
+ match = re.search(r'(\d+)%', line)
61
+ if match:
62
+ progress = int(match.group(1))
63
+ if progress != last_progress:
64
+ last_progress = progress
65
+ progress_callback(progress)
66
+
67
+ if process.returncode == 0:
68
+ return {
69
+ 'provider': 'mega',
70
+ 'filename': os.path.basename(file_path),
71
+ 'size': os.path.getsize(file_path)
72
+ }
73
+
74
+ return None
75
+
76
+ finally:
77
+ if os.path.exists(megarc_path):
78
+ os.remove(megarc_path)