Skip to content

Instantly share code, notes, and snippets.

@jabbalaci
Created October 20, 2015 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabbalaci/a1bf5cafc223ac887db0 to your computer and use it in GitHub Desktop.
Save jabbalaci/a1bf5cafc223ac887db0 to your computer and use it in GitHub Desktop.
test if a URL is an image or not
#!/usr/bin/env python2
# encoding: utf-8
"""
test if a URL is an image or not
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import os
from urlparse import urlparse
IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif']
def image_url(url):
o = urlparse(url)
path = o.path
fname = os.path.split(path)[1].lower()
for ext in IMAGE_EXTENSIONS:
if fname.endswith(ext):
return True
#
return False
def main():
url = "https://host/a/b/c/img.jpg"
assert image_url(url) == True
#
url = "https://host/a/b/c/img.jpg?w=10&h=10"
assert image_url(url) == True
#
url = "https://host/img.txt"
assert image_url(url) == False
#
url = "https://host/myjpg.doc"
assert image_url(url) == False
#
url = "http://host/my.jpg.xlsx"
assert image_url(url) == False
##############################################################################
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment