-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.py
More file actions
246 lines (188 loc) · 7.21 KB
/
Copy pathtelegram.py
File metadata and controls
246 lines (188 loc) · 7.21 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import os
import shutil
import requests
import time
from tqdm import tqdm
from fnmatch import fnmatch
from concurrent.futures import ThreadPoolExecutor
from plyer import notification
# ---------------- CONFIG ----------------
BOT_TOKEN = ""
CHAT_ID = ""
BACKUP_FOLDER = r"Backup"
UPLOADED_LIST_FILE = os.path.join(BACKUP_FOLDER, ".uploaded_list.txt")
BACKUPIGNORE = os.path.join(BACKUP_FOLDER, ".backupignore")
# video formats
VIDEO_EXT = [".mp4", ".mkv", ".avi", ".mov", ".wmv", ".webm"]
# telegram max size
MAX_BOT_SIZE = 49 * 1024 * 1024 # 49 MB safe limit
# ---------------- NOTIFICATION ----------------
def show_notification(title, message):
try:
notification.notify(
title=title,
message=message,
timeout=5
)
except:
pass
# ---------------- IGNORE SYSTEM ----------------
def load_ignore_patterns():
patterns = []
if os.path.exists(BACKUPIGNORE):
with open(BACKUPIGNORE, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
patterns.append(line)
return patterns
IGNORE_PATTERNS = load_ignore_patterns()
def is_ignored(path):
name = os.path.basename(path)
for pattern in IGNORE_PATTERNS:
if fnmatch(name, pattern) or fnmatch(path, pattern):
return True
return False
# ---------------- UPLOADED LIST ----------------
def load_uploaded_list():
if not os.path.exists(UPLOADED_LIST_FILE):
return set()
with open(UPLOADED_LIST_FILE, "r", encoding="utf-8") as f:
return set(line.strip() for line in f.readlines())
def save_uploaded_item(item_path):
with open(UPLOADED_LIST_FILE, "a", encoding="utf-8") as f:
f.write(item_path + "\n")
# ---------------- TELEGRAM SENDERS ----------------
def send_video(file_path):
"""Send video for <50MB"""
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendVideo"
file_name = os.path.basename(file_path)
with open(file_path, "rb") as f:
response = requests.post(
url,
data={"chat_id": CHAT_ID},
files={"video": (file_name, f)},
timeout=600
)
return response.status_code == 200
def send_file_with_progress(file_path):
"""Send any file as document"""
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendDocument"
file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path)
with open(file_path, "rb") as f:
with tqdm(total=file_size, unit="B", unit_scale=True, desc=file_name) as bar:
response = requests.post(
url,
data={"chat_id": CHAT_ID},
files={"document": (file_name, f)},
timeout=600
)
bar.update(file_size)
return response.status_code == 200
# ---------------- ZIP FOLDER ----------------
def zip_and_upload_folder(folder_path, folder_name, uploaded_list):
zip_name = folder_name + ".zip"
zip_path = os.path.join(BACKUP_FOLDER, zip_name)
if zip_name in uploaded_list:
print(f"⏩ Already uploaded ZIP: {zip_name}")
return False
print(f"📦 Creating ZIP: {zip_name}")
shutil.make_archive(os.path.join(BACKUP_FOLDER, folder_name), 'zip', folder_path)
print(f"📤 Uploading ZIP: {zip_name}")
if send_file_with_progress(zip_path):
print(f"✔ Uploaded: {zip_name}")
save_uploaded_item(zip_name)
shutil.rmtree(folder_path)
os.remove(zip_path)
print(f"🗑 Deleted folder & ZIP: {folder_name}")
return True
else:
print(f"❌ Failed: {zip_name}")
return False
# ---------------- PROCESS FILE ----------------
def process_file(item, full_path, uploaded):
if item in uploaded:
print(f"⏩ Already uploaded: {item}")
return False
ext = os.path.splitext(item)[1].lower()
file_size = os.path.getsize(full_path)
# ---------------- Video handling ----------------
if ext in VIDEO_EXT:
if file_size <= MAX_BOT_SIZE:
print(f"\n🎬 Uploading video: {item} (<49MB)")
if send_video(full_path):
print(f"✔ Uploaded video: {item}")
save_uploaded_item(item)
os.remove(full_path)
return True
else:
print(f"❌ Failed video: {item}")
return False
else:
# Too big -> ZIP it
print(f"\n📦 Video too large → Zipping: {item}")
zip_name = item + ".zip"
zip_path = os.path.join(BACKUP_FOLDER, zip_name)
# create zip
shutil.make_archive(zip_path.replace(".zip", ""), 'zip',
os.path.dirname(full_path), item)
print(f"🎬 Uploading zipped large video: {zip_name}")
if send_file_with_progress(zip_path):
print(f"✔ Uploaded ZIP video: {zip_name}")
save_uploaded_item(zip_name)
os.remove(full_path)
os.remove(zip_path)
return True
else:
print(f"❌ Failed ZIP video: {zip_name}")
return False
# ---------------- Normal file handling ----------------
print(f"\n📤 Uploading: {item}")
if send_file_with_progress(full_path):
print(f"✔ Uploaded: {item}")
save_uploaded_item(item)
os.remove(full_path)
return True
else:
print(f"❌ Failed: {item}")
return False
# ---------------- PROCESS FOLDER ----------------
def process_folder(item, full_path, uploaded):
before = len(uploaded)
success = zip_and_upload_folder(full_path, item, uploaded)
after = len(uploaded)
return success or (after > before)
# ---------------- MAIN BACKUP ----------------
def backup():
uploaded = load_uploaded_list()
items = os.listdir(BACKUP_FOLDER)
tasks = []
upload_happened = False
with ThreadPoolExecutor(max_workers=4) as executor:
for item in items:
if item in [".uploaded_list.txt", ".backupignore"]:
continue
if is_ignored(item):
print(f"🚫 Ignored: {item}")
continue
full_path = os.path.join(BACKUP_FOLDER, item)
if os.path.isfile(full_path):
tasks.append(executor.submit(process_file, item, full_path, uploaded))
elif os.path.isdir(full_path):
tasks.append(executor.submit(process_folder, item, full_path, uploaded))
for t in tasks:
try:
if t.result():
upload_happened = True
except:
pass
if upload_happened:
show_notification("Backup Completed", "All new files were uploaded successfully!")
print("\n🎉 Backup Completed!\n")
# ---------------- LOOP ----------------
while True:
print("🔄 Checking for new files...")
backup()
print("⏳ Waiting 10 seconds...\n")
time.sleep(10)