Skip to content

Instantly share code, notes, and snippets.

View fmasanori's full-sized avatar

Fernando Masanori fmasanori

View GitHub Profile
import urllib.request
import json
api_key = '7ba14b76dcea0086a03f8eca5a3a801f:1:74295566'
category = 'editorial'
for i in range(5):
url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?" \
"q={0}&api-key={1}&page={2}".format(category, api_key, i)
h = urllib.request.urlopen(url)
result = json.loads(h.read().decode('utf-8'))
@fmasanori
fmasanori / propublica.py
Last active February 2, 2020 05:59
propublica.py
from urllib.request import Request, urlopen
import json
url = "https://api.propublica.org/campaign-finance/v1/2016/president/totals.json"
q = Request(url)
q.add_header('X-API-Key', 'n8QLPm4lNS2Mfak1bam5X7HlevBAOSoF9epQkX0m')
data = urlopen(q).read()
data = json.loads(data.decode('utf-8'))
@fmasanori
fmasanori / pi.py
Last active October 11, 2015 10:52
pi_generator
#Pi Generator ref https://mail.python.org/pipermail/edu-sig/2015-September/011317.html
def pi_digits():
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while True:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a//b, a1//b1
while d == d1:
yield int(d)
a, a1 = 10*(a%b), 10*(a1%b1)
@fmasanori
fmasanori / hackproxy.py
Created July 3, 2015 13:41
Set HTTP_PROXY
import urllib.request as req
proxy = req.ProxyHandler({'http': r'http://user:password@ip:port'})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
@fmasanori
fmasanori / Copa2014all.py
Created July 1, 2015 17:58
Copa 2014 todas as obras
import urllib.request
url = 'http://www.portaltransparencia.gov.br/copa2014/api/rest/empreendimento'
resp = urllib.request.urlopen(url).read().decode('utf-8')
total = 0
j = 0
while True:
abre = '<valorTotalPrevisto>'
fecha = '</valorTotalPrevisto>'
j = resp.find(abre, j)
if j == -1:
@fmasanori
fmasanori / Copa2014.py
Last active August 29, 2015 14:23
Gastos da Copa 2014
#The context of this program is a course of an hour to journalists who know nothing about programming in a lab with Python 3 only.
import urllib.request
url = 'http://www.portaltransparencia.gov.br/copa2014/api/rest/empreendimento'
resp = urllib.request.urlopen(url).read().decode('utf-8')
total = 0
j = 0
and1 = '<andamento>'
and2 = '</andamento>'
abre = '<valorTotalPrevisto>'
fecha = '</valorTotalPrevisto>'
@fmasanori
fmasanori / QEduAvancada2.py
Last active October 22, 2019 17:11
QEdu exemplo de Busca Avançada: escolas, em funcionamento, sem água, energia e esgoto, mostrando alguns detalhes
#The context of this program is a course of an hour to journalists who know nothing about programming in a lab with Python 3 only.
import urllib.request
import json
def analisa_detalhe(cod):
url = 'http://educacao.dadosabertosbr.com/api/escola/'
resp = urllib.request.urlopen(url+str(cod)).read()
resp = json.loads(resp.decode('utf-8'))
if int(resp['salasExistentes']) > 1:
print ('Salas Existentes:', resp['salasExistentes'])
@fmasanori
fmasanori / QEduAvancada.py
Created June 28, 2015 15:57
QEdu Exemplo de Busca Avançada: escolas, em funcionamento, sem energia, água e esgoto
import urllib.request
import json
url = 'http://educacao.dadosabertosbr.com/api/escolas/buscaavancada?situacaoFuncionamento=1&energiaInexistente=on&aguaInexistente=on&esgotoInexistente=on'
resp = urllib.request.urlopen(url).read()
resp = json.loads(resp.decode('utf-8'))
print ('Número de Escolas em funcionamento sem energia, água e esgoto:', resp[0])
for x in resp[1]:
print (x['nome'], x['cod'])
print (x['cidade'], x['estado'], x['regiao'])
print ()
@fmasanori
fmasanori / QEduDetalhe.py
Last active June 13, 2017 12:54
QEdu Busca alguns detalhes pelo código da escola
import urllib.request
import json
url = 'http://educacao.dadosabertosbr.com/api/escola/'
codigo = '35141240'
resp = urllib.request.urlopen(url+codigo).read()
resp = json.loads(resp.decode('utf-8'))
print (resp['nome'])
print ('Salas:', resp['salasExistentes'])
print ('Computadores:', resp['computadores']+resp['computadoresAdm']+resp['computadoresAlunos'])
print ('Formação Docente:', resp['formacaoDocente'])
@fmasanori
fmasanori / QEduEscolas.py
Created June 28, 2015 15:54
QEdu Busca Simples por Escolas
import urllib.request
import json
url = 'http://educacao.dadosabertosbr.com/api/escolas?nome='
escola = 'embraer'
resp = urllib.request.urlopen(url+escola).read()
resp = json.loads(resp.decode('utf-8'))
for x in resp[1]:
print (x['nome'])
print ('Código:', x['cod'])
print (x['cidade'], x['estado'])