<jupyter_start><jupyter_text>From the documentation we know this is the max return --- How can we get all the results?<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>Do the right thing, take some time to look at the codebook and see what these columns are<jupyter_code>niger_res.columns
#PIKE Checking the violent groups in Niger
niger_res.actor1<jupyter_output><empty_output><jupyter_text>Homework --- Make a map of some ACLED Data (absolutely use the code from the Global Terrorism Database exercise)<jupyter_code>!pip install pyproj
from bokeh.tile_providers import get_provider, Vendors
from pyproj import Transformer
title_provider = get_provider('STAMEN_TERRAIN')
#PIKE Visualizing the groups and the geo coordiantes
country_map = niger_res[["latitude", 'longitude', 'actor1']]
country_map.head(7)
transformer = Transformer.from_crs('epsg:4326','epsg:3857')
map_dict = {} # empty dictionary to track group attacks 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['actor1'] in map_dict.keys(): 
        if math.isnan(float(row["latitude"])):
            #This counts no data
            if row['actor1'] in nan_count.keys(): 
                nan_count[row['actor1']] += 1 
            else: 
                nan_count[row['actor1']] = 1
        else: 
            #This has to convert the lat/long to a mercator projection 
            point = transformer.transform(row["latitude"],row["longitude"])
            map_dict[row['actor1']].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(float(row["latitude"])):
          nan_count[row['actor1']] = 1
        else: 
            point = transformer.transform(row["latitude"],row["longitude"])
            map_dict[row['actor1']] =[[point[0],point[1]]]
    
        
#This tells how many attacks we are losing
nan_count
#PIKE Displaying all the groups that produces violent events in Niger
for key in map_dict:
    print(key)
#PIKE Mapping the attacks from the first 5 top groups, defining the geo Coords for Niger
pts = [(25.00, 0), (10, 18.00)]
bbox = []
for pt in transformer.itransform(pts): 
    bbox.append(pt)
#PIKE Visualizing the top 5 groups and the number of violent attacks per each
length_dict = {key: len(value) for key, value in map_dict.items()}

list_dict = [(k, v) for k, v in length_dict.items()]
list_dict.sort(key=lambda x: x[1], reverse=True)
for a in list_dict[0:5]:
    for items in a:
        print(items)
#PIKE Transferring the attackas of each of the identified 5 top groups into the transformed coordinates
for pt in map_dict["Islamic State (West Africa) and/or Boko Haram - Jamatu Ahli is-Sunnah lid-Dawatai wal-Jihad"]:
    NPA_x.append(pt[0])
    NPA_y.append(pt[1])
for pt in map_dict["Islamic State (Greater Sahara)"]:
    NPA_x.append(pt[0])
    NPA_y.append(pt[1])
for pt in map_dict["Unidentified Armed Group (Niger)"]:
    NPA_x.append(pt[0])
    NPA_y.append(pt[1])
for pt in map_dict["Islamic State (West Africa)"]:
    NPA_x.append(pt[0])
    NPA_y.append(pt[1])
for pt in map_dict["Protesters (Niger)"]:
    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", title="VIOLENT EVENTS PER THE TOP FIVE GROUPS IN NIGER")
#add the map form the Bokeh map vendor in this case Stamen_Terrain --- see documentation
p.add_tile(title_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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10811335403726709, 'fraction_numerical': 0.014169254658385094, 'mean_word_length': 3.9500480307396733, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '47865', 'license': 'MIT', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1645, 'n_words': 568, 'path': 'Session 4/.ipynb_checkpoints/Acled - Niger-checkpoint.ipynb', 'repo_name': 'stpalacios/applications', 'size': '5152'}