-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackfill_thumbnails.py
More file actions
executable file
·53 lines (43 loc) · 1.95 KB
/
Copy pathbackfill_thumbnails.py
File metadata and controls
executable file
·53 lines (43 loc) · 1.95 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
import asyncio
import os
from app import create_app
from models import db, File
from user_handler import generate_thumbnail, upload_thumbnail_with_pyrogram
from bot_handler import download_file_to_cache
async def backfill_thumbnails():
app = create_app()
with app.app_context():
files_to_process = File.query.filter(
File.thumbnail_file_id.is_(None),
File.mime_type.like('image/%') | File.mime_type.like('video/%')
).all()
print(f"Found {len(files_to_process)} files needing thumbnails.")
for file in files_to_process:
print(f"Processing file: {file.filename} (ID: {file.id})")
# 1. Download the file from Telegram
temp_file_path = os.path.join('temp_uploads', file.file_id)
if not download_file_to_cache(file.file_id):
print(f" Failed to download file: {file.filename}")
continue
# 2. Generate a thumbnail
thumbnail_path = await generate_thumbnail(temp_file_path, file.mime_type)
if not thumbnail_path:
print(f" Failed to generate thumbnail for: {file.filename}")
os.remove(temp_file_path)
continue
# 3. Upload the thumbnail to get a thumbnail_file_id
thumbnail_file_id = await upload_thumbnail_with_pyrogram(thumbnail_path)
if not thumbnail_file_id:
print(f" Failed to upload thumbnail for: {file.filename}")
os.remove(temp_file_path)
os.remove(thumbnail_path)
continue
# 4. Update the database
file.thumbnail_file_id = thumbnail_file_id
db.session.commit()
print(f" Successfully updated thumbnail for: {file.filename}")
# 5. Clean up temporary files
os.remove(temp_file_path)
os.remove(thumbnail_path)
if __name__ == '__main__':
asyncio.run(backfill_thumbnails())