def main(): app = create_app() with app.app_context(): schedule = generate_schedule( date(2016, 10, 1), date(2016, 10, 7), [("cebupacific", ("PEK", "MNL")), ("airasia", ("PEK", "KUL"))], 3600 ) schedule2 = generate_schedule( date(2016, 5, 1), date(2016, 5, 8), [("airasia", ("KUL", "MEL")), ("airasia", ("KUL", "CMB")), ("airasia", ("PEK", "CMB")), ("airasia", ("PEK", "MEL"))], 3600, days_offset=0, ) for item in schedule2: job = { "flight_date": item[0], "airline": item[1], "origin": item[2], "destination": item[3], "period": item[4], "next_run_after": date(2016, 1, 1), } job_id = CrawlJob.upsert(**job) print CrawlJob.get(job_id)
def main(): now = datetime.now() all_jobs = CrawlJob.get_jobs(now) # print len(all_jobs) for crawl_job in all_jobs: try: price = fetch_lowest_price( crawl_job.airline, crawl_job.origin, crawl_job.destination, crawl_job.flight_date, crawl_job.period, ) if price is None: print "%s: ERROR" % (crawl_job,) else: # TODO 处理无可用航班与查询出错的情况 print "%s: Y%d" % (crawl_job, price) next_run_after = now + \ timedelta(seconds=crawl_job.period) crawl_job.update( next_run_after=next_run_after ) except: pass
def test_list_jobs(self): job_id = CrawlJob.add( flight_date=date(2016, 10, 10), airline="airasia", origin="PEK", destination="KUL", next_run_after=date(2000, 1, 1), ) job_id2 = CrawlJob.add( flight_date=date(2016, 10, 11), airline="airasia", origin="PEK", destination="KUL", next_run_after=date(2001, 1, 1), ) assert len(CrawlJob.get_jobs(at=datetime(2016, 1, 1))) == 2 assert len(CrawlJob.get_jobs(at=datetime(2000, 2, 2))) == 1 assert len(CrawlJob.get_jobs(at=datetime(1999, 2, 2))) == 0 CrawlJob.delete(job_id) CrawlJob.delete(job_id2)
def crawl_jobs(): return jsonify({ "status": "ok", "response": [job.to_dict() for job in CrawlJob.get_jobs(at=None)] })