git ssb

0+

Grey the earthling / scuttleblog



Commit 62a28722a0c852a12fa6a50cfad99015be00c5d5

MVP

Posts have titles.
The blog has a name.
Greg K Nicholson committed on 10/15/2017, 12:38:05 AM
Parent: 136baac17175b73d1a5d8684b8e060af7f2b0fd1

Files changed

scuttleblog.pychanged
scuttleblog.pyView
@@ -23,48 +23,82 @@
2323
2424 import datetime
2525 import json
2626 import os
27 +import re
2728 import subprocess
2829
2930 import conf
3031
31-def restructure(p):
32 +def 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 +
40 +def 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 +
46 +def build_post_structure(p):
3247 post = {}
33- post['sequence'] = p['value']['sequence']
3448 post['frontmatter'] = {}
3549 post['frontmatter']['key'] = p['key']
50 + post['frontmatter']['title'] = define_post_title(p['value']['content']['text'])
3651 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
3752 post['frontmatter']['sequence'] = p['value']['sequence']
3853 post['text'] = p['value']['content']['text']
3954 return (post)
4055
41-def defineFilename(post):
42- folder = 'hugo/content/posts'
43- #slug = post['key'].replace('+','-').replace('/','_').replace('=.sha256','')
44- slug = str(post['sequence'])
45- filetype = 'md'
46- return folder + '/' + slug + '.' + filetype
47-
48-def formatContent(post):
56 +def format_post_file(post):
4957 content = ''
5058 content += str(json.dumps(post['frontmatter'], indent=4))
5159 content += '\n\n'
5260 content += str(post['text'])
5361 return content
5462
55-def writePostFile(post):
56- os.makedirs(os.path.dirname(defineFilename(post)), exist_ok=True)
57- with open(defineFilename(post), 'w') as f:
58- f.write (formatContent(post))
63 +def 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
5969
60-def getUserStream(userid):
61- userStreamArgs = ['sbot', 'createUserStream', '--id', userid]
62- jsonFilterArgs = ['json', '-g', '-c', 'this.value.content.type == "post"', '-c', 'this.value.content.root == null']
63- userStream = subprocess.Popen(userStreamArgs, stdout=subprocess.PIPE)
64- posts = subprocess.check_output(jsonFilterArgs, stdin = userStream.stdout)
65- jsonPosts = json.loads(posts)
66- for post in jsonPosts:
67- writePostFile(restructure(post))
68-
69-getUserStream(conf.userid)
70 +def 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))
7074
75 +def 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 +
80 +write_posts_from_user(conf.userid)
81 +
82 +def 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 +
90 +def 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 +
98 +def 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 +
104 +write_hugo_config_from_user(conf.userid)

Built with git-ssb-web