import urllib3 import xml.etree.ElementTree as ET import math from item import * from getStationData import stationInfo def finde(s,f): index = s.find(f) if(index == -1): raise ValueError("Could not find") return(index) def stationName(s): for k in range(3): s = s[finde(s,"td")+1:] while True: s = s[finde(s,">")+1:] if(s[0]!='<'): break dist = float(s[:finde(s,"<")].replace(",",".")) #print(dist) for k in range(2): s = s[finde(s,"td")+1:] while True: s = s[finde(s,">")+1:] if(s[0:7]=="")] while True: s = s[finde(s,">")+1:] if(s[0]!='<'): break name = s[:finde(s,"<")] #print(name) for k in range(3): s = s[finde(s,"td")+1:] return(s,dist,name,link) def lineInfo(number): user_agent = {'user-agent': 'agen'} http = urllib3.PoolManager(10,headers=user_agent) url = http.urlopen('GET',f"https://www.stationsweb.nl/lijnlijst.asp?lijn={number}") s = str(url.data) #Find start of table i=-1 for k in range(4): s = s[finde(s[i+1:],"table")+1:] t = s[:finde(s,"table")] subLines = [] try: while True: t = t[finde(t,"")+8:] dateString = t[:finde(t,"")] date = dateString.split(" ") t = t[finde(t,"")+17:] fromToString = t[:finde(t,"")] [fromString,toString] = fromToString.split(" - ") subLines.append((fromString.replace(" "," "),toString.replace(" "," "),date)) except: pass s = s[finde(s[i+1:],"table")+1:] s = s[finde(s[i+1:],"table")+1:] stations = [] #Get first station 5* td 4* > try: while(True): (s,dist,name,link)=stationName(s) stations.append((dist,name,link)) except: return((stations,subLines)) def dist_coords(latd1,longd1,latd2,longd2): R = 6378.137 lat1 = math.radians(latd1) long1 = math.radians(longd1) lat2 = math.radians(latd2) long2 = math.radians(longd2) #d = 2R × sin⁻¹(√[sin²((θ₂ - θ₁)/2) + cosθ₁ × cosθ₂ × sin²((φ₂ - φ₁)/2)]) d = 2*R*math.asin(math.sqrt(math.sin((lat1-lat2)/2)**2+math.cos(lat1)*math.cos(lat2)*math.sin((long2-long1)/2)**2)) return(d) monthDic = {"jan":0,"feb":1,"mrt":2,"apr":3,"mei":4,"jun":5,"jul":6,"aug":7,"sep":8,"okt":9,"nov":10,"dec":11} def dateKey(item): date = item.date return(int(date[0])+31*(monthDic[date[1]] + 12*int(date[2]))) def getItemList(name, lineNum): (stations,subLines) = lineInfo(lineNum) tree = ET.parse(name) trkseg = tree.getroot()[1][1] station_points = [] dist =0 prev_lat_point =0 prev_lon_point =0 first = True i = 0 for trkpt in trkseg: lat_point = trkpt.items()[0][1] lon_point = trkpt.items()[1][1] if(not first): dist+=dist_coords(float(prev_lat_point),float(prev_lon_point),float(lat_point),float(lon_point)) if(i=stations[i][0]): i+=1 station_points.append((float(lat_point),float(lon_point))) (prev_lat_point,prev_lon_point)=(lat_point,lon_point) first = False station_points.append((float(prev_lat_point),float(prev_lon_point))) itemsPerStation = [] for (station,station_point) in zip(stations,station_points): itemsOfStation = [] (openings,closings) = stationInfo(station[2]) if(openings==[]): print(f"It did not go well with station {station[1]}") for opening in openings: itemsOfStation.append(Station(station[1],station_point[0],station_point[1],opening)) for closing in closings: itemsOfStation.append(RemoveStation(station[1],closing)) itemsPerStation.append(itemsOfStation) #items = [Line(name,"red",["0","jan","1800"])] items = [] for (fromStr,toStr,date) in subLines: from_i = 0 while from_i