From c185dbb07507c250aec73abfcf8bb90f11fa5781 Mon Sep 17 00:00:00 2001 From: Peter Molnar Date: Fri, 22 Feb 2019 10:16:15 +0000 Subject: [PATCH] --- facebook2wordpress.py | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 facebook2wordpress.py diff --git a/facebook2wordpress.py b/facebook2wordpress.py new file mode 100644 index 0000000..9e5a1dd --- /dev/null +++ b/facebook2wordpress.py @@ -0,0 +1,85 @@ +import json +import arrow +import os +import logging +from shlex import quote + +from pprint import pprint + +base = '/web/blog.pakuauk.com/exports/facebook-export' +with open('facebook-export/posts/posts.json', 'rt') as f: + data = json.loads(f.read()) +data = data['status_updates'] + + +for c, post in enumerate(data): + #print('--- doing %d ---' % (c)) + if 'title' not in post: + #logging.error('#%s no title', c) + continue + if 'new photo' not in post['title']: + #logging.error('#%s not a photo', c) + continue + if 'attachments' not in post: + #logging.error('#%s no attachments', c) + continue + if 'data' not in post['attachments'][0]: + #logging.error('#%s no data n attachments[0]', c) + continue + if 'data' not in post: + #logging.error('#%s no data in post', c) + continue + if not len (post['attachments'][0]['data']): + #logging.error('#%s no attachments', c) + continue + + out = [] + dt = arrow.get(post['timestamp']) + content = '' + if 'post' in post['data'][0]: + content = post['data'][0]['post'] + + if len(post['attachments'][0]['data']) == 1: + t = 'image' + else: + t = 'gallery' + + + cmd = "post_id=$(wp post create --porcelain --post_category='Facebook' --post_date='%s' --post_content=%s --post_title='%s')" % ( + str(dt), + quote(content), + dt.format('YYYY-MM-DD HH:mm') + ) + out.append(cmd) + + for cntr, media in enumerate(post['attachments'][0]['data']): + media = media['media'] + + if 'uri' not in media: + logging.error('missing uri') + continue + fpath = "%s/%s" % (base,media['uri']) + fdt = arrow.get(media['creation_timestamp']) + if 'description' in media: + fcontent = media['description'] + elif 'post' in post['data'][0]: + fcontent = post['data'][0]['post'] + + if cntr == 0: + f = '--featured_image' + else: + f = '' + out.append("wp-cli media import %s --post_id=${post_id} --title='%s' --caption=%s %s" % ( + fpath, + os.path.basename(media['uri']), + quote(fcontent), + f + )) + + if len(out) == 1: + continue + else: + for l in out: + print(l) + print('') + print('')