Skip to content

Instantly share code, notes, and snippets.

@Jarvl
Created April 7, 2016 18:34
Show Gist options
  • Save Jarvl/3799acac27283f80641d57804faac9ae to your computer and use it in GitHub Desktop.
Save Jarvl/3799acac27283f80641d57804faac9ae to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import flickrapi
import requests
import os
import re
FLICKR_KEY = "your api key"
FLICKR_SECRET = "your api secret"
USER_ID = "your user id"
SET_ID = "the set (album) id"
def make_url(photo):
# url_template = "http://farm{farm-id}.staticflickr.com/
# {server-id}/{id}_{secret}_[mstzb].jpg"
photo['filename'] = "%(id)s_%(secret)s_z.jpg" % photo
url = ("http://farm%(farm)s.staticflickr.com/%(server)s/%(filename)s"
% photo)
return url, photo['filename']
def main():
#get new imaged from flickr
print " ---> Requesting photos..."
count = 0
update = False
flickr = flickrapi.FlickrAPI(FLICKR_KEY, FLICKR_SECRET)
photos = flickr.walk_set(SET_ID)
for photo in photos:
count += 1
url, filename = make_url(photo.attrib)
path = '/home/pi/photoframe/flickr/%s' % filename
try:
image_file = open(path)
print " ---> Already have %s" % url
except IOError:
print " ---> Downloading %s" % url
r = requests.get(url)
image_file = open(path, 'w')
image_file.write(r.content)
image_file.close()
update = True
#check to see if it needs to remove photos from folder
filelist = os.listdir("/home/pi/photoframe/flickr")
if count < len(filelist):
print " ---> Removing photos"
for f in filelist:
pics = flickr.walk_set(SET_ID)
print f
for pic in pics:
url, filename = make_url(pic.attrib)
matchObj = re.match(f, filename)
if matchObj:
print " ---> Found %s, matched %s" %(f,filename)
break
else:
print " ---> Deleting %s" %f
os.remove("/home/pi/photoframe/flickr/%s" %f)
update = True
#if it added or removed a photo, update slideshow
if update == True:
print " ---> Restarting slideshow"
os.system("killall feh && /home/pi/bin/script_slideshow.sh")
if __name__ == '__main__':
main()
#!/bin/bash
export DISPLAY=:0.0
XAUTHORITY=/home/pi/.Xauthority
feh -q -Z -F -z -Y -D 5 /home/pi/photoframe/flickr
@ArmedToad
Copy link

Hey Danny!

I've actually been working on this same project and here is what I've found searching the web:

The Flickr api uses a variety of labels for this image sizes:

s - square 75x75
q large square
t - thumbnail
m -small
n - small
z - 640x 480
c -medium 800x600
b - large - 1024 x 768
o - original 2400x1800

So in line 16 of the code: photo['filename'] = "%(id)s_%(secret)s_z.jpg" % photo replace the z with another letter to change the download size.

Example: photo['filename'] = "%(id)s_%(secret)s_b.jpg" % photo

For some reason if you attempt to make the download option the original size, It will download a Flickr file not found

Here is where I got the info on the image sizes: https://www.flickr.com/services/api/flickr.photos.getSizes.html

@ArmedToad
Copy link

So far the largest I've found that works would be "h" which gives a large resolution of 1600x1200

@gtdanny
Copy link

gtdanny commented Jun 29, 2016

Thanks so much, that works perfectly!

@rmharrell
Copy link

rmharrell commented Oct 9, 2016

My Pi isn't pulling in my photos from Flickr or booting into full screen mode

@merlinmb
Copy link

Would recommend opening the file as binary, which allows this to work on Windows-based box too. My tests were on a Windows Python installation which caused corrupt jpgs to be downloaded until I added the binary flag 'b'

Line 39:
image_file = open(path, 'wb')

@merlinmb
Copy link

Does anyone have an idea how to retrieve images from a personal photostream (and not just a Set or specific Album)?
The general feed allows you to use Ifttt.com to push new Instagram images to Flickr (https://ifttt.com/recipes/392-automatically-share-your-new-instagram-photos-to-flickr) which has a far easier implementation for photo frames implementations. The photo frame can then display most recent Instagram photos (Instagram seem to have closed their APIs for external app integrations)

@sheridanwendt
Copy link

Everytime I try to run these commands:

export DISPLAY=:0.0
XAUTHORITY=/home/pi/.Xauthority
feh -q -Z -F -z -Y -D 5 /home/pi/photoframe/flickr

I get the error below:
feh ERROR: CAn't open X display. It *is* running, yeah?

Any suggestions would be much appreciated!
-Pi & Linux Newbie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment