git ssb

0+

Grey the earthling / scuttleblog



Tree: 7f52efdb80cbc9ef650fe516df888c342d967cdd

Files: 7f52efdb80cbc9ef650fe516df888c342d967cdd / scuttleblog.py

4202 bytesRaw
1#!/usr/bin/python3
2
3__copyright__ = """
4
5 Scuttleblog
6
7 Copyright (C) 2017 Greg K Nicholson
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Affero General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Affero General Public License for more details.
18
19 You should have received a copy of the GNU Affero General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22"""
23
24import datetime
25import json
26import os
27import re
28import time
29import subprocess
30
31import conf
32
33def run_sbot():
34 for attempt in range(100):
35 try:
36 subprocess.check_output(['sbot', 'status'])
37 except:
38 pid = subprocess.Popen(['sbot', 'server']).pid
39 time.sleep(1)
40 else:
41 break
42
43def get_user_posts(ssb_userid):
44 user_posts_args = ['sbot', 'createUserStream', '--id', ssb_userid]
45 json_posts_args = ['json', '--group', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null']
46 user_posts_stream = subprocess.Popen(user_posts_args, stdout = subprocess.PIPE)
47 user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout)
48 user_posts = json.loads(user_posts_json)
49 return user_posts
50
51def define_post_title(text):
52 title = text.strip().splitlines()[0]
53 title = re.sub('^\W+', '', title)
54 titleparts = re.split('([.?!\[\]])', title)
55 return titleparts[0] + titleparts[1] if len(titleparts) > 1 else titleparts[0]
56
57def build_post_structure(p):
58 post = {}
59 post['frontmatter'] = {}
60 post['frontmatter']['key'] = p['key']
61 post['frontmatter']['title'] = define_post_title(p['value']['content']['text'])
62 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
63 post['frontmatter']['sequence'] = p['value']['sequence']
64 post['text'] = p['value']['content']['text']
65 return (post)
66
67def format_post_file(post):
68 content = ''
69 content += str(json.dumps(post['frontmatter'], indent = 4))
70 content += '\n\n'
71 content += str(post['text'])
72 return content
73
74def define_post_filename(post):
75 folder = 'hugo/content/posts'
76 #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','')
77 slug = str(post['frontmatter']['sequence'])
78 filetype = 'md'
79 return folder + '/' + slug + '.' + filetype
80
81def write_post_file(post):
82 os.makedirs(os.path.dirname(define_post_filename(post)), exist_ok=True)
83 with open(define_post_filename(post), 'w') as f:
84 f.write (format_post_file(post))
85
86def write_posts_from_user(ssb_userid):
87 posts = get_user_posts(ssb_userid)
88 for post in posts:
89 write_post_file(build_post_structure(post))
90
91
92def get_user_metadata(ssb_userid):
93 user_metadata_args = ['sbot', 'links', '--source', ssb_userid, '--dest', ssb_userid, '--rel', 'about', '--values']
94 json_metadata_args = ['json', '--deep-merge', '-c', 'this.value.content.type == "about"']
95 user_metadata_stream = subprocess.Popen(user_metadata_args, stdout = subprocess.PIPE)
96 user_metadata_json = subprocess.check_output(json_metadata_args, stdin = user_metadata_stream.stdout)
97 user_metadata = json.loads(user_metadata_json)
98 return user_metadata
99
100def build_hugo_config(m):
101 hugo_config = {}
102 if 'name' in m['value']['content']:
103 hugo_config['title'] = m['value']['content']['name']
104 hugo_config['theme'] = conf.hugo_theme
105 hugo_config['params'] = {}
106 if 'description' in m['value']['content']:
107 hugo_config['params']['subtitle'] = m['value']['content']['description'].replace('\n', ' ')
108 return (hugo_config)
109
110def write_hugo_config_from_user(ssb_userid):
111 metadata = get_user_metadata(ssb_userid)
112 with open('hugo/config.json', 'w') as f:
113 f.write (json.dumps(build_hugo_config(metadata), indent = 4))
114 # TODO: add the user's image in the right place
115
116def run_hugo():
117 subprocess.run(['hugo', '-s', 'hugo'])
118
119run_sbot()
120write_posts_from_user(conf.ssb_userid)
121write_hugo_config_from_user(conf.ssb_userid)
122run_hugo()
123
124

Built with git-ssb-web