Last active
February 22, 2019 10:57
-
-
Save petermolnar/d0df06f88cd5247a2be4112b45b7f823 to your computer and use it in GitHub Desktop.
facebook json export to wp-cli import bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-cli post create --porcelain --post_category='Facebook' --post_date='%s' --post_content=%s --post_title='%s')" % ( | |
dt.format('YYYY-MM-DD HH:mm:ss'), | |
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 | |
)) | |
out.append("wp-cli post update ${post_id} --post_content=%s --post_status='publish' --post_author='4'" % ( | |
quote("%s\n\n[gallery]" % content) | |
)) | |
out.append("wp-cli post update ${post_id} --post_date='%s'" % ( | |
dt.format('YYYY-MM-DD HH:mm:ss') | |
)) | |
if len(out) == 1: | |
continue | |
else: | |
for l in out: | |
print(l) | |
print('') | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment