Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active Dec 25, 2018
Embed
What would you like to do?
Sort entries in Sherlock data.json file
#!/usr/bin/env python3
# sort entries listed in https://github.com/sdushantha/sherlock/blob/master/data.json
import re
fname = "data.json"
site_re = re.compile('(".*?\},)',re.M|re.S)
name_re = re.compile('"(.*?)": \{')
def main():
all_entries = {}
with open(fname,encoding="latin1") as fp:
data = fp.read()
# add a comma to the last entry, so that the RE will include it
data = re.sub(" \}\n"," },",data)
match = site_re.findall(data)
for entry in match:
match2 = name_re.findall(entry)
site_name = match2[0]
all_entries[site_name] = entry
# case-insensitive sort
sorted_entries = sorted(list(all_entries.keys()), key=str.lower)
print("{")
total = len(sorted_entries)
current = 0
for entry in sorted_entries:
current += 1
if current == total:
# do not include a comma after last entry
print( re.sub(" \},", " }", all_entries[entry]))
else:
print(all_entries[entry])
print("}")
if "__main__" == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment