# -*- coding: utf-8 -*- #!/usr/bin/env python from json import loads HEADER = ''' Traffy 73 tracker
สาย

73ก BETA

''' STOP_TMPL = '''
  • %(at_stop)s
    %(running)s

    %(name)s

  • ''' GO = '''
    ''' 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 = '' 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': 'ดูขาเข้า' }) 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 = '' 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': 'ดูขาออก' }) 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 = '' # 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': 'ดูขาออก' # }) # f.write( (''.join(CONTENT)).encode('utf-8') ) # f.write(FOOTER)