-
Notifications
You must be signed in to change notification settings - Fork 15
/
postutils.py
executable file
·44 lines (33 loc) · 1.2 KB
/
postutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import json
import os
import re
import textwrap
def create_dir(dirname, username):
path = os.path.join(dirname, username)
if not os.path.exists(path):
os.makedirs(path)
def save_post_as_text(username, post, dir="data"):
fname = os.path.join(dir, username, post['timestamp'] + '-' + re.sub(r'\s+', '-', post['title']))
print("Filename :: {}".format(fname))
with open(fname, 'w') as f:
f.write(textwrap.fill(post['content'], 140))
def save_post_as_json(username, post, dir="data"):
fname = os.path.join(dir, username, re.sub(r'\s+', '-', post['title']) + ".json")
print("Filename :: {}".format(fname))
with open(fname, 'w') as f:
json.dump(post, f, indent=4)
def save_json_single(posts, dir="data"):
fname = os.path.join(dir, "dump.json")
with open(fname, 'w') as f:
json.dump(posts, f, indent=4)
def save_posts(posts, dir="data", dump_type='text'):
print("Dumping all the shit...")
print("Total number of posts :: {}".format(len(posts)))
func = save_post_as_text if dump_type=='text' else save_post_as_json
for post in posts:
func(post, dir)
def main():
pass
if __name__ == "__main__":
main()