def format_result_json(s): d={} d['response_code']=200 d['error']=s['error'] td=db.train_metadata(s['num']) d['train_number']=td['number'] d['train_name']=td['name'] sd=db.station_metadata(s['source']) d['from']={} d['from']['name']=sd['fullname'] d['from']['code']=sd['code'] d['from']['lat']=sd['lat'] d['from']['lng']=sd['lng'] d['to']={} sd=db.station_metadata(s['dest']) d['to']['name']=sd['fullname'] d['to']['code']=sd['code'] d['to']['lat']=sd['lat'] d['to']['lng']=sd['lng'] d['class']={} d['class']['class_code']=s['class'] d['class']['class_name']=classname.get(s['class']) d['quota']={} d['quota']['quota_code']=s['quota'] d['quota']['quota_name']=quotaname.get(s['quota']) d['availability']=[] for i in range(len(s['dates'])): t={} t['date']=s['dates'][i] t['status']=s['seats'][i] d['availability'].append(t) d=json.dumps(d,indent=4) return d
def format_result_json(l,code): d={} d['response_code']=200 d['train']=[] d['station']=code doj=time.strftime("%Y%m%d") total=0 print(l) for i in l: t={} m=live.callable_status(i,doj) val='' for j in m: n=breakstn(j['station']) if n['code']==code: val=j break #Exceptional case for trains like 11039 who have slip routes #They will not show the given station in their route when fetching them #from live train servers. if val=='': continue t['actarr']=val['act_arrival'] t['actdep']=val['act_departure'] t['schdep']=val['sch_departure'] t['scharr']=val['sch_arrival'] meta=db.train_metadata(i) t['number']=meta['number'] t['name']=meta['name'] total+=1 d['train'].append(t) d['total']=total d=json.dumps(d,indent=4) return d
def direct_route(source, dest): '''Search for trains between source & dest & returns a list of dictionary containing train metadata''' source_trn = [] with db.opendb(db.MAINDB) as sch: sch._exec("SELECT train FROM schedule WHERE station=(?)", (source, )) t = sch._fetchone() while t != None: source_trn.append(t['train']) t = sch._fetchone() #print(source_trn) if (source_trn == []): return [] dest_trn = [] with db.opendb(db.MAINDB) as sch: sch._exec("SELECT train FROM schedule WHERE station=(?)", (dest, )) t = sch._fetchone() while t != None: dest_trn.append(t['train']) t = sch._fetchone() if (dest_trn == []): return [] #print(dest_trn) return_list = [] for i in range(len(source_trn)): trn = source_trn[i] if (trn in dest_trn): if (comes_after(trn, source, dest)): return_list.append(db.train_metadata(trn)) return return_list
def format_result_json(r,k): d={} d['response_code']=200 d['from']={} sd=db.station_metadata(k['source']) d['from']['name']=sd['fullname'] d['from']['code']=sd['code'] d['to']={} sd=db.station_metadata(k['dest']) d['to']['name']=sd['fullname'] d['to']['code']=sd['code'] d['fare']=[] for i in r: t={} t['code']=i['class'] t['name']=classname.get(i['class']) t['fare']=i['fare'] d['fare'].append(t) d['quota']={} d['quota']['code']=k['quota'] d['quota']['name']=quotaname.get(k['quota']) td=db.train_metadata(k['train']) d['train']={} d['train']['name']=td['name'] d['train']['number']=k['train'] #ensures result in case if DB has no record of such a train d=json.dumps(d,indent=4) return d
def format_result_json(r, k): d = {} d['response_code'] = 200 d['from'] = {} sd = db.station_metadata(k['source']) d['from']['name'] = sd['fullname'] d['from']['code'] = sd['code'] d['to'] = {} sd = db.station_metadata(k['dest']) d['to']['name'] = sd['fullname'] d['to']['code'] = sd['code'] d['fare'] = [] for i in r: t = {} t['code'] = i['class'] t['name'] = classname.get(i['class']) t['fare'] = i['fare'] d['fare'].append(t) d['quota'] = {} d['quota']['code'] = k['quota'] d['quota']['name'] = quotaname.get(k['quota']) td = db.train_metadata(k['train']) d['train'] = {} d['train']['name'] = td['name'] d['train']['number'] = k[ 'train'] #ensures result in case if DB has no record of such a train d = json.dumps(d, indent=4) return d
def format_result_json(l, code): d = {} d["response_code"] = 200 d["train"] = [] d["station"] = code doj = time.strftime("%Y%m%d") total = 0 print(l) for i in l: t = {} m = json.loads(live.get_status(i, doj))["route"] val = "" for j in m: n = breakstn(j["station"]) if n["code"] == code: val = j break # Exceptional case for trains like 11039 who have slip routes # They will not show the given station in their route when fetching them # from live train servers. if val == "": continue t["actarr"] = val["actarr"] t["actdep"] = val["actdep"] t["schdep"] = val["schdep"] t["scharr"] = val["scharr"] meta = db.train_metadata(i) t["number"] = meta["number"] t["name"] = meta["name"] total += 1 d["train"].append(t) d["total"] = total d = json.dumps(d, indent=4) return d
def format_result_json(p): d={} d['response_code']=200 d['train_start_date']={} d['pnr']=p['pnr'] #d['coach_position']=0 # For backwards comptability d['doj']=strip_inline_space(p['doj']) d['train_num']=p['number'][1:] train_md=db.train_metadata(d['train_num']) d['train_name']=train_md['name'] t={} if not p['error']: date=[int(dt) for dt in d['doj'].split('-')] traveldate=datetime(date[2],date[1],date[0]) with db.opendb(db.MAINDB) as sch: sch._exec("SELECT * FROM schedule WHERE train=(?) AND station=(?)",(d['train_num'],p['boarding'])) stn_sch=sch._fetchone() if stn_sch!=None: runday=stn_sch['day']-1 start_date=traveldate-timedelta(days=runday) t['year']=start_date.year t['month']=start_date.month t['day']=start_date.day d['train_start_date']=t d['from_station']={} stn_md=db.station_metadata(p['from']) d['from_station']['code']=stn_md['code'] d['from_station']['name']=stn_md['fullname'] d['to_station']={} stn_md=db.station_metadata(p['to']) d['to_station']['code']=stn_md['code'] d['to_station']['name']=stn_md['fullname'] d['reservation_upto']={} stn_md=db.station_metadata(p['upto']) d['reservation_upto']['code']=stn_md['code'] d['reservation_upto']['name']=stn_md['fullname'] d['boarding_point']={} stn_md=db.station_metadata(p['boarding']) d['boarding_point']['code']=stn_md['code'] d['boarding_point']['name']=stn_md['fullname'] d['class']=p['class'] d['error']=p['error'] d['chart_prepared']=p['chart'] d['total_passengers']=p['total'] d['passengers']=[] curr_status=p['current_status'] book_status=p['booking_status'] coach_position=p['coach_position'] for i in range(p['total']): t={} t['no']=i+1 t['booking_status']=strip_inline_space(book_status[i]) t['current_status']=strip_inline_space(curr_status[i]) t['coach_position']=coach_position[i] d['passengers'].append(t) d=json.dumps(d,indent=4) return d
def format_result_json(p): d = {} d['response_code'] = 200 d['pnr'] = p['pnr'] train_md = db.train_metadata(p['number']) d['train_num'] = train_md['number'] d['train_name'] = train_md['name'] d['doj'] = p['doj'] d['from_station'] = {} stn_md = db.station_metadata(p['from']) d['from_station']['code'] = stn_md['code'] d['from_station']['name'] = stn_md['fullname'] d['to_station'] = {} stn_md = db.station_metadata(p['to']) d['to_station']['code'] = stn_md['code'] d['to_station']['name'] = stn_md['fullname'] d['reservation_upto'] = {} stn_md = db.station_metadata(p['upto']) d['reservation_upto']['code'] = stn_md['code'] d['reservation_upto']['name'] = stn_md['fullname'] d['boarding_point'] = {} stn_md = db.station_metadata(p['boarding']) d['boarding_point']['code'] = stn_md['code'] d['boarding_point']['name'] = stn_md['fullname'] d['class'] = p['class'] d['error'] = p['error'] d['char_prepared'] = p['chart'] d['chart_prepared'] = p['chart'] d['no_of_passengers'] = p['total'] d['passengers'] = [] curr_status = p['current_status'] book_status = p['booking_status'] for i in range(p['total']): t = {} t['no'] = i + 1 t['coach'] = t['berth'] = t['quota'] = '' t['coach'] = book_status[i] t['current_status'] = curr_status[i] d['passengers'].append(t) d = json.dumps(d, indent=4) return d
def format_result_json(r): rundays=db.train_metadata(r['number'])['days'] days=['SUN','MON','TUE','WED','THU','FRI','SAT'] d={} d['response_code']=200 d['train']=dict() d['train'].update({"number":r['number']}) d['train'].update({"name":r['name']}) d['train'].update({'days':[]}) for j in range(7): if days[j] in rundays: runs="Y" else: runs="N" d['train']['days'].append(dict({'day-code':days[j]})) d['train']['days'][j].update({"runs":runs}) d=json.dumps(d,indent=4) return d
def format_result_json(p): d={} d['response_code']=200 d['pnr']=p['pnr'] train_md=db.train_metadata(p['number']) d['train_num']=train_md['number'] d['train_name']=train_md['name'] d['doj']=p['doj'] d['from_station']={} stn_md=db.station_metadata(p['from']) d['from_station']['code']=stn_md['code'] d['from_station']['name']=stn_md['fullname'] d['to_station']={} stn_md=db.station_metadata(p['to']) d['to_station']['code']=stn_md['code'] d['to_station']['name']=stn_md['fullname'] d['reservation_upto']={} stn_md=db.station_metadata(p['upto']) d['reservation_upto']['code']=stn_md['code'] d['reservation_upto']['name']=stn_md['fullname'] d['boarding_point']={} stn_md=db.station_metadata(p['boarding']) d['boarding_point']['code']=stn_md['code'] d['boarding_point']['name']=stn_md['fullname'] d['class']=p['class'] d['error']=p['error'] d['chart_prepared']=p['chart'] d['total_passengers']=p['total'] d['passengers']=[] curr_status=p['current_status'] book_status=p['booking_status'] for i in range(p['total']): t={} t['no']=i+1 t['booking_status']=book_status[i] t['current_status']=curr_status[i] d['passengers'].append(t) d=json.dumps(d,indent=4) return d
def format_result_json(l, code): d = {} d['response_code'] = 200 d['train'] = [] d['station'] = code doj = time.strftime("%Y%m%d") total = 0 print(l) for i in l: t = {} m = live.callable_status(i, doj) val = '' for j in m: n = breakstn(j['station']) if n['code'] == code: val = j break #Exceptional case for trains like 11039 who have slip routes #They will not show the given station in their route when fetching them #from live train servers. if val == '': continue t['actarr'] = val['act_arrival'] t['actdep'] = val['act_departure'] t['schdep'] = val['sch_departure'] t['scharr'] = val['sch_arrival'] meta = db.train_metadata(i) t['number'] = meta['number'] t['name'] = meta['name'] total += 1 d['train'].append(t) d['total'] = total d = json.dumps(d, indent=4) return d
def runningtime(number,doj): url='http://runningstatus.in/status/{0}-on-{1}'.format(number,doj) d={} d['train_number']=number nullify(d) try: # Converting time from GMT to IST if len(doj)!=8: raise year=int(doj[0:4]) month=int(doj[4:6]) day=int(doj[6:8]) datetimeob=datetime.datetime(year,month,day) except: return format_result_json(nullify(d,'Date not in proper format')) weekday=datetimeob.weekday() html=fetchpage(url) soup=BeautifulSoup(html,"lxml") count=0 for i in soup.find_all("div"): if i.attrs.get("class",[None])[0]=="runningstatus-widget-content": if count==1: dot=i.text.find('.') if dot!=-1: end=i.text.find('\n\n\n') #Some pages of this site has no ending tag for <div>. Handles it. if end==-1: d['position']=i.text[dot+1:] else: d['position']=i.text[dot+1:end] count+=1 if "TRAIN IS CANCELLED" in i.text: return format_result_json(nullify(d,'Train is cancelled')) delay_time_header=0 for i in soup.find_all("th"): if i.text.strip()=="Delay Time": delay_time_header=1 trainmd=db.train_metadata(number) days=['MON','TUE','WED','THU','FRI','SAT','SUN'] if trainmd['days']!='': if days[weekday] not in trainmd['days']: return format_result_json(nullify(d,'Train does not run on given date')) lst=[] prog=re.compile("[A-Za-z0-9 .:/()-]+") for i in soup.find_all("td"): i=i.text.strip() if prog.match(i): lst.append(i) lst.append('END_MARKER') liter=iter(lst) nxt=next(liter) while True: t={} if nxt=='END_MARKER': break t['station']=nxt t['platform']=next(liter) t['scharr']=next(liter) t['schdep']=next(liter) t['actarr-actdep']=next(liter) t['status']='' nxt=next(liter) if station_name_format(nxt) or nxt=='END_MARKER': d['route'].append(t) continue if delay_time_header: nxt=next(liter) d['route'].append(t) continue t['status']=nxt d['route'].append(t) nxt=next(liter) if d['route']==[]: return format_result_json(nullify(d,'Invalid Train Number')) return format_result_json(d)