Created
February 12, 2014 21:01
-
-
Save blackfist/8964427 to your computer and use it in GitHub Desktop.
Add a region code to veris - experimental
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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