# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

#import libraries
import requests
from datetime import datetime as dt
from datetime import timedelta
import pandas as pd


# +
# get events from n days ago
iso = 562 #Look in ACLED documentation ISO code for number this is for Niger
limit = 400

api_url = 'https://api.acleddata.com/acled/read?terms=accept&iso={}'.format(iso)
print (api_url, type(api_url)) #creates request according to ACLED format specifications - p. 13
# -

response = requests.get(api_url)
data = response.json()
data.keys()
data['count'] 


# ### From the documentation we know this is the max return --- How can we get all the results?

# +
# Let's mkae a function that updates our search to get the new pages

def ping_acled(api_url): 
    '''
    Takes one parameter search term for API
    '''
    
    response = requests.get(api_url)
    data = response.json()
    return data
    


# +

results = [] # empty data strcture to store results
num_results = 500 # condition to continue adding pages
count = 0  # tracker of results
page = 1 #Per the documentation each page will give us more results

while num_results == 500: #if less 500 or 0 we know we have all the results
    print ("starting ", page, " ", num_results) #just to see our progress
    api_url = 'https://api.acleddata.com/acled/read?terms=accept&iso={}&page={}'.format(iso,page) #the search
    data = ping_acled(api_url) #call the previous function 
    results.append(data['data']) #store in our results
    count += data['count'] #Track number of results
    num_results = data['count'] #update our condition
    page += 1 #update our page variable
    print ("Total Results ", count) #Track our progress
    


# -

#Now I want to put them together into one giant result
super_list = []
for res in results: 
    super_list += res
    print (len(super_list))

#convert it into an pandas data frame or just use your data structure and do more stuff
niger_res = pd.DataFrame(super_list)
niger_res.head()

# ### Do the right thing, take some time to look at the codebook and see what these columns are

niger_res.columns


# ### Homework --- Make a map of some ACLED Data (absolutely use the code from the Global Terrorism Database exercise)

# +
# Import additional libraries

from bokeh.plotting import figure, output_notebook, show #builds interactive graphs for python
from bokeh.models import Range1d
import math #this is used in graphic section to use the irrational number pi
output_notebook() #Allows inline plotting for Juptyer notebook

#Imports necessary aspects of Bokeh for plotting on a map
from bokeh.tile_providers import get_provider, Vendors
from pyproj import Transformer
tile_provider = get_provider('STAMEN_TERRAIN')

# +
# niger_res.info()

# +
# Take the data results from Niger and get the lat/long of the attacks and the type of event
country_map = niger_res[["latitude", 'longitude', 'event_type']]

country_map[['latitude', 'longitude']] = country_map[['latitude', 'longitude']].astype(float)

#see the data this time first 7 rows
country_map.head(7)


# +
events_by_type = {} #make an empty datastructure (dictionary) to fill

#This loop goes through each row and counts the number of entries by group
for index, row in country_map.iterrows(): 
    if row["event_type"]  in events_by_type.keys():
        events_by_type[row["event_type"]] += 1 #if group is in the dictionary add 1 attack
    else:
        events_by_type[row["event_type"]] = 1 #add group name to dictionary if not in dictionary

events_by_type
# -

events = set(country_map.event_type)
events

#create pyproj transformer to convert form lat/long to web mercator
transformer = Transformer.from_crs('epsg:4326','epsg:3857')



# +
map_dict = {} # empty dictionary to track events by lat long
nan_count = {} # some data doesn't have a lat/long so we need to know what we are losing


# Iterate through tables and associate group with lat/long
for idx, row in country_map.iterrows():    
    if row['event_type'] in map_dict.keys(): 
        if math.isnan(row["latitude"]):
            #This counts no data
            if row['event_type'] in nan_count.keys(): 
                nan_count[row['event_type']] += 1 
            else: 
                nan_count[row['event_type']] = 1
        else: 
            #This has to convert the lat/long to a mercator projection 
            point = transformer.transform(row["latitude"],row["longitude"])
            map_dict[row['event_type']].append([point[0],point[1]]) 
    #BOTH the if an else statement do the same thing but since it is a dictionary one needs to add the group name first
    else:  
        if math.isnan(row["latitude"]):
          nan_count[row['event_type']] = 1
        else: 
            point = transformer.transform(row["latitude"],row["longitude"])
            map_dict[row['event_type']] =[[point[0],point[1]]]
        
#This tells how many attacks we are losing
nan_count    

# +
# Create the bounding box for Syria

pts = [(23, -1), (12, 17)]
bbox = []
for pt in transformer.itransform(pts): 
    bbox.append(pt)
    
# bbox
# -

NPA_x = []
NPA_y = []
for pt in map_dict["Violence against civilians"]:
    NPA_x.append(pt[0])
    NPA_y.append(pt[1])

# +
#Plots the bounding box
p = figure(x_range=(bbox[0][0], bbox[1][0]),y_range=(bbox[0][1], bbox[1][1]),x_axis_type="mercator", y_axis_type="mercator")
#add the map form the Bokeh map vendor in this case Stamen_Terrain --- see documentation
p.add_tile(tile_provider)
# Places a circle for each converted lat/long attack 
p.circle(x = NPA_x, y = NPA_y, color= "firebrick")

#shows the plot
show(p)


# +
### <NAME>  This is the ACLED homework - I'm also using much 
### of this code for my project - Cheers!

StarcoderdataJupyterScriptsDedupFiltered