Skip to content

Instantly share code, notes, and snippets.

@cyrildiagne
Last active November 17, 2015 10:30
Show Gist options
  • Save cyrildiagne/1aaff4e857bc6296cf14 to your computer and use it in GitHub Desktop.
Save cyrildiagne/1aaff4e857bc6296cf14 to your computer and use it in GitHub Desktop.
# adds collection names in metadatas jsons.
import csv
import os
import json
from Tkinter import Tk
import tkFileDialog
import io
collectionsFileName = "allCollectionNames.csv"
jsonFolder = ""
def run():
collectionIds = []
collectionNames = []
missingCollections = []
Tk().withdraw() # Remove TKinter default window
# jsonFolder = tkFileDialog.askdirectory()+"/" # select jsons folder
jsonFolder = "/Users/bastienGirschig/Downloads/jsonMini 2/"
print "updating "+jsonFolder+"..."
# read the collectionNames file, fill collectionIds/collectionNames array with it
with open(collectionsFileName, "r") as collectionsFile:
csvReader = csv.reader(collectionsFile)
for row in csvReader:
collectionIds.append(row[0])
collectionNames.append(row[1])
# loop through json files, and add collectionName item to them
for fileName in os.listdir(jsonFolder):
if os.path.splitext(fileName)[1] == ".json":
modified = False
with open(jsonFolder+fileName) as jsonFile:
jsonData = json.load(jsonFile)
byteify(jsonData)
if("collectionId" in jsonData.keys()):
if jsonData["collectionId"] in collectionIds:
collectionIndex = collectionIds.index(jsonData["collectionId"])
jsonData["collection"] = {'Id':jsonData["collectionId"], 'name':collectionNames[collectionIndex]}
del jsonData["collectionId"]
modified = True
else:
if(jsonData["collectionId"] not in missingCollections):
missingCollections.append(jsonData["collectionId"])
else:
print("json file "+fileName+" does not contain a 'collectionId' key")
if(modified):
modifiedFile = json.dumps(jsonData, ensure_ascii=False, encoding='utf8')
with io.open(jsonFolder+fileName, 'w', encoding='utf-8') as jsonFile:
print modifiedFile
jsonFile.write(modifiedFile)
print(fileName+" updated")
if(len(missingCollections)>0):
print ("\n=====================\nmissing collections:\n")
for collection in missingCollections: print("\t"+collection)
print("done")
def byteify(input):
if isinstance(input, dict):
return dict([(byteify(key), byteify(value)) for key, value in input.iteritems()])
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment