|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import requests |
| 4 | +from lxml.html import fromstring |
| 5 | +from database import MongoDb as Database |
| 6 | + |
| 7 | + |
| 8 | +class IbgeTracker(): |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self.url_ufs = 'http://www.ibge.gov.br/home/geociencias' + \ |
| 12 | + '/areaterritorial/principal.shtm' |
| 13 | + self.url_cidades = 'http://www.ibge.gov.br/home/geociencias' + \ |
| 14 | + '/areaterritorial/area.php?nome=%' |
| 15 | + |
| 16 | + def _request(self, url): |
| 17 | + response = requests.post(url) |
| 18 | + response.raise_for_status() |
| 19 | + return response.text |
| 20 | + |
| 21 | + def _get_info_ufs(self, siglas): |
| 22 | + texto = self._request(self.url_ufs) |
| 23 | + html = fromstring(texto) |
| 24 | + seletorcss_linhas = "div#miolo_interno > table > tr" |
| 25 | + linhas = html.cssselect(seletorcss_linhas) |
| 26 | + linhas.pop() # a primeira é o cabeçalho |
| 27 | + infos = [] |
| 28 | + for linha in linhas: |
| 29 | + seletorcss_celulas = "td" |
| 30 | + celulas = linha.cssselect(seletorcss_celulas) |
| 31 | + codigo_ibge = celulas[0].text_content() |
| 32 | + if codigo_ibge in siglas: |
| 33 | + sigla = siglas[codigo_ibge] |
| 34 | + infos.append({ |
| 35 | + 'sigla': sigla, |
| 36 | + 'codigo_ibge': codigo_ibge, |
| 37 | + 'nome': celulas[1].text_content(), |
| 38 | + 'area_km2': celulas[2].text_content() |
| 39 | + }) |
| 40 | + |
| 41 | + # neste ponto, após a carga |
| 42 | + # das cidades, a lista |
| 43 | + # 'infos' deve estar populada |
| 44 | + |
| 45 | + return infos |
| 46 | + |
| 47 | + def _get_info_cidades(self): |
| 48 | + texto = self._request(self.url_cidades) |
| 49 | + html = fromstring(texto) |
| 50 | + seletorcss_linhas = "div#miolo_interno > table > tr" |
| 51 | + linhas = html.cssselect(seletorcss_linhas) |
| 52 | + linhas.pop() # a primeira é o cabeçalho |
| 53 | + infos = [] |
| 54 | + for linha in linhas: |
| 55 | + seletorcss_celulas = "td" |
| 56 | + celulas = linha.cssselect(seletorcss_celulas) |
| 57 | + infos.append({ |
| 58 | + 'codigo_ibge_uf': celulas[0].text_content(), |
| 59 | + 'sigla_uf': celulas[1].text_content(), |
| 60 | + 'codigo_ibge': celulas[2].text_content(), |
| 61 | + 'nome': celulas[3].text_content(), |
| 62 | + 'area_km2': celulas[4].text_content() |
| 63 | + }) |
| 64 | + return infos |
| 65 | + |
| 66 | + def _track_ufs(self, db, siglas): |
| 67 | + infos = self._get_info_ufs(siglas) |
| 68 | + for info in infos: |
| 69 | + db.insert_or_update_uf(info) |
| 70 | + |
| 71 | + def _track_cidades(self, db): |
| 72 | + infos = self._get_info_cidades() |
| 73 | + siglas = {} |
| 74 | + for info in infos: |
| 75 | + codigo_ibge_uf = info['codigo_ibge_uf'] |
| 76 | + sigla_uf = info['sigla_uf'] |
| 77 | + nome = info['nome'] |
| 78 | + if codigo_ibge_uf not in siglas: |
| 79 | + siglas[codigo_ibge_uf] = sigla_uf |
| 80 | + |
| 81 | + # a chave única de uma cidade não |
| 82 | + # pode ser só o nome, pois |
| 83 | + # existem cidades com mesmo nome |
| 84 | + # em estados diferentes |
| 85 | + info['sigla_uf_nome_cidade'] = '%s_%s' % (sigla_uf, nome) |
| 86 | + |
| 87 | + db.insert_or_update_cidade(info) |
| 88 | + |
| 89 | + return siglas |
| 90 | + |
| 91 | + def track(self, db): |
| 92 | + """ |
| 93 | + Atualiza as bases internas do mongo |
| 94 | + com os dados mais recentes do IBGE |
| 95 | + referente a ufs e cidades |
| 96 | + """ |
| 97 | + siglas = self._track_cidades(db) # siglas é um dict cod_ibge -> sigla: |
| 98 | + # { '35': 'SP', '35': 'RJ', ... } |
| 99 | + self._track_ufs(db, siglas) |
| 100 | + |
| 101 | + |
| 102 | +def _standalone(): |
| 103 | + db = Database() |
| 104 | + ibge = IbgeTracker() |
| 105 | + ibge.track(db) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + _standalone() |
0 commit comments