Q:

Downloading file from imgur using python directly via url

Sometime, links to imgur are not given with the file extension. For example: http://imgur.com/rqCqA. I want to download the file and give it a known name or get it name inside a larger code. The problem is that I don't know the file type, so I don't know what extension to give it. 
How can I achieve this in python or bash?

A:

You should use the Imgur JSON API. Here's an example in Python, using requests: 
import posixpath
import urllib.parse
import requests

url = "http://api.imgur.com/2/image/rqCqA.json"
r = requests.get(url)
img_url = r.json["image"]["links"]["original"]
fn = posixpath.basename(urllib.parse.urlsplit(img_url).path)

r = requests.get(img_url)
with open(fn, "wb") as f:
    f.write(r.content)

A:

I just tried going to the following URLs:

http://imgur.com/rqCqA.jpg
http://imgur.com/rqCqA.png
http://imgur.com/rqCqA.gif

And they all worked. It seems that Imgur stores several types of the same image - you can take your pick.

