-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (29 loc) · 1.27 KB
/
Copy pathmain.py
File metadata and controls
40 lines (29 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from flask import Flask, render_template, request
import requests
app = Flask(__name__)
api_url = "https://dadosabertos.camara.leg.br/api/v2"
@app.route("/")
def main():
page = request.args.get("pagina", 1)
try:
# 1. Fetch the list of legislations
list_url = (
f"{api_url}/proposicoes?itens=30&ordem=ASC&ordenarPor=id&pagina={page}"
)
# Using a session for better performance with multiple requests
with requests.Session() as session:
response = session.get(list_url)
response.raise_for_status()
raw_data = response.json()
legislations = raw_data.get("dados", [])
enriched_data = []
for legislation in legislations:
detail_url = f"{api_url}/proposicoes/{legislation['id']}"
detail_resp = session.get(detail_url)
if detail_resp.status_code == 200:
extra_info = detail_resp.json().get("dados", {})
legislation.update(extra_info)
enriched_data.append(legislation)
return render_template("index.html", data=enriched_data, current_page=int(page))
except requests.exceptions.RequestException as e:
return f"An error occurred: {e}", 500