I'm making this project for a course and I can't get my Instagram pic downloader program to save the photo in a usable format. Even if I give it .jpg, it doesn't help. Still says "It appears we don't support this file format" when trying to open the picture.
Been stuck on this for a while, I've tried other ways of download too but the downloaded file still cant be used.
Here's the code:
import requests
import re
import shutil
url = input('Enter Instagram Photo URL: ')
def get_response(url):
r = requests.get(url)
while r.status_code != 200:
r.raw.decode_content = True
r = requests.get(url, stream = True)
return r.text
response = get_response(url)
def prepare_urls(matches):
return list({match.replace("\\u0026", "&") for match in matches})
vid_matches = re.findall('"video_url":"([^"]+)"', response)
pic_matches = re.findall('"display_url":"([^"]+)"', response)
vid_urls = prepare_urls(vid_matches)
pic_urls = prepare_urls(pic_matches)
if vid_urls:
print('Detected Videos:\n{0}'.format('\n'.join(vid_urls)))
print("Can't download video, the provided URL must be of a picture.")
if pic_urls:
print('Detected Pictures:\n{0}'.format('\n'.join(pic_urls)))
from urllib.request import urlretrieve
dst = 'INSTA.jpg'
urlretrieve(url, dst)
if not (vid_urls or pic_urls):
print('Could not recognize the media in the provided URL.')