- import urllib
- import urllib2
- from bs4 import BeautifulSoup
- import re
- import time
- #---[ Functions ]---#
- def isTimeFormat(input):
- try:
- time.strptime(input, '%H:%M')
- return True
- except ValueError:
- return False
- #expressao regular pra remover a sujeira do campo da linha de onibus
- linha_r = re.compile('[\t\n\r\-\|]+')
- url = "http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibus"
- f = urllib2.urlopen(url)
- doc = BeautifulSoup(f.read())
- # print doc
- EMPRESAS = []
- for e in doc.select('select[name="empresa"] > option'):
- if e.has_key("title"):
- EMPRESAS.append((e["title"].strip().encode("utf-8"), int(e["value"])))
- empresaId = 0
- horarioIdIda = 0
- horarioIdVolta = 0
- itinaratioId = 0
- for empresa,id in EMPRESAS:
- print 'EMPRESA :' + empresa
- empresaId = empresaId +1
- #Cria Empresa no banco
- empresadb = {
- 'empresaNome': empresa,
- 'empresaId': empresaId
- }
- #Salvando no DB
- print 'Empresa salva'
- db.empresas.save(empresadb)
- data = {
- "empresa":id,
- "opcao":1,
- "passoGeral":1,
- "passoEmpresa":1,
- }
- req = urllib2.Request(url, urllib.urlencode(data))
- doc = BeautifulSoup(urllib2.urlopen(req).read())
- for linha in doc.select('ul[class="listagem"] > li'):
- linha_data = linha_r.split(linha.contents[0].strip().encode("utf-8"))
- linha_numero = linha_data[0].strip()
- linha_nome = linha_data[1].strip()
- print '---> ONIBUS: ',linha_numero,"-",linha_nome.title()
- #Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
- aba = {'passoGeral' : '3'}
- url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
- data = urllib.urlencode(aba) # Use urllib to encode the parameters
- request = urllib2.Request(url2, data)
- f = urllib2.urlopen(request) # This request is sent in HTTP POST
- doc = BeautifulSoup(f.read())
- x = doc.find_all('ul','listagem')[0]
- for k in x.find_all('li'):
- print str(k)[16:65].title(),
- # Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
- aba = {'passoGeral' : '1'}
- url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
- data = urllib.urlencode(aba) # Use urllib to encode the parameters
- request = urllib2.Request(url2, data)
- f = urllib2.urlopen(request) # This request is sent in HTTP POST
- doc = BeautifulSoup(f.read())
- x = str(doc.find_all(valign="top")[1])
- y=0
- while True:
- y = x.find('<br/>',y+1)
- if y == -1:
- break
- else:
- h = x[y-5:y]
- if isTimeFormat(h) is True:
- print h
- #Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
- aba = {'passoGeral' : '2'}
- url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
- data = urllib.urlencode(aba) # Use urllib to encode the parameters
- request = urllib2.Request(url2, data)
- f = urllib2.urlopen(request) # This request is sent in HTTP POST
- doc = BeautifulSoup(f.read())
- x = str(doc.find_all(valign="top")[1])
- print '------> Horario VOLTA:'
- y=0
- while True:
- y = x.find('<br/>',y+1)
- if y == -1:
- break
- else:
- h = x[y-5:y]
- if isTimeFormat(h) is True:
- print h