You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.4 KiB
Python

import config
import requests
def is_url_accessible(url):
try:
response = requests.head(url, timeout=5) # HEAD request is usually faster and enough to check availability
return response.status_code == 200
except requests.RequestException:
return False
media_names = ['mediaUrl', 'mediaPreviewUrl']
db, cursor = config.gen_connection()
for media_type in media_names:
cursor.execute(f"SELECT id, {media_type} FROM snapchat_stories WHERE {media_type} NOT LIKE 'https://cf-st.sc-cdn.net/d/%' AND status != 'inactive'")
rows = cursor.fetchall()
total = len(rows)
count = 0
for row in rows:
count += 1
record_id, original_url = row
media_id = original_url.split('/')[-1]
new_url = f'https://cf-st.sc-cdn.net/d/{media_id}'
if is_url_accessible(new_url):
print(f"✅ [{count} / {total}] {new_url} is accessible (converted from {original_url})")
cursor.execute(f"UPDATE snapchat_stories SET {media_type} = %s, status = 'updated' WHERE id = %s", (new_url, record_id))
db.commit()
continue
print(f"❌ [{count} / {total}] {new_url} is NOT accessible (original: {original_url})")
cursor.execute("""UPDATE snapchat_stories SET status = 'inactive' WHERE id = %s""", (record_id,))
db.commit()
cursor.close()
db.close()