Beispiel #1
0
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)
        if not updated == dbDate:
            new_status = Status(confirmed=data['confirmed']['value'],
                                deaths=data['deaths']['value'],
                                recovered=data['recovered']['value'],
                                active_care=data['activeCare']['value'],
                                country_id="id",
                                created=updated,
                                updated=updated)
            session.add(new_status)
        for index, row in enumerate(getData):
            if not row['confirmed']['diff'] == 0 and \
               not row['deaths']['diff'] == 0 and \
               not row['recovered']['diff'] == 0 and \
               not row['active_care']['diff'] == 0:
                row['metadata']['last_updated'] = \
                    getData[index-1]['metadata']['last_updated']
                return _response(row, 200)
        return _response(getData[0], 200)
    else:
        new_status = Status(confirmed=data['confirmed']['value'],
                            deaths=data['deaths']['value'],
                            recovered=data['recovered']['value'],
                            active_care=data['activeCare']['value'],
                            country_id="id",
                            created=updated,
                            updated=updated)
        session.add(new_status)
        return _response(_id_beauty(data, 0), 200)
Beispiel #2
0
def id():
    req = requests.get("https://kawalcovid19.harippe.id/api/summary")
    data = req.json()
    updated = parser.parse(data['metadata']['lastUpdatedAt']) \
        .replace(tzinfo=None)
    alldata = session.query(Status).order_by(Status.id.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)
        if not updated == dbDate:
            new_status = Status(confirmed=data['confirmed']['value'],
                                deaths=data['deaths']['value'],
                                recovered=data['recovered']['value'],
                                active_care=data['activeCare']['value'],
                                country_id="id",
                                created=updated,
                                updated=updated)
            session.add(new_status)
        for row in getData:
            if not row['confirmed']['diff'] == 0 and \
               not row['deaths']['diff'] == 0 and \
               not row['recovered']['diff'] == 0 and \
               not row['active_care']['diff'] == 0:
                return jsonify(row), 200
        return jsonify(getData[0]), 200
    else:
        new_status = Status(confirmed=data['confirmed']['value'],
                            deaths=data['deaths']['value'],
                            recovered=data['recovered']['value'],
                            active_care=data['activeCare']['value'],
                            country_id="id",
                            created=updated,
                            updated=updated)
        session.add(new_status)
        return jsonify(_id_beauty(data, 0)), 200
Beispiel #3
0
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
Beispiel #4
0
def odi_api(state):
    DAERAH = helper.DAERAH
    req = requests.get(ODI_API)
    if not req.status_code == 200:
        return []
    prov = {prov["provinsi"]: prov for prov in req.json()["data"]}
    hasil = prov[DAERAH[state]]
    todayIsNone = True

    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": ""
        }
    }

    result['metadata'] = {
        "source": "https://indonesia-covid-19.mathdro.id/",
        "province": DAERAH[state].upper()
    }

    with helper.transaction() as tx:
        get_state = tx.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())
            tx.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"]

    return result
class StatusPageIo(Provider):

    _operational_message = 'All Systems Operational'
    _BRAND_TRADE_MARK = 'http://www.statuspage.io'

    def __init__(self, url):
        self._url = url
        self._service_status = Status(url, 'statuspage.io')

    def _getHtml(self):
        super()._getHtml()

    def _getIncidentMessage(self):
        rssUrl = self._url + 'history.rss'
        d = feedparser.parse(rssUrl)
        messageList = []
        for i in range(min(5, len(d.entries))):
            msg = {
                'title': d.entries[i].title,
                'description': d.entries[i].description,
                'link': d.entries[i].link,
                'publishedAt': d.entries[i].published
            }
            messageList.append(msg)

        return messageList

    def _getScheduledMessages(self):
        scheduledMessageList = []
        scheduled_maintenances_container = self._dom(
            '.scheduled-maintenances-container')

        if (len(scheduled_maintenances_container) > 0):
            all_scheduled_msg = self._dom('.scheduled-maintenance')
            for cur in self._dom('.scheduled-maintenance').items():
                msg = {
                    'title':
                    cur.find('.incident-title a').text().strip(),
                    'description':
                    cur.find('.updates-container .update').text().strip(),
                    'scheduledDate':
                    cur.find('.incident-title small').text().strip(),
                    'publishedAt':
                    cur.find(
                        '.updates-container .update small').text().strip()
                }
                scheduledMessageList.append(msg)

        return scheduledMessageList

    def _parse(self):
        status_description = self._dom(
            '.page-status span.status').html().strip()
        status = StatusValue.unknown

        if status_description == self._operational_message:
            status = StatusValue.operational
        else:
            page_status_dom = self._dom('.page-status')
            if len(page_status_dom) > 0:
                if page_status_dom.hasClass('status-minor'):
                    status = StatusValue.minor
                elif page_status_dom.hasClass('status-major'):
                    status = StatusValue.major

            unresolved_indcident_dom = self._dom('.unresolved-incidents')

            if len(unresolved_indcident_dom) > 0:
                html = unresolved_indcident_dom.html()
                if html.find('impact-major') != -1:
                    status = StatusValue.major
                elif html.find('impact-minor') != -1:
                    status = StatusValue.minor
                elif html.find('impact-none') != -1:
                    status = StatusValue.non_impact

        scheduled_msg_list = self._getScheduledMessages()
        incident_msg_list = self._getIncidentMessage()
        self._service_status.set(status, scheduled_msg_list, incident_msg_list)

    def check(self):
        d = self._dom('.powered-by a')
        if (len(d) == 0):
            return False
        return d.attr('href') == self._BRAND_TRADE_MARK

    def run(self):
        self._getHtml()
        if self.check() == False:
            print("Provider doesn't match")
            return
        self._parse()
 def __init__(self, url):
     self._url = url
     self._service_status = Status(url, 'statuspage.io')
Beispiel #7
0
class StatusIo(Provider):
    _operational_message = 'All Systems Operational'

    def __init__(self, url):
        self._url = url
        self._service_status = Status(url, 'status.io')

    def _getHtml(self):
        super()._getHtml()

    def _getIncidentMessage(self):
        rssUrl = self._dom('#tab_rss a').attr('href')
        d = feedparser.parse(rssUrl)
        d.entries.reverse()
        messageList = []
        for i in range(min(5, len(d.entries))):
            msg = {
                'title': d.entries[i].title,
                'description': d.entries[i].description,
                'link': d.entries[i].link,
                'publishedAt': d.entries[i].published
            }
            messageList.append(msg)

        return messageList

    def _getScheduledMessages(self):
        scheduledMessageList = []
        scheduled_maintenances_container = self._dom(
            '#section_maintenance_scheduled')

        if (len(scheduled_maintenances_container) > 0):
            all_scheduled_msg = self._dom(
                'div[id^="statusio_maintenance_scheduled"]')
            for cur in all_scheduled_msg.items():
                detailed_info_dom = list(
                    cur.find('.maintenance_section.event_inner_text').items())
                msg = {
                    'title':
                    cur.find('.maintenance_status_description').siblings(
                        'a').text().strip(),
                    'description':
                    detailed_info_dom[3].text().strip(),
                    'scheduledDate':
                    detailed_info_dom[0].text().strip(),
                    'publishedAt':
                    None
                }
                scheduledMessageList.append(msg)

        return scheduledMessageList

    def _parse(self):
        status_description = self._dom('#statusbar_text').html().strip()
        status = StatusValue.unknown

        if status_description == self._operational_message:
            status = StatusValue.operational
        elif status_description.find('impact-major'):
            status = StatusValue.major
        elif status_description.find('impact-minor'):
            status = StatusValue.minor
        else:
            status = StatusValue.unknown

        scheduled_msg_list = self._getScheduledMessages()
        incident_msg_list = self._getIncidentMessage()
        self._service_status.set(status, scheduled_msg_list, incident_msg_list)

    def check(self):
        d = self._dom('#statusio_branding')

        if (len(d) == 0):
            return False
        return True

    def run(self):
        self._getHtml()
        if self.check() == False:
            print("Provider doesn't match")
            return
        self._parse()