Skip to content

Instantly share code, notes, and snippets.

@dwightjl
Last active March 23, 2022 14:32
Show Gist options
  • Save dwightjl/901d562734517efcec821b6097f9b7be to your computer and use it in GitHub Desktop.
Save dwightjl/901d562734517efcec821b6097f9b7be to your computer and use it in GitHub Desktop.
import os, json, csv
with open('new-doc-fields.csv', newline='') as csvfile:
newfields = list(csv.reader(csvfile, delimiter=','))
os.chdir('./data-dir')
entrycount = 0
docsHiddenCount = 0
validCategories = ["verified", "monitored", "custom", "specialized"]
for root, dirs, files in os.walk("."):
for file in files:
change = False
if file.endswith(".json"):
try:
f = open(os.path.join(root, file), "r", encoding="utf-8")
# Dealing with escapes; doing it wrong
x = f.read().encode("utf-8").replace(b'\u', b'\\\\u').decode("utf-8")
j = json.loads(x)
except json.decoder.JSONDecodeError:
print("Failed to parse JSON file: " + os.path.join(root, file))
# Find the "marketing" entry and update it with values from the CSV
if "marketing" in j and "status" in j and j["status"] != "dead":
pair = j["marketing"]["path"]
network = root.split("/")[1]
for row in newfields:
if (row[0] == pair and
row[1] == network and
row[9].split(" ")[1].lower() in validCategories):
new_docs = {
"feedCategory": row[9].split(" ")[1].lower(),
"feedType": row[4],
"assetName": row[2]
}
if "docs" in j:
j["docs"]["feedCategory"] = (new_docs["feedCategory"])
j["docs"]["feedType"] = (new_docs["feedType"])
j["docs"]["assetName"] = (new_docs["assetName"])
entrycount += 1
change = True
else:
j["docs"] = new_docs
entrycount += 1
change = True
elif "marketing" in j and "status" in j:
print("Ignored " + j["marketing"]["path"] + " because status is " + j["status"])
# If "docsHidden" exists, move it and its current value to "docs"
if "docsHidden" in j:
try:
j["docs"]["hidden"] = j["docsHidden"]
j.pop("docsHidden")
docsHiddenCount += 1
change = True
except KeyError:
j["docs"] = {}
j["docs"]["hidden"] = j["docsHidden"]
j.pop("docsHidden")
docsHiddenCount += 1
change = True
# Write the file again alphabetically and restore newline
if change == True:
with open(os.path.join(root, file), "w") as write_file:
out = json.dumps(j, sort_keys=True, indent=2, ensure_ascii=False)
# Restore the escapes again
x = out.encode("utf-8").replace(b'\\\\u', b'\u').decode("utf-8")
write_file.write(x)
write_file.write("\n")
# Old JSON dump before fixing escapes
#json.dump(j, write_file, sort_keys=True, indent=2, ensure_ascii=False)
print("Entries created/updated: " + str(entrycount))
print("docsHidden entries moved: " + str(docsHiddenCount))
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment