git ssb

0+

Grey the earthling / scuttleblog



Tree: 3b2f28bf55a5e4e16521028ecddad93b96d90a60

Files: 3b2f28bf55a5e4e16521028ecddad93b96d90a60 / scuttleblog.py

2303 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
29userid = '@JoiN9c9+xO0jgu0gg5yflTf+L1peLTBneFHjrKbmcKo=.ed25519'
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:%S%z')
37 post['frontmatter']['sequence'] = p['value']['sequence']
38 post['text'] = p['value']['content']['text']
39 return (post)
40
41def defineFilename(post):
42 folder = 'output'
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(userid)
70
71

Built with git-ssb-web