#!/usr/bin/python3 __copyright__ = """ Scuttleblog Copyright (C) 2017 Greg K Nicholson This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . """ import datetime import json import os import subprocess import conf def restructure(p): post = {} post['sequence'] = p['value']['sequence'] post['frontmatter'] = {} post['frontmatter']['key'] = p['key'] post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') post['frontmatter']['sequence'] = p['value']['sequence'] post['text'] = p['value']['content']['text'] return (post) def defineFilename(post): folder = 'hugo/content/posts' #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','') slug = str(post['sequence']) filetype = 'md' return folder + '/' + slug + '.' + filetype def formatContent(post): content = '' content += str(json.dumps(post['frontmatter'], indent=4)) content += '\n\n' content += str(post['text']) return content def writePostFile(post): os.makedirs(os.path.dirname(defineFilename(post)), exist_ok=True) with open(defineFilename(post), 'w') as f: f.write (formatContent(post)) def getUserStream(userid): userStreamArgs = ['sbot', 'createUserStream', '--id', userid] jsonFilterArgs = ['json', '-g', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null'] userStream = subprocess.Popen(userStreamArgs, stdout=subprocess.PIPE) posts = subprocess.check_output(jsonFilterArgs, stdin = userStream.stdout) jsonPosts = json.loads(posts) for post in jsonPosts: writePostFile(restructure(post)) getUserStream(conf.userid)