Skip to content

Instantly share code, notes, and snippets.

@cleverdevil
Created March 5, 2018 01:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleverdevil/d9c08ddc6eb2da0d060a5f6fe87ddf64 to your computer and use it in GitHub Desktop.
Save cleverdevil/d9c08ddc6eb2da0d060a5f6fe87ddf64 to your computer and use it in GitHub Desktop.
Process fb-export album data, upload the photos to a Micropub media endpoint, and export MF2-JSON data for posts
#!/usr/bin/env python
'''
Conditionally upload all of the photos from an exported Facebook album to a
Micropub-compatible website with a media endpoint, and dump MF2-JSON data
that can later be published to the same Micropub website.
'''
import json
import sys
import os
import requests
img_html_tmpl = '<a href="%(image_url)s">' + \
'<img class="sunlit_image" ' + \
'src="%(image_url)s" alt="" ' + \
'width="600" height="600" /></a>'
# configuration
token = os.environ.get('INDIEAUTH_TOKEN')
endpoint = os.environ.get('MICROPUB_ENDPOINT')
h = {'Authorization': 'Bearer ' + token}
def upload_photo(photo):
files = {'file': ('image.jpg', photo, 'image/jpeg')}
r = requests.post(endpoint, headers=h, files=files)
if r.status_code == 201:
return r.headers['Location']
else:
raise 'Error uploading a file'
def process_album(album):
photos = sorted(os.listdir('../albums/%s' % album['id']))
html = '<p>\n'
for photo in photos:
with open('../albums/%s/%s' % (album['id'], photo), 'rb') as photo_data:
photo_url = upload_photo(photo_data)
print('Uploaded: %s -> %s' % (photo, photo_url))
html += img_html_tmpl % {'image_url': photo_url} + '\n'
html += '</p>'
return html
if __name__ == '__main__':
albums = json.loads(open(sys.argv[1], 'r').read())
os.system('mkdir -p mf2-albums')
for album in albums:
action = raw_input('Process album "%s" [(Y)es, (n)o]?: ' % album['name'])
if action not in ('Y', 'y', ''):
continue
html = process_album(album)
mf2 = {
'type': ['h-entry'],
'properties': {
'name': [album['name']],
'content': [{'html': html}],
'published': [album['created_time']]
}
}
mf2_json = json.dumps(mf2, indent=2)
with open('mf2-albums/%s.json' % album['id'], 'wb') as converted:
converted.write(mf2_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment