Skip to content

Instantly share code, notes, and snippets.

@hronecviktor
Created November 2, 2013 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hronecviktor/7278361 to your computer and use it in GitHub Desktop.
Save hronecviktor/7278361 to your computer and use it in GitHub Desktop.
a_b_successful
import sys
from bs4 import BeautifulSoup
from collections import namedtuple as nt
import re
import requests
def getAtoB(startingStop, endStop):
#vych = "Damborskeho"
#cielova = "Trhovisko"
r = requests.get("http://imhd.zoznam.sk/ba/planovac-cesty-vyhladanie-spojenia.html?spojenieodkial="+startingStop+"&spojeniekam="+endStop)
data = r.text
soup = BeautifulSoup(data)
tables = soup.getText().split("Spojenie")
try:
tables[7]=tables[7].split("◄")[0]
except IndexError as e:
print("Spojenie nenajdene.")
return None
tables=tables[3:8]
print(tables[0])
casy = re.findall("[\\d]{2}:[\\d]{2} [^-(\\s]*",tables[0])
Spoj=nt("Spoj","casy nody presun")
Casy=nt("Casy","zaciatok koniec dlzka")
Noda=nt("Noda","cislo prva nastup posledna vystup")
#cas1=Casy("zaciatok","koniec",None)
#spoj1=Spoj(cas1,"nejakenody")
#print(spoj1.casy,"\t",spoj1.nody)
spoje=[]
for table in tables:
cnt=0;
busy=[]
sepStrings=table.split("►")[:-1]
for string in sepStrings:
busy.append(re.findall("[1-9][\\w]{0,3}",string[-4:])[0])
print("busy: ",busy)
zastavky=[]
stopStrings=table.replace(u'\xa0',u' ').split("►")[1:]
for string in stopStrings:
spoj=re.findall("(\\d{2}:\\d{2}[\\w ]+?)(zón|\\d|NA)",string)
spoj=[y[0].strip() for y in spoj]
zastavky.append([])
for y in spoj:
x=y
if y[5]!=" ":
x=y[:5]
x+=" "
x+=y[5:]
zastavky[cnt].append(x)
cnt+=1
print("cnt: ",cnt,"zast.:",zastavky)
presun = re.findall("Presun.+?min",table)
print("presun:",presun)
celk_cas = re.findall("\(.+?\)",table)[0].replace(u'\xa0', u' ').strip("()")
print("celk.cas: ",celk_cas)
cas=Casy(zastavky[0][0].split(" ")[0],zastavky[-1][-1].split(" ")[0],celk_cas)
buscntr=0;
nody=[]
print("ZASTAVKYYYYY> ",zastavky)
for i in range(len(zastavky)):
noda=Noda(busy[buscntr],zastavky[i][0].split(" ",1)[1],zastavky[i][0].split(" ",1)[0],zastavky[i][-1].split(" ",1)[1],zastavky[i][-1].split(" ",1)[0])
#print("noda: ",noda)
buscntr+=1
nody.append(noda)
kombinacia=Spoj(cas,nody,presun)
spoje.append(kombinacia)
print("==========================================\n"*2)
for spoj in spoje:
print(spoj)
#PRINTING
print("Vyhladane spojenie z \"",spoje[0].nody[0].prva,"\" na \"",spoje[0].nody[0].posledna,"\"")
print("="*80)
for spoj in spoje:
print("Celk. cas\tLinka\tZaciatok\t\t\t\tKoniec")
print(spoj.casy.dlzka,"\t",)
for noda in spoj.nody:
print("\t\t ",(noda.cislo).ljust(3," ")[:10]," ",(noda.nastup+"-"+noda.prva).ljust(21," ")," ",noda.vystup+"-"+noda.posledna)
if presun:
print(presun[0])
print("*****".center(80,"="))
if len(sys.argv)<2:
print("Ziadne argumenty. Help: -h.")
exit(0)
if sys.argv[1] in "-x" and len(sys.argv)==4:
getAtoB(sys.argv[2],sys.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment