Esempio n. 1
0
def index(request):
    date = request.GET.get('date', None)
    start = request.GET.get('start', None)
    arrive = request.GET.get('arrive', None)
    train_code = request.GET.get('train_code', None)
    if date and start and arrive:
        data = yupiao(date, start, arrive, train_code)
        fields = [item[1] for item in FIELDS]
        trains = []
        for train in data:
            trains.append([train[key].strip() for key,value in FIELDS])
        context = {'fields' : fields, 'trains' : trains}
        return render_to_response('mobile/yupiao.html', 
            context,
            context_instance = RequestContext(request)
        )
    else:
        return render_to_response('mobile/index.html',
            {
            },
            context_instance = RequestContext(request)
        )
Esempio n. 2
0
 def read(self, request):
     """
     Returns trains match the qeury
     """
     date = request.GET.get('date')
     start = request.GET.get('start')
     arrive = request.GET.get('arrive')
     train_code = request.GET.get('train_code', None)
     if len(date.split('-')) < 3:
         date = datetime.datetime.today().strftime('%Y') + '-' + date
     if train_code is None:
         cache_key = '%s-%s-%s' %(date, start, arrive)
     else:
         cache_key = '%s-%s-%s-%s' %(date, start, arrive, train_code)
     cache_data = cache.get(cache_key)
     if cache_data:
         return json.loads(cache_data)
     else:
         trains = yupiao(date, start, arrive, train_code)
         #TODO read expired time from 12306
         if len(trains) > 0:
             cache.set(cache_key, json.dumps(trains), 30 * 60)
         return trains
Esempio n. 3
0
Train Query Tool
'''

if __name__ == "__main__":
    from optparse import OptionParser
    import sys
    parser = OptionParser()
    parser.add_option("-d", "--date", dest="date",
                      help="date train leaves,format 2011-04-29")
    parser.add_option("-s", "--start", dest="start_station",
                      help="start station")
    parser.add_option("-a", "--arrive", dest="arrive_station",
                      help="arrive station")
    parser.add_option("-t", "--train", dest="train_code",
                      help="train_code")
    (options, args) = parser.parse_args()

    from train.provider._12306 import yupiao,format_trains
    if options.date and options.start_station and options.arrive_station:
        if options.train_code:
            ret = yupiao(options.date, options.start_station.decode(sys.stdin.encoding), options.arrive_station.decode(sys.stdin.encoding), options.train_code)
        else:
            ret = yupiao(options.date, options.start_station.decode(sys.stdin.encoding), options.arrive_station.decode(sys.stdin.encoding))
        if not sys.stdout.encoding:
            print format_trains(ret).encode('utf-8')
        else:
            print format_trains(ret)
    else:
        parser.error('argument not enough')
        parser.print_usage()