git ssb

0+

Grey the earthling / scuttleblog



Tree: c07cfead2153ab79cd0b2afd920ae54887585bce

Files: c07cfead2153ab79cd0b2afd920ae54887585bce / scuttleblog.py

2266 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 subprocess
28
29import conf
30
31def restructure(p):
32 post = {}
33 post['sequence'] = p['value']['sequence']
34 post['frontmatter'] = {}
35 post['frontmatter']['key'] = p['key']
36 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
37 post['frontmatter']['sequence'] = p['value']['sequence']
38 post['text'] = p['value']['content']['text']
39 return (post)
40
41def 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
48def formatContent(post):
49 content = ''
50 content += str(json.dumps(post['frontmatter'], indent=4))
51 content += '\n\n'
52 content += str(post['text'])
53 return content
54
55def 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))
59
60def 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
69getUserStream(conf.userid)
70
71

Built with git-ssb-web