Skip to content

Instantly share code, notes, and snippets.

@ohwgiles
Last active December 14, 2015 10:18
Show Gist options
  • Save ohwgiles/5070589 to your computer and use it in GitHub Desktop.
Save ohwgiles/5070589 to your computer and use it in GitHub Desktop.
python script to send a facebook photo album as an email (for those that don't use facebook)
#!/usr/bin/python
msg = "<html><head></head><body>"
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from urllib import request
import json
import os
# get an access token at https://developers.facebook.com/tools/explorer
access_token="long-access-token"
r = request.urlopen("https://graph.facebook.com/me/albums?access_token=%s" % access_token)
j = json.loads(r.read().decode('utf8'))
print("Select an album")
for i,album in enumerate(j['data']):
print("%d. %s" % (i,album['name']))
album = j['data'][int(input())]
e = open(album['name']+".eml","w")
r = request.urlopen("https://graph.facebook.com/%s/photos?access_token=%s&limit=999" % (album['id'], access_token))
j = json.loads(r.read().decode('utf8'))
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = album['name']
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgBody = MIMEText('','html')
msgRoot.attach(msgBody)
for i,image in enumerate(j['data']):
msg += "<img src='cid:fb_image%d'><br>" % i
print("downloading %s..." % image['source'])
r = request.urlopen(image['source'])
msgImage = MIMEImage(r.read())
msgImage.add_header('Content-ID', 'fb_image%d' % i)
msgImage.add_header('Content-Disposition', 'inline', filename=os.path.basename(image['source']))
msgRoot.attach(msgImage)
try:
name = image['name']
print(name)
msg += name + "<br>"
except KeyError:
print("<no caption>")
pass
msg += "<br>"
msg += "</body></html>"
msgBody.set_payload(msg,'utf-8')
e.write(str(msgRoot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment