0

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.')
    


Pelmz
  • 1
  • 1
  • What operating system are you using? Have you tried using a specific program to open the file? What is the full name of the file after downloading? (Your OS may be hiding parts of the name including the real file extension) – Hymns For Disco Dec 04 '20 at 12:20
  • I'm using Win10. If I dont add .jpg to the filename in the code, it just shows up as a file and the type as "FILE". Tried different means of opening it but no luck. The goal would be that it would just write it as a usable jpg file.. – Pelmz Dec 04 '20 at 12:44
  • It could be that the image file is corrupted/invalid. Try opening it in various image viewers/editors to see if they give a useful error message. Check the urls in your `pic_urls` to see if they're correct and lead directly to a jpg resource. Try other methods of downloading the image https://stackoverflow.com/questions/30229231/python-save-image-from-url/30229298 – Hymns For Disco Dec 05 '20 at 12:13
  • Cheers for that. Got it working, it was using the original instagram url from the user input instead of the one leading to a jpg. – Pelmz Dec 05 '20 at 18:22

0 Answers0