Created
October 20, 2015 09:32
-
-
Save jabbalaci/a1bf5cafc223ac887db0 to your computer and use it in GitHub Desktop.
test if a URL is an image or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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