-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplease.py
More file actions
93 lines (70 loc) · 2.46 KB
/
please.py
File metadata and controls
93 lines (70 loc) · 2.46 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
import os
import requests
import psycopg2
from psycopg2.extras import DictCursor
from PIL import Image
from io import BytesIO
# ==== CONFIGURATION ====
DB_CONFIG = {
"dbname": "quequeue_db",
"user": "quequeue_user",
"password": "quequeue_pass",
"host": "localhost",
"port": 5432,
}
OUTPUT_DIR = "/Users/parthkotwal/Projects/quequeue/quequeue-frontend/src/assets/album_covers" # directory to store images
TABLE_NAME = "queues_track"
IMAGE_COLUMN = "album_image_url"
ID_COLUMN = "id"
# =======================
# Use readable names instead of numbers
FILE_NAMES = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf"]
def get_unique_filename(base_path):
"""Return a unique filename by appending (1), (2), ... if file exists."""
if not os.path.exists(base_path):
return base_path
base, ext = os.path.splitext(base_path)
counter = 1
new_path = f"{base} ({counter}){ext}"
while os.path.exists(new_path):
counter += 1
new_path = f"{base} ({counter}){ext}"
return new_path
def download_and_save_image(url, filename):
"""Download image from URL and save as .webp"""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
# Open image in memory
img = Image.open(BytesIO(response.content)).convert("RGB")
# Ensure unique filename
filename = get_unique_filename(filename)
# Save as .webp
img.save(filename, "webp")
print(f"✅ Saved: {filename}")
except Exception as e:
print(f"❌ Failed to download {url}: {e}")
def main():
# Ensure output directory exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Connect to PostgreSQL
conn = psycopg2.connect(**DB_CONFIG)
cursor = conn.cursor(cursor_factory=DictCursor)
# Fetch first 7 rows only
cursor.execute(f"SELECT {ID_COLUMN}, {IMAGE_COLUMN} FROM {TABLE_NAME} LIMIT 7")
rows = cursor.fetchall()
print(f"Found {len(rows)} rows to process...")
for idx, row in enumerate(rows):
url = row[IMAGE_COLUMN]
if not url:
print(f"⚠️ Skipping row {idx+1}: no URL")
continue
# Pick name from FILE_NAMES list
name = FILE_NAMES[idx] if idx < len(FILE_NAMES) else f"extra_{idx+1}"
filename = os.path.join(OUTPUT_DIR, f"{name}.webp")
download_and_save_image(url, filename)
cursor.close()
conn.close()
print("🎉 Done!")
if __name__ == "__main__":
main()