Example #1
0
 def delete_today(cls):
     now = datetime.now()
     current_time = math.floor(now.timestamp())
     now = now + timedelta(days=-1)
     start_time = math.floor(
         now.replace(hour=23, minute=59, second=59,
                     microsecond=0).timestamp())
     City.select().filter(City.created_at.between(
         start_time, current_time)).delete(synchronize_session=False)
Example #2
0
 def china_detail(cls, date):
     if date is None:
         date = datetime.now().strftime('%Y%m%d')
     provinces = Province.select().filter(Province.date == date).all()
     cities = City.select().filter(City.date == date).all()
     city_map = {}
     for city in cities:
         cs = city_map.get(city.province_name, [])
         if len(cs) == 0:
             city_map[city.province_name] = []
         city_map[city.province_name].append(city.get_json())
     result = []
     for province in provinces:
         res = province.get_json()
         res['cities'] = city_map.get(province.province_name)
         result.append(res)
     return result
Example #3
0
 def china_summary(cls, date):
     if date is None:
         date = datetime.now().strftime('%Y%m%d')
     provinces = Province.select() \
         .filter(Province.country == '中国', Province.date == date) \
         .order_by(Province.confirmed.asc()) \
         .all()
     province_names = [province.province_name for province in provinces]
     cities = City.select() \
         .filter(City.province_name.in_(province_names), City.date == date) \
         .order_by(City.confirmed.asc()) \
         .all()
     china_summary = ChinaSummary(cured=0,
                                  confirmed=0,
                                  dead=0,
                                  suspected=0,
                                  cities={},
                                  province_level={},
                                  provinces={})
     city_map = {}
     for city in cities:
         if city.province_name in city_map.keys():
             city_map[city.province_name].append(city.get_json())
         else:
             city_map[city.province_name] = [city.get_json()]
     for province in provinces:
         china_summary.provinces[province.short_name] = {
             'cured': province.cured,
             'confirmed': province.confirmed,
             'dead': province.dead,
             'suspected': province.suspected
         }
         china_summary.cured += province.cured
         china_summary.confirmed += province.confirmed
         china_summary.dead += province.dead
         china_summary.suspected += province.suspected
         china_summary.province_level[
             province.short_name] = cls.summary_section(province.confirmed)
     china_summary.cities = city_map
     return china_summary
Example #4
0
 def query_by_time(cls, start: int, end: int):
     return City.select().filter(City.created_at.between(start, end)).all()
Example #5
0
 def sync(cls):
     now = datetime.now().strftime('%Y-%m-%d_%H')
     log.info(f'begin sync {now}')
     file_name = f"./data/{now}.json"
     if not os.path.exists(file_name):
         CrawlerService.crawler()
     raw_data = json.load(open(file_name, 'r'))
     data = raw_data['nCoV2019']
     date = datetime.now().strftime('%Y%m%d')
     world = World(date=date,
                   name='中国',
                   confirmed=data['totalConNum'],
                   suspected=data['totalSusNum'],
                   cured=data['totalCureNum'],
                   dead=data['totalDeathNum'],
                   add_confirmed=data['addCon'],
                   add_cured=data['addCure'],
                   add_dead=data['addDeath'],
                   add_suspected=data['addSus'])
     history_list = data['historyList']
     histories = []
     for history in history_list:
         histories.append(
             History(date=history['date'],
                     confirmed=history['conNum'],
                     cured=history['cureNum'],
                     dead=history['deathNum'],
                     suspected=history['susNum']))
     overseas_list = data['overseasList']
     worlds = []
     for overseas in overseas_list:
         worlds.append(
             World(date=date,
                   confirmed=overseas['conNum'],
                   cured=overseas['cureNum'],
                   suspected=overseas['susNum'],
                   dead=overseas['deathNum'],
                   name=overseas['name']))
     domestic_list = data['domesticList']
     provinces = []
     cities = []
     for domestic in domestic_list:
         provinces.append(
             Province(date=date,
                      province_name=domestic['name'],
                      short_name=domestic['name'],
                      confirmed=domestic['conNum'],
                      dead=domestic['deathNum'],
                      suspected=domestic['susNum'],
                      cured=domestic['cureNum']))
         city_list = domestic['cities']
         for city in city_list:
             cities.append(
                 City(date=date,
                      province_name=domestic['name'],
                      city_name=city['name'],
                      confirmed=city['conNum'],
                      suspected=city['susNum'],
                      cured=city['cureNum'],
                      dead=city['deathNum']))
     History.select().delete()
     History.bulk_insert(histories)
     World.select().filter(World.date == date).delete()
     world.insert()
     World.bulk_insert(worlds)
     Province.select().filter(Province.date == date).delete()
     Province.bulk_insert(provinces)
     City.select().filter(City.date == date).delete()
     City.bulk_insert(cities)
     log.info(f'sync {now} done')