Skip to content

Instantly share code, notes, and snippets.

@blackfist
Created February 12, 2014 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackfist/8964427 to your computer and use it in GitHub Desktop.
Save blackfist/8964427 to your computer and use it in GitHub Desktop.
Add a region code to veris - experimental
# Download https://raw2.github.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.json
# and save as all.json in the same folder
import json
import os
from datetime import datetime
import uuid
# i = getIncident('blahblahblah.json')
def getIncident(inString):
return json.loads(open(inString).read())
# updateIncident(i, 'blahblahblah.json', True)
def updateIncident(i,inFilename,validated=False):
i['plus']['modified'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
if validated:
i['plus']['analysis_status'] = "Validated"
outfile = open(inFilename,'w')
outfile.write(json.dumps(i,sort_keys=True, indent=2,separators=(',', ': ')))
outfile.close()
json_path = '../data/json'
country_codes = json.loads(open('all.json').read())
country_code_remap = {'Unknown':'000000'}
for eachCountry in country_codes:
try:
country_code_remap[eachCountry['alpha-2']] = eachCountry['region-code']
except:
country_code_remap[eachCountry['alpha-2']] = "000"
try:
country_code_remap[eachCountry['alpha-2']] += eachCountry['sub-region-code']
except:
country_code_remap[eachCountry['alpha-2']] += "000"
for eachFile in os.listdir(json_path):
i = getIncident(os.path.join(json_path,eachFile))
if 'external' in i['actor']:
i['plus']['external_region'] = []
for eachCountry in i['actor']['external']['country']:
i['plus']['external_region'].append(country_code_remap[eachCountry])
print "Updating",os.path.join(json_path,eachFile)+".",i['actor']['external']['country'],"==",i['plus']['external_region']
updateIncident(i,os.path.join(json_path,eachFile))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment