def news(**kwargs): if NEWSAPI_KEY == "xxxxxxxxxx": return jsonify({"message": "Please provide the NewsApi API KEY"}), 500 param = { "apiKey": NEWSAPI_KEY, "q": "corona", "pageSize": 3, "sources": "bbc-news" } if "countries" in kwargs: param["country"] = kwargs['countries'] param.pop("sources") req = requests.get(NEWSAPI_HOST, params=param) if not req.status_code == 200: return jsonify( {"message": "Error occured while " "trying to get the news"}), req.status_code if is_bot(): return jsonify(message=bot.news(req.json()['articles'])), 200 return jsonify(req.json()['articles']), 200
def _response(data, responseCode): if is_bot(): return jsonify(message=bot.id(data), images=[url_for('indonesia.graph', _external=True)]), responseCode else: return jsonify(data), responseCode
def hot_line(state): results = [] for city in HOTLINE: key = city["kota"].lower() if re.search(r"\b%s" % state.lower(), key): results.append(city) if is_bot(): return jsonify(message=bot.hotline(results)), 200 return jsonify(results)
def province(province): if province in DAERAH: result = crawler.odi_api(province) if len(result) == 0: return jsonify(message="Not Found"), 404 if is_bot(): return jsonify(message=bot.province(result)), 200 else: return jsonify(result), 200 if province == "prov" or province == "list": provinsi = [item for item in DAERAH] if is_bot(): return jsonify(message=bot.province_list(provinsi)), 200 else: return jsonify([item for item in DAERAH]), 200 return jsonify(message="Not Found"), 404
def index(): data = { "confirmed": 0, "deaths": 0, "recovered": 0, } for item in _get_today(): data['confirmed'] += int(item['confirmed']) data['deaths'] += int(item['deaths']) data['recovered'] += int(item['recovered']) if is_bot(): return jsonify(message=bot.summary(data)), 200 return jsonify(data), 200
def _response(data, responseCode): if is_bot(): return jsonify(message=bot.id(data)), responseCode else: return jsonify(data), responseCode
def _odi_api(state): req = requests.get(ODI_API) if not req.status_code == 200: jsonify({"message": f"Error when trying to crawl {ODI_API}"}), 404 prov = {prov["provinsi"]: prov for prov in req.json()["data"]} hasil = prov[DAERAH[state]] todayIsNone = True result = _default_resp() result['metadata'] = { "source": "https://indonesia-covid-19.mathdro.id/", "province": DAERAH[state].upper() } get_state = session.query(Status) \ .filter(Status.country_id == f"id.{state}") \ .order_by(Status.created.desc()) \ .all() if len(get_state) > 0: for row in get_state: if not row.created.date() == datetime.utcnow().date(): result["total_sembuh"]["diff"] = \ hasil["kasusSemb"] - row.recovered result["total_positif"]["diff"] = \ hasil["kasusPosi"] - row.confirmed result["total_meninggal"]["diff"] = \ hasil["kasusMeni"] - row.deaths result["metadata"]["diff_date"] = \ row.created.isoformat() result["metadata"]["source_date"] = \ datetime.utcnow().isoformat() break else: todayIsNone = False result["metadata"]["source_date"] = \ row.created.isoformat() if todayIsNone: new_status = Status(confirmed=hasil["kasusPosi"], deaths=hasil["kasusMeni"], recovered=hasil["kasusSemb"], active_care=0, country_id=f"id.{state}", created=datetime.utcnow(), updated=datetime.utcnow()) session.add(new_status) result["metadata"]["source_date"] = \ datetime.utcnow().isoformat() result["total_sembuh"]["value"] = hasil["kasusSemb"] result["total_positif"]["value"] = hasil["kasusPosi"] result["total_meninggal"]["value"] = hasil["kasusMeni"] if len(result) == 0: jsonify({"message": "Not Found"}), 404 if is_bot(): return jsonify(message=bot.province(result)), 200 else: return jsonify(result), 200
from src.models import Status from src.helper import (is_empty, is_bot, is_not_bot, TODAY_STR, TODAY, YESTERDAY_STR, DAERAH) from src import bot indonesia = Blueprint('indonesia', __name__) JABAR = 'https://coredata.jabarprov.go.id/analytics/covid19/aggregation.json' ODI_API = 'https://indonesia-covid-19.mathdro.id/api/provinsi' KAWAL_COVID = "https://kawalcovid19.harippe.id/api/summary" CONTACT = "https://pikobar.jabarprov.go.id/contact/" sLIMITER = 5 @indonesia.route('/') @limiter.limit(f"1/{sLIMITER}second", key_func=lambda: is_bot(), exempt_when=lambda: is_not_bot()) # noqa @cache.cached(timeout=50) def id(): req = requests.get(KAWAL_COVID) data = req.json() updated = parser.parse(data['metadata']['lastUpdatedAt']) \ .replace(tzinfo=None) alldata = session.query(Status) \ .filter(Status.country_id == "id") \ .order_by(Status.created.desc()).all() dbDate = "" if len(alldata) > 0: getData = [_id_beauty(data, row) for row in alldata] dbDate = parser.parse(getData[0]["metadata"]["last_updated"]) \ .replace(tzinfo=None)
def provinces(): if is_bot(): return jsonify(message="Not Found"), 404 with open('src/provinces.json', 'rb') as outfile: return jsonify(json.load(outfile)), 200 return jsonify({"message": "Error Occured"}), 500
def stat(status): if is_bot(): return jsonify(message="Not Found"), 404 if status in ['confirmed', 'deaths', 'recovered']: return jsonify(_get_today(only_keys=status)), 200 return jsonify(message="Not Found"), 404
def hotline(): if is_bot(): return jsonify(message=bot.hotline(HOTLINE)), 200 return jsonify(HOTLINE), 200
def introduction(): if is_bot(): return jsonify(message=bot.introduction()), 200 return jsonify(message="Not Found"), 404
def country(country_id): if is_bot(): return jsonify(message=bot.countries( _get_today(country=country_id.upper(), mode="diff"))), 200 return jsonify(_get_today(country=country_id.upper())), 200
def before_request(request): if is_bot(): return jsonify(message="Not Found"), 404
def jabar(): result = { "tanggal": { "value": "" }, "total_sembuh": { "value": 0, "diff": 0 }, "total_positif": { "value": 0, "diff": 0 }, "total_meninggal": { "value": 0, "diff": 0 }, "proses_pemantauan": { "value": 0 }, "proses_pengawasan": { "value": 0 }, "selesai_pemantauan": { "value": 0 }, "selesai_pengawasan": { "value": 0 }, "total_odp": { "value": 0 }, "total_pdp": { "value": 0 }, "source": { "value": "" } } response = requests.get(JABAR) if not response.status_code == 200: jsonify({"message": f"Error when trying to crawl {JABAR}"}), 404 json_resp = response.json() today_stat = _search_list(json_resp, "tanggal", TODAY_STR['hyphen-dmy']) yeday_stat = _search_list(json_resp, "tanggal", YESTERDAY_STR['hyphen-dmy']) if today_stat["selesai_pengawasan"] is None: twodaysago = _search_list( json_resp, "tanggal", datetime.strftime(TODAY - timedelta(days=2), "%d-%m-%Y")) result = _jabarset_value(yeday_stat, twodaysago) result['metadata'] = { "source": "https://pikobar.jabarprov.go.id", "province": "Jawa Barat", "source_date": YESTERDAY_STR['hyphen-dmy'], } else: result = _jabarset_value(today_stat, yeday_stat) result['metadata'] = { "source": "https://pikobar.jabarprov.go.id", "province": "Jawa Barat", "source_date": TODAY_STR['hyphen-dmy'], } if len(result) == 0: jsonify({"message": "Not Found"}), 404 if is_bot(): return jsonify(message=bot.jabar(result)), 200 else: return jsonify(result), 200