Skip to content

Instantly share code, notes, and snippets.

@JonathanGarro
Last active June 28, 2023 15:35
Show Gist options
  • Save JonathanGarro/466d6f6d3627003569b8973f95cd4983 to your computer and use it in GitHub Desktop.
Save JonathanGarro/466d6f6d3627003569b8973f95cd4983 to your computer and use it in GitHub Desktop.
"""
Update the event ID and insert your token (leaving quotes around the token code).
Save this script in a specific folder where you want the CSV that it generates to also be saved.
Note that you will need to update the tags_list values when new profile types are added to RRMS.
"""
import requests
import pandas
import csv
from datetime import date
# config
event_id = 0000
token = ''
# build request
event_selector = '?event='
surge_api = 'https://goadmin.ifrc.org/api/v2/surge_alert/'
go_token = {'Authorization': 'Token {}'.format(token)}
tags_list = ['ADMIN-CO', 'ASSESS-CO', 'CEA- RCCE', 'CEA-CO', 'CEA-OF', 'CIVMILCO', 'COM-TL', 'COMCO', 'COMOF', 'COMPH', 'COMVID', 'CVACO', 'CVAOF', 'DEP-OPMGR', 'DRR-CO', 'EAREC-OF', 'FA Of', 'FIELDCO', 'FIN-CO', 'HEALTH-CO', 'HEALTH-ETL', 'HEOPS', 'HRCO', 'HR-OF', 'HUMLIAS', 'IDRLCO', 'IM-CO', 'IM-PDC', 'IM-VIZ', 'IMANALYST', 'ITT-CO', 'ITT-OF', 'LIVECO', 'LIVEINCM', 'LIVEMRKT', 'LOG-CO', 'LOG-ETL', 'LOG-OF', 'LOGADMIN', 'LOGAIROPS', 'LOGCASH', 'LOGFLEET', 'LOGPIPELINE', 'LOGPROC', 'LOGWARE', 'MDHEALTH-CO', 'MEDLOG', 'MIG-CO', 'MOVCO', 'NSDCO', 'NSDVOL', 'OPMGR', 'PER-CO', 'PER-OF', 'PGI-CO', 'PGI-OF', 'PHEALTH-CO', 'PMER-CO', 'PMER-OF', 'PRD-NS', 'PRD-OF', 'PSS-CO', 'PSS-ERU', 'PSS-OF', 'PSSCMTY', 'RECCO', 'RELCO', 'RELOF', 'SEC-CO', 'SHCLUSTER-CO', 'SHCLUSTER-DEP', 'SHCLUSTER-ENV', 'SHCLUSTER-HUB', 'SHCLUSTER-IM', 'SHCLUSTER-REC', 'SHCLUSTER-TEC', 'SHELTERP-CB', 'SHELTERP-CO', 'SHELTERP-SP', 'SHELTERP-TEC', 'SHELTERP-TL', 'SIMSCo', 'STAFFHEALTH', 'WASH-CO', 'WASH-ENG', 'WASH-ETL', 'WASH-HP', 'WASH-OF', 'WASH-SAN', 'WASH-TEC']
# placeholder to store list of dicts
list_alerts = []
event_url = surge_api + str(event_selector) + str(event_id)
event_surge_alerts = requests.get(event_url).json()
for alert in event_surge_alerts['results']:
temp_dict = {}
temp_dict['country'] = alert['country']['iso3']
temp_dict['message'] = alert['message']
temp_dict['opens'] = alert['opens']
if alert['molnix_tags']:
for tag in alert['molnix_tags']:
if tag['name'] in tags_list:
try:
temp_dict['profile'] = tag['description']
except:
pass
list_alerts.append(temp_dict)
# fill in 'missing' on alerts where Molnix did not include a profile type
for x in list_alerts:
if x.get('profile') is None:
x['profile'] = 'Missing'
today = date.today()
# save output to local csv
keys = list_alerts[0].keys()
a_file = open("{}_event_surge_alerts.csv".format(today), "w")
dict_writer = csv.DictWriter(a_file, keys)
dict_writer.writeheader()
dict_writer.writerows(list_alerts)
a_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment