|
|
|
@ -0,0 +1,307 @@
|
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
#!/usr/bin/env python |
|
|
|
|
from json import loads |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HEADER = ''' |
|
|
|
|
<!DOCTYPE html> |
|
|
|
|
<html lang="en"> |
|
|
|
|
<head> |
|
|
|
|
<meta charset="UTF-8" /> |
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
|
|
<title>Traffy 73 tracker</title> |
|
|
|
|
<meta name="description" content="Traffy" /> |
|
|
|
|
<meta name="keywords" content="traffy bus tracking 73" /> |
|
|
|
|
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300|Open+Sans' rel='stylesheet' type='text/css'> |
|
|
|
|
<link rel="stylesheet" href="http://bus.traffy.xyz/build/css/style.css"> |
|
|
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> |
|
|
|
|
</head> |
|
|
|
|
<body> |
|
|
|
|
<div class="container"> |
|
|
|
|
|
|
|
|
|
<header class="clearfix"> |
|
|
|
|
<ul class="pull-right inline-list"> |
|
|
|
|
<li class="width-20"><img class="thumbnail" alt="BMTA" src="http://bus.traffy.xyz/build/css/images/bmta_logo.png" /></li> |
|
|
|
|
<li class="width-20"><img class="thumbnail" alt="AIS" src="http://bus.traffy.xyz/build/css/images/ais_logo.png" /></li> |
|
|
|
|
<li class="width-20"><img class="thumbnail" alt="Traffy" src="http://bus.traffy.xyz/build/css/images/traffy_logo.png" /></li> |
|
|
|
|
<li class="width-20"><img class="thumbnail" alt="NECTEC" src="http://bus.traffy.xyz/build/css/images/nectec_logo.png" /></li> |
|
|
|
|
<li class="width-20"><img class="thumbnail" alt="NSTDA" src="http://bus.traffy.xyz/build/css/images/nstda_logo.png" /></li> |
|
|
|
|
</ul> |
|
|
|
|
<span>สาย</span> |
|
|
|
|
<h1>73ก <small>BETA</small> </h1> |
|
|
|
|
</header> |
|
|
|
|
|
|
|
|
|
<div class="content"> |
|
|
|
|
<ul class="inline-list"> |
|
|
|
|
<li>จำนวนรถ: %(bus_total)s คัน</li> |
|
|
|
|
<li>%(link)s</li> |
|
|
|
|
<li><a href="http://goo.gl/forms/99FEHosxQl" target="_blank" class="outside_link">[ แจ้งเหตุ/ของหายในรถเมล์ ]</a></li> |
|
|
|
|
<li><a href="http://goo.gl/forms/e7GYDPC0CK" target="_blank" class="outside_link"> |
|
|
|
|
[ เสนอแนะ/ติชม ]</a></li> |
|
|
|
|
<li><a href="http://kumo.traffy.in.th/busmap/" target="_blank" class="outside_link"> |
|
|
|
|
[ ดูเป็นแผนที่ ]</a></li> |
|
|
|
|
</ul> |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
<div class="main"> |
|
|
|
|
<ul class="cbp_tmtimeline"> |
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
FOOTER = ''' |
|
|
|
|
</ul> |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div> |
|
|
|
|
</body> |
|
|
|
|
</html> |
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
STOP_TMPL = ''' |
|
|
|
|
<li class="each_stop"> |
|
|
|
|
<div class="block"> |
|
|
|
|
<time class="cbp_tmtime"> |
|
|
|
|
<span></span> |
|
|
|
|
<span></span></time> |
|
|
|
|
<div class="cbp_tmicon">%(at_stop)s</div> |
|
|
|
|
%(running)s |
|
|
|
|
<div class="cbp_tmlabel"> |
|
|
|
|
<h4>%(name)s</h4> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
</li> |
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GO = '''<div class="cbp_tmicon ontheway d80"><i class="fa fa-bus"></i></div> |
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
inbound_bus = [] |
|
|
|
|
outbound_bus = [] |
|
|
|
|
in_stop = [] |
|
|
|
|
out_stop = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('bus_loc.json', 'r') as f: |
|
|
|
|
data = loads(f.read()) |
|
|
|
|
inbound_bus = [i for i in data if i['status_id'] == '11'] |
|
|
|
|
outbound_bus = [i for i in data if i['status_id'] == '12'] |
|
|
|
|
|
|
|
|
|
print 'bus in #%s -- out #%s' % (len(inbound_bus), len(outbound_bus)) |
|
|
|
|
|
|
|
|
|
with open('inbound.json', 'r') as f: |
|
|
|
|
data = loads(f.read())['results'] |
|
|
|
|
in_stop = data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('outbound.json', 'r') as f: |
|
|
|
|
data = loads(f.read())['results'] |
|
|
|
|
out_stop = data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
OUTBOUND ------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
stops = {} |
|
|
|
|
for i in out_stop: |
|
|
|
|
stops[i['stop_id']] = { |
|
|
|
|
'at_stop': '', 'running': '', |
|
|
|
|
} |
|
|
|
|
for i in in_stop: |
|
|
|
|
stops[i['stop_id']] = { |
|
|
|
|
'at_stop': '', 'running': '', |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('index.html', 'w') as f: |
|
|
|
|
|
|
|
|
|
stops = {} |
|
|
|
|
for i in out_stop: |
|
|
|
|
stops[i['stop_id']] = { |
|
|
|
|
'at_stop': '', 'running': '', |
|
|
|
|
} |
|
|
|
|
print 'outbound ----------' |
|
|
|
|
print 'stop: #', len(stops) |
|
|
|
|
|
|
|
|
|
ind = 0 |
|
|
|
|
|
|
|
|
|
for j in outbound_bus: |
|
|
|
|
btw_stop_ref = float(j['next_stop']['linear_ref']) - float(j['prev_stop']['linear_ref']) |
|
|
|
|
rel_ref = (float(j['linear_ref']) - float(j['prev_stop']['linear_ref'])) * 100 / btw_stop_ref |
|
|
|
|
prev_stop_id = j['prev_stop']['stop_id'] |
|
|
|
|
next_stop_id = j['next_stop']['stop_id'] |
|
|
|
|
|
|
|
|
|
print ' btw stop %s - %s -- ref = %s' % (prev_stop_id, next_stop_id, rel_ref) |
|
|
|
|
if rel_ref < 10: |
|
|
|
|
stops[prev_stop_id]['at_stop'] = True |
|
|
|
|
elif rel_ref > 90: |
|
|
|
|
stops[next_stop_id]['at_stop'] = True |
|
|
|
|
else: |
|
|
|
|
stops[prev_stop_id]['running'] = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content_ = [] |
|
|
|
|
for stop in out_stop: |
|
|
|
|
# has_bus_at_stop = [si for si in outbound_bus if si['prev_stop'] == i['id']] |
|
|
|
|
stop_id = stop['stop_id'] |
|
|
|
|
at_stop_text = '' |
|
|
|
|
if stops[stop_id]['at_stop']: |
|
|
|
|
print ' stop %s has %s bus at stop' % (stop_id, stops[stop_id]['at_stop']) |
|
|
|
|
at_stop_text = '<i class="fa fa-bus"></i>' |
|
|
|
|
running_text = '' |
|
|
|
|
if stops[stop_id]['running']: |
|
|
|
|
print ' stop %s has %s bus leaving' % (stop_id, stops[stop_id]['running']) |
|
|
|
|
running_text = GO |
|
|
|
|
stop_data = { |
|
|
|
|
'name': stop['name'], |
|
|
|
|
'at_stop': at_stop_text, |
|
|
|
|
'running': running_text, |
|
|
|
|
} |
|
|
|
|
s = STOP_TMPL % stop_data |
|
|
|
|
|
|
|
|
|
if at_stop_text or running_text: |
|
|
|
|
print s.encode('utf-8') |
|
|
|
|
content_.append(s) |
|
|
|
|
ind += 1 |
|
|
|
|
|
|
|
|
|
f.write(HEADER % { |
|
|
|
|
'bus_total': len(outbound_bus), |
|
|
|
|
'link': '<a class="outside_link" href="/transit/in.html">ดูขาเข้า</a>' |
|
|
|
|
}) |
|
|
|
|
f.write( (''.join(content_)).encode('utf-8') ) |
|
|
|
|
f.write(FOOTER) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
INBOUND ------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open('in.html', 'w') as f: |
|
|
|
|
|
|
|
|
|
stops = {} |
|
|
|
|
for i in in_stop: |
|
|
|
|
stops[i['stop_id']] = { |
|
|
|
|
'at_stop': '', 'running': '', |
|
|
|
|
} |
|
|
|
|
for i in out_stop: |
|
|
|
|
stops[i['stop_id']] = { |
|
|
|
|
'at_stop': '', 'running': '', |
|
|
|
|
} |
|
|
|
|
print 'in ----------' |
|
|
|
|
print 'stop: #', len(stops) |
|
|
|
|
|
|
|
|
|
ind = 0 |
|
|
|
|
|
|
|
|
|
for j in inbound_bus: |
|
|
|
|
btw_stop_ref = float(j['next_stop']['linear_ref']) - float(j['prev_stop']['linear_ref']) |
|
|
|
|
rel_ref = (float(j['linear_ref']) - float(j['prev_stop']['linear_ref'])) * 100 / btw_stop_ref |
|
|
|
|
prev_stop_id = j['prev_stop']['stop_id'] |
|
|
|
|
next_stop_id = j['next_stop']['stop_id'] |
|
|
|
|
|
|
|
|
|
print ' btw stop %s - %s -- ref = %s' % (prev_stop_id, next_stop_id, rel_ref) |
|
|
|
|
if rel_ref < 10: |
|
|
|
|
stops[prev_stop_id]['at_stop'] = True |
|
|
|
|
elif rel_ref > 90: |
|
|
|
|
stops[next_stop_id]['at_stop'] = True |
|
|
|
|
else: |
|
|
|
|
stops[prev_stop_id]['running'] = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content_ = [] |
|
|
|
|
for stop in in_stop: |
|
|
|
|
# has_bus_at_stop = [si for si in inbound_bus if si['prev_stop'] == i['id']] |
|
|
|
|
stop_id = stop['stop_id'] |
|
|
|
|
at_stop_text = '' |
|
|
|
|
if stops[stop_id]['at_stop']: |
|
|
|
|
print ' stop %s has %s bus at stop' % (stop_id, stops[stop_id]['at_stop']) |
|
|
|
|
at_stop_text = '<i class="fa fa-bus"></i>' |
|
|
|
|
running_text = '' |
|
|
|
|
if stops[stop_id]['running']: |
|
|
|
|
print ' stop %s has %s bus leaving' % (stop_id, stops[stop_id]['running']) |
|
|
|
|
running_text = GO |
|
|
|
|
stop_data = { |
|
|
|
|
'name': stop['name'], |
|
|
|
|
'at_stop': at_stop_text, |
|
|
|
|
'running': running_text, |
|
|
|
|
} |
|
|
|
|
s = STOP_TMPL % stop_data |
|
|
|
|
|
|
|
|
|
if at_stop_text or running_text: |
|
|
|
|
print s.encode('utf-8') |
|
|
|
|
content_.append(s) |
|
|
|
|
ind += 1 |
|
|
|
|
|
|
|
|
|
f.write(HEADER % { |
|
|
|
|
'bus_total': len(inbound_bus), |
|
|
|
|
'link': '<a class="outside_link" href="/transit/">ดูขาออก</a>' |
|
|
|
|
}) |
|
|
|
|
f.write( (''.join(content_)).encode('utf-8') ) |
|
|
|
|
f.write(FOOTER) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# with open('in.html', 'w') as f: |
|
|
|
|
|
|
|
|
|
# stops = {} |
|
|
|
|
# for i in in_stop: |
|
|
|
|
# stops[i['stop_id']] = { |
|
|
|
|
# 'at_stop': '', 'running': '', |
|
|
|
|
# } |
|
|
|
|
|
|
|
|
|
# ind = 0 |
|
|
|
|
# CONTENT = [] |
|
|
|
|
|
|
|
|
|
# for j in inbound_bus: |
|
|
|
|
# btw_stop_ref = float(j['next_stop']['linear_ref']) - float(j['prev_stop']['linear_ref']) |
|
|
|
|
# rel_ref = (float(j['linear_ref']) - float(j['prev_stop']['linear_ref'])) * 100 / btw_stop_ref |
|
|
|
|
# prev_stop_id = j['prev_stop']['stop_id'] |
|
|
|
|
# next_stop_id = j['next_stop']['stop_id'] |
|
|
|
|
# if rel_ref < 10: |
|
|
|
|
# stops[prev_stop_id]['at_stop'] = True |
|
|
|
|
# elif rel_ref > 90: |
|
|
|
|
# stops[next_stop_id]['at_stop'] = True |
|
|
|
|
# else: |
|
|
|
|
# stops[prev_stop_id]['running'] = True |
|
|
|
|
|
|
|
|
|
# for stop in in_stop: |
|
|
|
|
# # has_bus_at_stop = [si for si in inbound_bus if si['prev_stop'] == i['id']] |
|
|
|
|
# # print |
|
|
|
|
# at_stop_text = '' |
|
|
|
|
# if stops[stop['stop_id']]['at_stop']: |
|
|
|
|
# at_stop_text = '<i class="fa fa-bus"></i>' |
|
|
|
|
# running_text = '' |
|
|
|
|
# if stops[stop['stop_id']]['running']: |
|
|
|
|
# running_text = GO |
|
|
|
|
# stop = { |
|
|
|
|
# 'name': stop['name'], |
|
|
|
|
# 'at_stop': at_stop_text, |
|
|
|
|
# 'running': running_text, |
|
|
|
|
# } |
|
|
|
|
# s = STOP_TMPL % stop |
|
|
|
|
# CONTENT.append(s) |
|
|
|
|
# ind += 1 |
|
|
|
|
|
|
|
|
|
# f.write(HEADER % { |
|
|
|
|
# 'bus_total': len(inbound_bus), |
|
|
|
|
# 'link': '<a class="outside_link" href="/transit/">ดูขาออก</a>' |
|
|
|
|
# }) |
|
|
|
|
# f.write( (''.join(CONTENT)).encode('utf-8') ) |
|
|
|
|
# f.write(FOOTER) |
|
|
|
|
|