Skip to content

Instantly share code, notes, and snippets.

@DanielBeckstein
Last active December 14, 2016 16:00
Show Gist options
  • Save DanielBeckstein/2004c0f2f6c05ca2860f709bf2823c71 to your computer and use it in GitHub Desktop.
Save DanielBeckstein/2004c0f2f6c05ca2860f709bf2823c71 to your computer and use it in GitHub Desktop.
Download flickr images by url
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2016 Daniel Beckstein
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# license information: https://opensource.org/licenses/MIT - MIT License
import os
import errno
import urllib
"""
# generates file path if multiple directories are missing
# this function is a quick hack, and not tested very much
def mkdir_all(path, stop_from_end_splits=1):
path_parts = path.split("/")
print "parts"
print path_parts
msg = []
for i, _ in enumerate( path_parts ):
if i < len(path_parts) - stop_from_end_splits:
path_curr = "/".join( path_parts[:i+1] )
msg += [ ("path", "mkdir_all", path_curr) ]
mkdir(path_curr)
print "__mkdir_all__", msg[-1][2]
"""
def mkdir(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def fetch_img_form_url(url, path_out):
url_parts = url.split("/")
fname = url_parts[-1]
path_out = path_out + fname
if 1:
print "url", url
print "fname", fname
print "path_out", path_out
urllib.urlretrieve(url, path_out)
def main():
# there are multiple alternate file name endings like this "_m.jpg"
# which denote the file size of the image if you need other resolutions see
# changing the file size is not possible by just switching _m to _o (for original largest file size)
# you need to get a differnt url, you can get the urls from twitter api responses, see:
# https://www.flickr.com/services/api/flickr.photos.getSizes.html
urls = []
urls += [ "https://farm3.staticflickr.com/2950/15232292419_e3b1846217_m.jpg" ]
urls += [ "https://farm4.staticflickr.com/3953/15613249585_d1e45f2ee5_m.jpg" ]
path_output_folder = "imgs/"
print "STATUS - storing images in", path_output_folder
# makes sure path_out gets created if not exists
# this is needed in python
mkdir(path_output_folder)
for url in urls:
fetch_img_form_url(url, path_output_folder)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment