Skip to content

Instantly share code, notes, and snippets.

@wheresalice
Created November 4, 2023 16:17
Show Gist options
  • Save wheresalice/3a578a43a19cc098af985719941c752b to your computer and use it in GitHub Desktop.
Save wheresalice/3a578a43a19cc098af985719941c752b to your computer and use it in GitHub Desktop.
Create a markdown file for every arms company that CAAT know about
import json
import os.path
import frontmatter
from slugify import slugify
path_to_json_files = 'arms-and-security-fair-exhibitors/data/'
json_file_names = [filename for filename in os.listdir(path_to_json_files) if filename.endswith('.json')]
for json_file_name in json_file_names:
with open(os.path.join(path_to_json_files, json_file_name)) as f:
data = json.loads(f.read())
if 'exhibitor' in data:
for c in data['exhibitor']:
if "address" in c and "iso2" in c['address']:
os.makedirs(f"companies/{c['address']['iso2']}", exist_ok=True)
filepath = f"companies/{c['address']['iso2']}/{slugify(c['name'])}.md"
else:
filepath = f"companies/{slugify(c['name'])}.md"
if os.path.isfile(filepath):
company = frontmatter.load(filepath)
else:
company = frontmatter.loads("")
company['title'] = c['name']
if "tags" not in company:
company["tags"] = []
company['tags'].append(slugify(data['name']))
if "category" in c:
for category in c['category']:
company['tags'].append(category)
if "address" in c:
try:
company['location'] = [c['address']['lat'], c['address']['lon']]
except:
print('failed to parse company location')
if 'iso2' in c['address']:
company['country'] = c['address']['iso2']
if 'text' in c['address']:
company['address'] = c['address']['text']
else:
company['address'] = c['address']
with open(filepath, "w") as file:
file.write(frontmatter.dumps(company))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment