git ssb

0+

Grey the earthling / scuttleblog



Tree: cd701886292dd0144e10889ae3995adc117bd334

Files: cd701886292dd0144e10889ae3995adc117bd334 / scuttleblog.py

5185 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
33scuttleblog_dir = os.path.dirname(__file__)
34
35def run_sbot():
36 for attempt in range(100):
37 try:
38 subprocess.check_output(['sbot', 'status'])
39 except:
40 pid = subprocess.Popen(['sbot', 'server']).pid
41 time.sleep(1)
42 else:
43 break
44
45def get_user_posts(ssb_userid):
46 user_posts_args = ['sbot', 'createUserStream', '--id', ssb_userid]
47 json_posts_args = ['json', '--group', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null']
48 user_posts_stream = subprocess.Popen(user_posts_args, stdout = subprocess.PIPE)
49 user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout)
50 user_posts = json.loads(user_posts_json)
51 return user_posts
52
53def define_post_text(text):
54 text = text.strip()
55 title = text.splitlines()[0]
56 if re.match('#', text):
57 # The first line is a heading. Use it as the title and strip it from the body.
58 body = ''.join(text.splitlines(keepends=True)[1:]).strip() \
59 if len(text.splitlines()) > 1 \
60 else ''
61 else:
62 # Truncate the first line and use it as the title. Keep everything in the body.
63 maxlength = 100
64 ellipsis = '...'
65 title = title if len(title) <= maxlength else title[:maxlength].rsplit(' ', 1)[0] + ellipsis
66 body = text
67 title = re.sub('^#+\s+', '', title)
68 # FIXME: This doesn't gracefully handle markdown near the start of a post.
69 return {'title': title, 'body': body}
70
71def build_post_structure(p):
72 post = {}
73 post['frontmatter'] = {}
74 post['frontmatter']['key'] = p['key']
75 post['frontmatter']['title'] = define_post_text(p['value']['content']['text'])['title']
76 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
77 post['frontmatter']['sequence'] = p['value']['sequence']
78 post['body'] = define_post_text(p['value']['content']['text'])['body']
79 return (post)
80
81def format_post_file(post):
82 content = ''
83 content += str(json.dumps(post['frontmatter'], indent = 4))
84 content += '\n\n'
85 content += str(post['body'])
86 return content
87
88def define_post_filename(post):
89 folder = 'hugo/content/posts'
90 #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','')
91 slug = str(post['frontmatter']['sequence'])
92 filetype = 'md'
93 return folder + '/' + slug + '.' + filetype
94
95def write_post_file(post):
96 os.makedirs(os.path.dirname(define_post_filename(post)), exist_ok=True)
97 with open(define_post_filename(post), 'w') as f:
98 f.write (format_post_file(post))
99
100def write_posts_from_user(ssb_userid):
101 posts = get_user_posts(ssb_userid)
102 for post in posts:
103 write_post_file(build_post_structure(post))
104
105
106def get_user_metadata(ssb_userid):
107 user_metadata_args = ['sbot', 'links', '--source', ssb_userid, '--dest', ssb_userid, '--rel', 'about', '--values']
108 json_metadata_args = ['json', '--deep-merge', '-c', 'this.value.content.type == "about"']
109 user_metadata_stream = subprocess.Popen(user_metadata_args, stdout = subprocess.PIPE)
110 user_metadata_json = subprocess.check_output(json_metadata_args, stdin = user_metadata_stream.stdout)
111 user_metadata = json.loads(user_metadata_json)
112 return user_metadata
113
114def check_hugo_theme(theme, theme_clone_url):
115 hugo_theme_dir = os.path.join(scuttleblog_dir, 'hugo', 'themes', theme)
116 if not os.path.isdir(hugo_theme_dir):
117 subprocess.run(['git', 'submodule', 'add', '-f', theme_clone_url, hugo_theme_dir])
118
119def build_hugo_config(m):
120 hugo_config = {}
121 hugo_config['baseurl'] = conf.hugo_baseurl
122 hugo_config['theme'] = conf.hugo_theme
123 if 'name' in m['value']['content']:
124 hugo_config['title'] = m['value']['content']['name']
125 hugo_config['params'] = {}
126 if 'description' in m['value']['content']:
127 hugo_config['params']['subtitle'] = m['value']['content']['description'].replace('\n', ' ')
128 return (hugo_config)
129
130def write_hugo_config_from_user(ssb_userid):
131 metadata = get_user_metadata(ssb_userid)
132 for file in ['hugo/config.toml', 'hugo/config.yaml']:
133 if os.path.exists(file):
134 os.remove(file)
135 with open('hugo/config.json', 'w') as f:
136 f.write (json.dumps(build_hugo_config(metadata), indent = 4))
137 # TODO: add the user's image in the right place
138
139def run_hugo():
140 subprocess.run(['hugo', '-s', 'hugo'])
141
142run_sbot()
143write_posts_from_user(conf.ssb_userid)
144write_hugo_config_from_user(conf.ssb_userid)
145check_hugo_theme(conf.hugo_theme, conf.hugo_theme_clone_url)
146run_hugo()
147
148

Built with git-ssb-web