Skip to content

Instantly share code, notes, and snippets.

@hronecviktor
Created November 4, 2013 12:59
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/7302083 to your computer and use it in GitHub Desktop.
Save hronecviktor/7302083 to your computer and use it in GitHub Desktop.
1..12
import sys
from bs4 import BeautifulSoup
from collections import namedtuple as nt
import re
import requests
def getStop(stop):
r = requests.get("http://imhd.zoznam.sk/ba/vyhladavanie.html?hladaj="+stop)
data = r.text
bsoup = BeautifulSoup(data)
linky = bsoup.find_all("table")
try:
linky=(linky[1].getText("***"))
except IndexError as e:
print("Zastavka nenajdena")
exit(1)
linka=nt("Linka","cas okolko linka smer")
linky=linky.split("***")[3:]
spoje=[]
for i in range(len(linky))[::4]:
linky[i+1]=linky[i+1].strip("() ")
#if linky[i+1][0]in"o":
#linky[i+1]=linky[i+1][1:]
spoj=linka(linky[i],linky[i+1].strip("() "),linky[i+2],linky[i+3])
#print(spoj)
spoje.append(spoj)
#print("A".center(80,"*"))
#PRINTING
centering_width=15
centering_char=" "
print("Cas".ljust(centering_width,centering_char)+"Linka".ljust(centering_width,centering_char)+"Smer".ljust(centering_width,centering_char))
for spoj in spoje:
print((spoj.cas).ljust(centering_width,centering_char)+"\n"+(spoj.okolko).ljust(centering_width,centering_char)+(spoj.linka).ljust(centering_width,centering_char)+(spoj.smer).ljust(centering_width,centering_char)+"\n"+"*****".center(80,"="))
def getAtoB(startingStop, endStop):
r = requests.get("http://imhd.zoznam.sk/ba/planovac-cesty-vyhladanie-spojenia.html?spojenieodkial="+startingStop+"&spojeniekam="+endStop)
#
print("http://imhd.zoznam.sk/ba/planovac-cesty-vyhladanie-spojenia.html?spojenieodkial="+startingStop+"&spojeniekam="+endStop)
print("start:"+startingStop+" end: "+endStop+" url: "+"http://imhd.zoznam.sk/ba/planovac-cesty-vyhladanie-spojenia.html?spojenieodkial="+startingStop+"&spojeniekam="+endStop)
data = r.text
soup = BeautifulSoup(data)
print(data,"\n*************")
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
print("zastavky:",zastavky)
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[-1].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 / --help.")
exit(0)
if sys.argv[1] in "-sp" and len(sys.argv)==4:
getAtoB(sys.argv[2].replace("_","+"),sys.argv[3].replace("_","+"))
elif sys.argv[1]in {"-z"}:
getStop(sys.argv[2].replace("_","+"))
elif sys.argv[1] in {"-h","--help"}:
print("Parametre: \n1.) -sp <pociatocna_zastavka> <konecna_zastavka>\n\t-najde spoj z bodu A do bodu B. Netreba pisat cely nazov ak zadana cast koresponduje iba s jednou zastavkou.\n\t(napr \"Damb\" pre Damborskeho, \"hl st\" pre Hl. stanicu je v poriadku.)")
else:
print("Invalid arguments: ",*sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment