-
Notifications
You must be signed in to change notification settings - Fork 0
/
bs_add.py
executable file
·108 lines (92 loc) · 4.05 KB
/
bs_add.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
###########################################################################
# #
# Copyright (c) 2011, Simon David Pratt <me@simondavidpratt.com> #
# #
# Permission to use, copy, modify, and/or distribute this software #
# for any purpose with or without fee is hereby granted, provided #
# that the above copyright notice and this permission notice appear #
# in all copies. #
# #
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL #
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED #
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE #
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR #
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM #
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, #
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN #
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #
# #
###########################################################################
###########################################################################
# imports
from sys import argv
import os.path
from common import read_until_eof
import json
###########################################################################
# check proper usage
if len(argv) < 3:
print 'Usage:', argv[0], '{index_file} {post_file}'
exit()
###########################################################################
# check post file exists
post_filename = argv[2]
if not os.path.exists(post_filename):
print 'File not found: ',post_filename
exit()
###########################################################################
# read in post
post = dict()
with open(post_filename,'rU') as file:
post = json.loads(read_until_eof(file))
# remove text section
post.pop('text')
###########################################################################
# create or read in the index
index_filename = argv[1]
index = dict(posts=[],posts_by_url={},posts_by_date={},posts_by_tag={},posts_by_filename={},posts_by_author={},prev={},next={})
if os.path.exists(index_filename):
with open(index_filename,'rU') as file:
index = json.loads(read_until_eof(file))
###########################################################################
# check that post filename/url doesn't already exist
if index['posts_by_url'].has_key(post['url']):
print 'url already exists in index: ', post['url']
exit()
if index['posts_by_filename'].has_key(post_filename):
print 'file already exists in index: ', post_filename
exit()
###########################################################################
# add post to index
index['posts'].insert(0,post_filename) # add post to beginning of list
# by filename
index['posts_by_filename'][post_filename] = post
# by url
url = post['url']
index['posts_by_url'][url] = post_filename
# by date
date = post['date']
index['posts_by_date'][date] = post_filename
# by tags
for tag in post['tags']:
if index['posts_by_tag'].has_key(tag):
index['posts_by_tag'][tag].append(post_filename)
else:
index['posts_by_tag'][tag] = [post_filename]
# by author
author = post['author']
if index['posts_by_author'].has_key(author):
index['posts_by_author'][author].append(post_filename)
else:
index['posts_by_author'][author] = [post_filename]
# by next
if len(index['posts']) > 1:
prev = index['posts'][1]
index['next'][prev] = post_filename
index['prev'][post_filename] = prev
###########################################################################
# output index to file
with open(index_filename,'w') as file:
file.write(json.dumps(index))
file.write('\n')