Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #qpy:2
- #qpy:console
- #import androidhelper as android
- import time, urllib2, math
- def glt():
- return time.localtime()
- def dtr(d):
- return d*3.1415926535/180
- def hmstr(t): # secs to "9 mins" or "50 secs"
- if t>=3600 : return str(t/3600)+" hrs"
- if t>=600 : return str(t/60)+ " mins"
- if t>=60 : return ' '+str(t/60)+" mins"
- if t>=10 : return str(t) + " secs"
- if t>=0 : return ' '+str(t) +" secs"
- def dist(la, lo, lat, lon):
- r=6371000
- dla = dtr(lat-la)
- dlo = dtr(lon-lo)
- a = math.sin(dla/2)**2 + math.cos(dtr(la))*math.cos(dtr(lat)) * math.sin(dlo/2)**2
- c = 2 * math.atan2(a**0.5, (1-a)**0.5)
- return r * c
- def secs(hhmmss):
- return 3600*int(hhmmss[0:2])+60*int(hhmmss[3:5])+int(hhmmss[6:8])
- def hms(secs):
- h, m, s = secs/3600, (secs%3600)/60, secs%60
- return str(h/10)+str(h%10)+':'+str(m/10)+str(m%10)+':'+str(s/10)+str(s%10)
- def isv(serv):
- lt = glt()
- return isAv(serv, str(lt[0])+str(lt[1]/10)+str(lt[1]%10)+str(lt[2]/10)+str(lt[2]%10), lt[6]);
- def isAv(serv, date, dow): # date is 'YYYYMMAA' # dow is from Mon 0
- s=sum([s[2] for s in cd if s[0]==serv and s[1]==date])
- if s>0: return s==1
- date = int(date)
- return sum([int(s[dow+1]) for s in cl if s[0]==serv and date>=int(s[8]) and date<=int(s[9])])>0
- print 'Loading files...'
- droot = '/storage/emulated/0/Download/Tan/'
- droot = 'Documents/Tan/'
- st = [s.split(',') for s in open(droot+'stops.txt').readlines()[1:]]
- ts = [s.split(',') for s in open(droot+'stop_times.txt').readlines()[1:]]
- tr = [s.split(',') for s in open(droot+'trips.txt').readlines()[1:]]
- rt = [s.split(',') for s in open(droot+'routes.txt').readlines()[1:]]
- cl = [s.split(',') for s in open(droot+'calendar.txt').readlines()[1:]]
- cd = [s.split(',') for s in open(droot+'calendar_dates.txt').readlines()[1:]]
- pt = [s for s in st if s[7]=='0']
- st = [s for s in st if s[7]=='1']
- print 'Setup complete!'
- # stops, times, trips, routes, points
- gps = False
- home= True
- loop= True
- while loop:
- found = False
- if gps:
- time.sleep(2)
- droid=android.Android()
- droid.startLocating()
- event=droid.eventWaitFor('location', 10000).result
- try:
- lng = -1.55716847 if home else -1.51530761
- lat = 47.25072151 if home else 47.28189980
- if gps:
- lng = event['data']['gps']['longitude']
- lat = event['data']['gps']['latitude']
- print "Lng: %s Lat: %s" %(lng,lat)
- found = True
- except:
- print "Network Coordinates"
- if found:
- print "Calculating..."
- sttime = time.time();
- prox = []
- for s in st[1:]:
- la = float(s[3])
- lo = float(s[4])
- nm = s[1]
- ds = dist(la, lo, lat, lng)
- prox.append((ds, nm[1:-1], s[0]))
- prox.sort()
- lt = glt()
- dow = lt[6] if lt[3]>3 else (lt[6]+6)%7
- now = 3600*lt[3]+60*lt[4]+lt[5]+(84600 if lt[3]<4 else 0)
- ids = [s[2] for s in prox]
- stt = [s for s in pt if s[8] in ids]
- for i in range(8):
- stop=prox[i]
- print stop[1]+' ('+str(int(stop[0]))+'m)'
- if i<4:
- sts = [s[0] for s in stt if s[8]==stop[2]]
- pxr = [(secs(s[2])-now, s) for s in ts if s[3] in sts and secs(s[2])>now and secs(s[2])<=now+1800]
- pxr = [(p[0], p[1], [s for s in tr if s[2]==p[1][0]][0]) for p in pxr]
- prx = [(p[0], p[1], p[2], [r for r in rt if r[0]==p[2][0]][0]) for p in pxr if isv(p[2][1]) ]
- prx.sort()
- for p in prx:
- print ' '+hmstr(p[0])+' - '+p[3][2]+' vers '+p[2][3][1:-1]
- print 'Time : '+str(int(1000*(time.time()-sttime)))+' ms'
- if not gps:
- time.sleep(3)
- loop=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement