Skip to content

Commit

Permalink
Adding a Facebook downloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelclay committed Apr 7, 2015
1 parent 8ac2f36 commit dc799dd
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -179,7 +179,7 @@ Once those are installed, save this script as `download_flickr.py`
Add this to your crontab with `crontab -e`:

0 * * * * python /home/pi/photoframe/download_flickr.py
30 * * * * FLICKR_TAG /home/pi/photoframe/download_koalas.sh
30 * * * * FLICKR_TAG=koala /home/pi/photoframe/download_koalas.sh

### Automatic start of the photo frame software

Expand Down
111 changes: 111 additions & 0 deletions download_facebook.py
@@ -0,0 +1,111 @@
from urllib import urlopen
from json import loads
from sys import argv
import dateutil.parser as dateparser
import logging

# plugin your username and access_token (Token can be get and
# modified in the Explorer's Get Access Token button):
# https://graph.facebook.com/USER_NAME/photos?type=uploaded&fields=source&access_token=ACCESS_TOKEN_HERE
FACEBOOK_USER_ID = "10203552316662505"
FACEBOOK_ACCESS_TOKEN = "CAACEdEose0cBAKtF3S5WUN4qSRxljDPWoB28DJmbsd6O71Nz7REORmZAGYrYD8wW8kNE3CDdZBFxZCu4aMQZBtz5XLftzLjlnVjNZBu7TNW6YCEKdJXEVS5ZAZA8mMEypQLrXviYrj68QFdpeaaqiIZBjxSQ0tgZB9x8ME6LjZCUXycZBLzZADUnTN75kjLUNPidmKbTlj5OhEEvnWmkU4ATFCVkUz83JfcHhNkZD"

def get_logger(label='lvm_cli', level='INFO'):
"""
Return a generic logger.
"""
format = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(format=format)
logger = logging.getLogger(label)
logger.setLevel(getattr(logging, level))
return logger

def urlrequest(url):
"""
Make a url request
"""
req = urlopen(url)
return req.read()

def get_json(url):
"""
Make a url request and return as a JSON object
"""
res = urlrequest(url)
return loads(res)

def get_next(data):
"""
Get next element from facebook JSON response,
or return None if no next present.
"""
try:
return data['paging']['next']
except KeyError:
return None

def get_images(data):
"""
Get all images from facebook JSON response,
or return None if no data present.
"""
try:
return data['data']
except KeyError:
return []

def get_all_images(url):
"""
Get all images using recursion.
"""
data = get_json(url)
images = get_images(data)
next = get_next(data)

if not next:
return images
else:
return images + get_all_images(next)

def get_url(userid, access_token):
"""
Generates a useable facebook graph API url
"""
root = 'https://graph.facebook.com/'
endpoint = '%s/photos?type=uploaded&fields=source,updated_time&access_token=%s' % \
(userid, access_token)
return '%s%s' % (root, endpoint)

def download_file(url, filename):
"""
Write image to a file.
"""
data = urlrequest(url)
path = '/home/pi/photoframe/facebook/%s' % filename
f = open(path, 'w')
f.write(data)
f.close()

def create_time_stamp(timestring):
"""
Creates a pretty string from time
"""
date = dateparser.parse(timestring)
return date.strftime('%Y-%m-%d-%H:%M:%S')

def download(userid, access_token):
"""
Download all images to current directory.
"""
logger = get_logger()
url = get_url(userid, access_token)
logger.info('Requesting image direct link, please wait..')
images = get_all_images(url)

for image in images:
logger.info('Downloading %s' % image['source'])
filename = '%s.jpg' % create_time_stamp(image['created_time'])
download_file(image['source'], filename)

if __name__ == '__main__':
download(FACEBOOK_USER_ID, FACEBOOK_ACCESS_TOKEN)

0 comments on commit dc799dd

Please sign in to comment.