def build_cache(): import logging logging.basicConfig(level=logging.INFO) cache.invalidate(hard=True) BeijingBus.get_all_stations() click.secho('Done!', fg='green')
def get_s2s_data(msg): stations = BeijingBus.extract_stations(msg) if len(stations) < 2: output = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return output from_station, to_station = stations[:2] lines = BeijingBus.extract_lines(msg) lines = match_stations_with_lines(from_station, to_station, lines) if not lines: output = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return output output = get_realtime_message(lines, from_station) return output
def query(): q = click.prompt('请输入线路名', value_proc=str) lines = BeijingBus.search_lines(q) for index, line in enumerate(lines): click.echo() click.secho('[%s] %s' % (index + 1, line.name), bold=True, underline=True) station_names = [s.name for s in line.stations] click.echo() click.echo('站点列表:%s' % ','.join(station_names)) click.echo() q = click.prompt('请从结果中选择线路编号', type=int) line = lines[q - 1] click.clear() click.echo('你选择了 %s,下面请选择站点' % line.name) click.echo() for index, station in enumerate(line.stations): click.echo('[%s] %s' % (index + 1, station.name)) click.echo() q = click.prompt('请从结果中选择线路编号', type=int) while True: echo_realtime_data(line, q) time.sleep(5)
def cli(): q = click.prompt('请输入线路名', value_proc=str) lines = BeijingBus.search_lines(q) for index, line in enumerate(lines): click.echo() click.secho('[%s] %s' % (index+1, line.name), bold=True, underline=True) station_names = [s.name for s in line.stations] click.echo() click.echo('站点列表:%s' % ','.join(station_names)) click.echo() q = click.prompt('请从结果中选择线路编号', type=int) line = lines[q-1] click.clear() click.echo('你选择了 %s,下面请选择站点' % line.name) click.echo() for index, station in enumerate(line.stations): click.echo('[%s] %s' % (index+1, station.name)) click.echo() q = click.prompt('请从结果中选择线路编号', type=int) while True: echo_realtime_data(line, q) time.sleep(5)
def get_lines(line_num): lines = BeijingBus.search_lines(line_num) result = '' for line_index, line in enumerate(lines): result += '[{}] {}\n'.format(line_index+1, line.name) station_names = [s.name for s in line.stations] '站点列表:{}\n\n'.format(','.join(station_names)) return result + '请从结果中选择线路编号'
def get_line_info(line_query): result = '' lines = BeijingBus.search_lines(line_query[0]) if line_query[1].isdigit() and string.atoi(line_query[1]) <= len(lines): line = lines[string.atoi(line_query[1])-1] for s_index, station in enumerate(line.stations): result += '[{}] {}\n'.format(s_index+1, station.name) result += '你选择了 {},下面请选择站点'.format(line.name) return result
def match_stations_with_lines(from_station, to_station, lines=None): def match(a, b, L): """检查L中包含a和b且a比b靠前""" try: return L.index(a) < L.index(b) except ValueError: return False if not lines: lines = BeijingBus.get_all_lines() return [line for line in lines if match(from_station, to_station, line.stations)]
def query(**kwargs): username = kwargs.get('sender') sender = kwargs.get('receiver') message_type = kwargs.get('type') def r(content): return weixin.reply(username, sender=sender, content=content) if message_type == 'event' and kwargs.get('event') == 'subscribe': msg = app.config.get('ON_FOLLOW_MESSAGE') if not msg: return '' return weixin.reply(username, type='news', sender=sender, articles=[msg]) content = kwargs.get('content') if not content: reply = '我好笨笨哦,还不懂你在说什么。\n%s' % QUERY_EXAMPLE return r(reply) if isinstance(content, unicode): content = content.encode('utf-8') stations = BeijingBus.extract_stations(content) lines = BeijingBus.extract_lines(content) if len(stations) < 2: reply = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return r(reply) from_station, to_station = stations[:2] lines = match_stations_with_lines(from_station, to_station, lines) if not lines: reply = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return r(reply) reply = get_realtime_message(lines, from_station) return r(reply)
def list_supported_lines(): names = set([ line.short_name for line in BeijingBus.get_all_lines() ]) return '''<!DOCTYPE html> <html lang="zh-CN"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>支持的线路的列表</title> </head> <body>%s</body> </html> ''' % ''.join(['<p>%s</p>' % name for name in sorted(names)])
def query(**kwargs): username = kwargs.get('sender') sender = kwargs.get('receiver') message_type = kwargs.get('type') def r(content): return weixin.reply( username, sender=sender, content=content ) if message_type == 'event' and kwargs.get('event') == 'subscribe': reply = ('欢迎关注北京实时公交!\n' '你可以通过向我发送消息查询公交实时到站时间。\n\n%s' ) % QUERY_EXAMPLE return r(reply) content = kwargs.get('content') if not content: reply = '我好笨笨哦,还不懂你在说什么。\n%s' % QUERY_EXAMPLE return r(reply) if isinstance(content, unicode): content = content.encode('utf-8') stations = BeijingBus.extract_stations(content) lines = BeijingBus.extract_lines(content) if len(stations) < 2: reply = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return r(reply) from_station, to_station = stations[:2] lines = match_stations_with_lines(from_station, to_station, lines) if not lines: reply = '没有结果,可能还不支持这条线路呢~ \n%s' % QUERY_EXAMPLE return r(reply) reply = get_realtime_message(lines, from_station) return r(reply)
def query(**kwargs): username = kwargs.get("sender") sender = kwargs.get("receiver") message_type = kwargs.get("type") def r(content): return weixin.reply(username, sender=sender, content=content) if message_type == "event" and kwargs.get("event") == "subscribe": msg = app.config.get("ON_FOLLOW_MESSAGE") if not msg: return "" return weixin.reply(username, type="news", sender=sender, articles=[msg]) content = kwargs.get("content") if not content: reply = "我好笨笨哦,还不懂你在说什么。\n%s" % QUERY_EXAMPLE return r(reply) if isinstance(content, unicode): content = content.encode("utf-8") stations = BeijingBus.extract_stations(content) lines = BeijingBus.extract_lines(content) if len(stations) < 2: reply = "没有结果,可能还不支持这条线路呢~ \n%s" % QUERY_EXAMPLE return r(reply) from_station, to_station = stations[:2] lines = match_stations_with_lines(from_station, to_station, lines) if not lines: reply = "没有结果,可能还不支持这条线路呢~ \n%s" % QUERY_EXAMPLE return r(reply) reply = get_realtime_message(lines, from_station) return r(reply)
def match_stations_with_lines(from_station, to_station, lines=None): def match(a, b, l): '''检查l中包含a和b且a比b靠前''' try: return l.index(a) < l.index(b) except ValueError: return False if not lines: lines = BeijingBus.get_all_lines() return [ line for line in lines if match(from_station, to_station, line.stations) ]
def get_station_info(line_query): result = '' lines = BeijingBus.search_lines(line_query[0]) if line_query[1].isdigit() and string.atoi(line_query[1]) <= len(lines): line = lines[string.atoi(line_query[1]) - 1] if line_query[2].isdigit() and string.atoi(line_query[2]) <= len(lines): station = line.stations[string.atoi(line_query[2]) - 1] realtime_data = line.get_realtime_data(string.atoi(line_query[2])) now = datetime.now().strftime('%H:%M:%S') result += '实时数据: {} 线路: {} 更新于: {}\n'.format(station.name, line.name, now) realtime_data = filter(lambda d: d['station_arriving_time'], realtime_data) realtime_data = sorted(realtime_data, key=lambda d: d['station_arriving_time'], reverse=False) for i, data in enumerate(realtime_data): result += '公交车辆 {}:\n'.format(i + 1) result += '距离 {} 还有 {} 米\n'.format(station.name, data['station_distance']) result += '预计 {} 到达\n'.format(data['station_arriving_time'].strftime('%H:%M')) result += '感谢使用!重新查询请重新输入线路。' return result
def build_cache(): import logging logging.basicConfig(level=logging.DEBUG) BeijingBus.build_cache() click.secho('Done!', fg='green')
def list_supported_lines(): names = set([line.short_name for line in BeijingBus.get_all_lines()]) names = sorted([n.decode("utf-8") for n in names]) return render_template("list.html", line_names=names)
def list_supported_lines(): names = set([ line.short_name for line in BeijingBus.get_all_lines() ]) names = sorted([n.decode('utf-8') for n in names]) return render_template('list.html', line_names=names)