Created
November 15, 2016 14:54
-
-
Save cdent/1254a9c98d8ba316e6c0223f9dd03925 to your computer and use it in GitHub Desktop.
a tool to pull the binaries out of a fat json tiddlers collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Licensed under the Apache License, Version 2.0 (the "License"); you may | |
# not use this file except in compliance with the License. You may obtain | |
# a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
# License for the specific language governing permissions and limitations | |
# under the License. | |
import base64 | |
import json | |
import os | |
import sys | |
def process(filename): | |
"""Read a fat JSON tiddlers collection and dump out any images. | |
The output file is named $bag.name-${tiddler.title}.${type}. | |
""" | |
with open(filename) as data: | |
try: | |
tiddlers = json.loads(data.read()) | |
except ValueError as exc: | |
# If the file doesn't contain json this can happen. That | |
# can happen if there was a timeout from the server. | |
sys.stderr.write('unable to process %s: %s\n' % (filename, exc)) | |
return | |
for tiddler in tiddlers: | |
if tiddler['type'] and tiddler['type'].startswith('image/'): | |
ext = tiddler['type'].split('/')[1] | |
text_data = tiddler['text'] | |
bag = tiddler['bag'] | |
title = tiddler['title'] | |
binary_data = base64.b64decode(text_data) | |
output_filename = 'binaries/%s-%s.%s' % (bag, title, ext) | |
with open(output_filename, 'wb') as output: | |
output.write(binary_data) | |
print('...wrote %s' % output_filename) | |
if __name__ == '__main__': | |
if not os.path.isdir('binaries'): | |
os.mkdir('binaries') | |
filenames = sys.argv[1:] | |
if not filenames: | |
sys.stderr.write( | |
'provide a list of filenames (or shell glob "*.json") as args\n') | |
sys.exit(1) | |
for filename in filenames: | |
print(filename) | |
process(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment