git ssb

0+

Grey the earthling / scuttleblog



Tree: c12c00615ebc62ecf11607d4aef9cadefefd5030

Files: c12c00615ebc62ecf11607d4aef9cadefefd5030 / scuttleblog.py

3769 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 subprocess
29
30import conf
31
32def get_user_posts(userid):
33 user_posts_args = ['sbot', 'createUserStream', '--id', userid]
34 json_posts_args = ['json', '--group', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null']
35 user_posts_stream = subprocess.Popen(user_posts_args, stdout=subprocess.PIPE)
36 user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout)
37 user_posts = json.loads(user_posts_json)
38 return user_posts
39
40def define_post_title(text):
41 title = text.strip().splitlines()[0]
42 title = re.sub('^\W+', '', title)
43 titleparts = re.split('([.?!])', title)
44 return titleparts[0] + titleparts[1] if len(titleparts) > 1 else titleparts[0]
45
46def build_post_structure(p):
47 post = {}
48 post['frontmatter'] = {}
49 post['frontmatter']['key'] = p['key']
50 post['frontmatter']['title'] = define_post_title(p['value']['content']['text'])
51 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
52 post['frontmatter']['sequence'] = p['value']['sequence']
53 post['text'] = p['value']['content']['text']
54 return (post)
55
56def format_post_file(post):
57 content = ''
58 content += str(json.dumps(post['frontmatter'], indent=4))
59 content += '\n\n'
60 content += str(post['text'])
61 return content
62
63def define_post_filename(post):
64 folder = 'hugo/content/posts'
65 #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','')
66 slug = str(post['frontmatter']['sequence'])
67 filetype = 'md'
68 return folder + '/' + slug + '.' + filetype
69
70def write_post_file(post):
71 os.makedirs(os.path.dirname(define_post_filename(post)), exist_ok=True)
72 with open(define_post_filename(post), 'w') as f:
73 f.write (format_post_file(post))
74
75def write_posts_from_user(userid):
76 posts = get_user_posts(userid)
77 for post in posts:
78 write_post_file(build_post_structure(post))
79
80write_posts_from_user(conf.userid)
81
82def get_user_metadata(userid):
83 user_metadata_args = ['sbot', 'links', '--source', userid, '--dest', userid, '--rel', 'about', '--values']
84 json_metadata_args = ['json', '--deep-merge', '-c', 'this.value.content.type == "about"']
85 user_metadata_stream = subprocess.Popen(user_metadata_args, stdout=subprocess.PIPE)
86 user_metadata_json = subprocess.check_output(json_metadata_args, stdin = user_metadata_stream.stdout)
87 user_metadata = json.loads(user_metadata_json)
88 return user_metadata
89
90def build_hugo_config(m):
91 hugo_config = {}
92 hugo_config['title'] = m['value']['content']['name']
93 hugo_config['theme'] = 'ananke' # FIXME
94 hugo_config['params'] = {}
95 hugo_config['params']['subtitle'] = m['value']['content']['description'].replace('\n', ' ')
96 return (hugo_config)
97
98def write_hugo_config_from_user(userid):
99 metadata = get_user_metadata(userid)
100 with open('hugo/config.json', 'w') as f:
101 f.write (json.dumps(build_hugo_config(metadata)))
102 # TODO: add the user's image in the right place
103
104write_hugo_config_from_user(conf.userid)
105

Built with git-ssb-web