Skip to content

Instantly share code, notes, and snippets.

@hronecviktor
Created November 2, 2013 12:27
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/7278414 to your computer and use it in GitHub Desktop.
Save hronecviktor/7278414 to your computer and use it in GitHub Desktop.
mhdget 1.1
import sys
from bs4 import BeautifulSoup
from collections import namedtuple as nt
import re
import requests
def getAtoB(startingStop, endStop):
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]
Spoj=nt("Spoj","casy nody presun")
Casy=nt("Casy","zaciatok koniec dlzka")
Noda=nt("Noda","cislo prva nastup posledna vystup")
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])
zastavky=[]
stopStrings=table.replace("\xa0"," ").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
presun = re.findall("Presun.+?min",table)
celk_cas = re.findall("\(.+?\)",table)[0].replace("\xa0"," ").strip("()")
cas=Casy(zastavky[0][0].split(" ")[0],zastavky[-1][-1].split(" ")[0],celk_cas)
buscntr=0;
nody=[]
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])
buscntr+=1
nody.append(noda)
kombinacia=Spoj(cas,nody,presun)
spoje.append(kombinacia)
#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