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.py | changed |
scuttleblog.py | |||
---|---|---|---|
@@ -23,48 +23,82 @@ | |||
23 | 23 … | ||
24 | 24 … | import datetime | |
25 | 25 … | import json | |
26 | 26 … | import os | |
27 … | +import re | ||
27 | 28 … | import subprocess | |
28 | 29 … | ||
29 | 30 … | import conf | |
30 | 31 … | ||
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): | ||
32 | 47 … | post = {} | |
33 | - post['sequence'] = p['value']['sequence'] | ||
34 | 48 … | post['frontmatter'] = {} | |
35 | 49 … | post['frontmatter']['key'] = p['key'] | |
50 … | + post['frontmatter']['title'] = define_post_title(p['value']['content']['text']) | ||
36 | 51 … | post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') | |
37 | 52 … | post['frontmatter']['sequence'] = p['value']['sequence'] | |
38 | 53 … | post['text'] = p['value']['content']['text'] | |
39 | 54 … | return (post) | |
40 | 55 … | ||
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): | ||
49 | 57 … | content = '' | |
50 | 58 … | content += str(json.dumps(post['frontmatter'], indent=4)) | |
51 | 59 … | content += '\n\n' | |
52 | 60 … | content += str(post['text']) | |
53 | 61 … | return content | |
54 | 62 … | ||
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 | ||
59 | 69 … | ||
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)) | ||
70 | 74 … | ||
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