utils: Add flag to store json.

This commit is contained in:
Vishnu KS
2019-10-12 18:49:19 +05:30
parent 08b11eb478
commit 5e02e59123

View File

@@ -1,3 +1,5 @@
import simplejson
# we assume that every line after # Books # we assume that every line after # Books
# starting with * is a book title if file type is old # starting with * is a book title if file type is old
# starting with | (and not with | Name or |--) is a book if the file type is new # starting with | (and not with | Name or |--) is a book if the file type is new
@@ -26,6 +28,12 @@ try:
action='store_true', action='store_true',
default=False default=False
) )
parser.add_argument(
'--store-json',
dest='store-json',
action='store_true',
default=False
)
flags = parser.parse_args() flags = parser.parse_args()
except ImportError: except ImportError:
flags = None flags = None
@@ -49,14 +57,15 @@ def main():
input_file_type = flags.input_file_type or 'new' input_file_type = flags.input_file_type or 'new'
sort_by = flags.sort_by or 'rating' sort_by = flags.sort_by or 'rating'
force = flags.force force = flags.force
store_json = flags.store_json
reverse = True if sort_by == 'rating' else False reverse = True if sort_by == 'rating' else False
library = load(in_file, input_file_type) library = load(in_file, input_file_type)
get_goodread_info(library, force) get_goodread_info(library, force)
library = sort(library, sort_by, reverse) library = sort(library, sort_by, reverse)
render(in_file, out_file, library) render(in_file, out_file, library)
if store_json:
with open("out.json", "w") as f:
f.write(simplejson.dumps(library, indent=4, sort_keys=True))
if __name__ == '__main__': if __name__ == '__main__':
main() main()