Skip to content

Instantly share code, notes, and snippets.

@cbertelegni
Created September 19, 2017 22:23
Show Gist options
  • Save cbertelegni/4e963c318292453325f1fe870fcab724 to your computer and use it in GitHub Desktop.
Save cbertelegni/4e963c318292453325f1fe870fcab724 to your computer and use it in GitHub Desktop.
Congresoscopio: convierte el json de ranking en csv
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def get_csv_rank(year=2017):
url_data = 'http://votaciones.lanacion.com.ar/api/diputados/ranking/{year}'.format(year=year)
filename_csv = 'tmp/rank{year}.csv'.format(year=year)
r = requests.get(url_data)
if r.status_code == 200:
data = r.json()
rank_types = ["ABSTENCION", "AFIRMATIVO", "AUSENTE", "NEGATIVO"]
fieldnames = data["legisladores"][0].keys() + rank_types
with open(filename_csv, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for l in data["legisladores"]:
""" each leg """
for rank in l["ranking"]:
""" Append votes type """
l[rank["voto"]]=rank["cant"]
# print(l)
writer.writerow(l)
if __name__ == "__main__":
# Default year 2017
get_csv_rank()
get_csv_rank(2016)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment