#!/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 re import time import subprocess import conf def run_sbot(): for attempt in range(100): try: subprocess.check_output(['sbot', 'status']) except: pid = subprocess.Popen(['sbot', 'server']).pid time.sleep(1) else: break def get_user_posts(userid): user_posts_args = ['sbot', 'createUserStream', '--id', userid] json_posts_args = ['json', '--group', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null'] user_posts_stream = subprocess.Popen(user_posts_args, stdout = subprocess.PIPE) user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout) user_posts = json.loads(user_posts_json) return user_posts def define_post_title(text): title = text.strip().splitlines()[0] title = re.sub('^\W+', '', title) titleparts = re.split('([.?!])', title) return titleparts[0] + titleparts[1] if len(titleparts) > 1 else titleparts[0] def build_post_structure(p): post = {} post['frontmatter'] = {} post['frontmatter']['key'] = p['key'] post['frontmatter']['title'] = define_post_title(p['value']['content']['text']) 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 format_post_file(post): content = '' content += str(json.dumps(post['frontmatter'], indent = 4)) content += '\n\n' content += str(post['text']) return content def define_post_filename(post): folder = 'hugo/content/posts' #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','') slug = str(post['frontmatter']['sequence']) filetype = 'md' return folder + '/' + slug + '.' + filetype def write_post_file(post): os.makedirs(os.path.dirname(define_post_filename(post)), exist_ok=True) with open(define_post_filename(post), 'w') as f: f.write (format_post_file(post)) def write_posts_from_user(userid): posts = get_user_posts(userid) for post in posts: write_post_file(build_post_structure(post)) def get_user_metadata(userid): user_metadata_args = ['sbot', 'links', '--source', userid, '--dest', userid, '--rel', 'about', '--values'] json_metadata_args = ['json', '--deep-merge', '-c', 'this.value.content.type == "about"'] user_metadata_stream = subprocess.Popen(user_metadata_args, stdout = subprocess.PIPE) user_metadata_json = subprocess.check_output(json_metadata_args, stdin = user_metadata_stream.stdout) user_metadata = json.loads(user_metadata_json) return user_metadata def build_hugo_config(m): hugo_config = {} hugo_config['title'] = m['value']['content']['name'] hugo_config['theme'] = 'ananke' # FIXME hugo_config['params'] = {} hugo_config['params']['subtitle'] = m['value']['content']['description'].replace('\n', ' ') return (hugo_config) def write_hugo_config_from_user(userid): metadata = get_user_metadata(userid) with open('hugo/config.json', 'w') as f: f.write (json.dumps(build_hugo_config(metadata), indent = 4)) # TODO: add the user's image in the right place def run_hugo(): subprocess.run(['hugo', '-s', 'hugo']) run_sbot() write_posts_from_user(conf.userid) write_hugo_config_from_user(conf.userid) run_hugo()