Update main.py

This commit is contained in:
nloginov 2024-07-07 14:23:33 +02:00 committed by GitHub
parent 4c5540a06e
commit 6a2ff0fe72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -66,12 +66,15 @@ def extract_and_fix_urls(text):
def get_imgur_url(imgur_url): def get_imgur_url(imgur_url):
try: try:
# Format for Imgur images
fmt_url = r'https://i\.imgur\.com/\w+\.(?:jpg|png|gif|mp4)'
# Handle album URLs # Handle album URLs
if '/a/' in imgur_url: if '/a/' in imgur_url:
response = requests.get(imgur_url, timeout=10) response = requests.get(imgur_url, timeout=10)
response.raise_for_status() response.raise_for_status()
# Extract all image URLs from the album page # Extract all image URLs from the album page
image_urls = re.findall(r'https://i\.imgur\.com/\w+\.(?:jpg|png|gif|mp4)', response.text) image_urls = re.findall(fmt_url, response.text)
return image_urls if image_urls else None return image_urls if image_urls else None
# Handle single image/video URLs # Handle single image/video URLs
@ -80,7 +83,7 @@ def get_imgur_url(imgur_url):
content = response.text content = response.text
# Try to find direct image/video URL in the page source # Try to find direct image/video URL in the page source
match = re.search(r'https://i\.imgur\.com/\w+\.(?:jpg|png|gif|mp4)', content) match = re.search(fmt_url, content)
if match: if match:
return match.group(0) return match.group(0)