This commit is contained in:
commit
c185dbb075
1 changed files with 85 additions and 0 deletions
85
facebook2wordpress.py
Normal file
85
facebook2wordpress.py
Normal file
|
@ -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('')
|
Loading…
Reference in a new issue