Giter Site home page Giter Site logo

nf1s / covid Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 24.0 3.13 MB

Python package to fetch information regarding the novel corona virus provided by Johns Hopkins university and worldometers.info

Home Page: https://ahmednafies.github.io/covid/

License: MIT License

Python 98.81% Makefile 1.19%
covid-19 covid-virus coronavirus covid covid-2019 covid-api python python36

covid's Introduction

Covid

CircleCI codecov GitHub Pipenv locked Python version Downloads GitHub

Description

Python package to get information regarding the novel corona virus provided by Johns Hopkins university and worldometers.info

Full Documentation can be found here

corona.jpeg

Requirements

python >= 3.6

How to install

pip install covid

Dependencies

pydantic
requests

How to use

John Hopkins University API

john_hopkins

Get All Data

from covid import Covid

covid = Covid()
covid.get_data()

Result

[
    {
        'id': '53',
        'country': 'China',
        'confirmed': 81020,
        'active': 9960,
        'deaths': 3217,
        'recovered': 67843,
        'latitude': 30.5928,
        'longitude': 114.3055,
        'last_update': 1584097775000
    },
    {
        'id': '115',
        'country': 'Italy',
        'confirmed': 24747,
        'active': 20603,
        'deaths': 1809,
        'recovered': 2335,
        'latitude': 41.8719,
        'longitude': 12.5674,
        'last_update': 1584318130000
    },
    ...
]

List Countries

This comes in handy when you need to know the available names of countries when using get_status_by_country_name, eg. "The Republic of Moldova" or just "Moldova" So use this when you need to know the country exact name that you can use.

countries = covid.list_countries()

Result

[
    {'id': '53', 'country': 'China'},
    {'id': '115', 'country': 'Italy'}
    ...
]

Get Status By Country ID

italy_cases = covid.get_status_by_country_id(115)

Result

{
    'id': '115',
    'country': 'Italy',
    'confirmed': 24747,
    'active': 20603,
    'deaths': 1809,
    'recovered': 2335,
    'latitude': 41.8719,
    'longitude': 12.5674,
    'last_update': 1584318130000
}

Get Status By Country Name

italy_cases = covid.get_status_by_country_name("italy")

Result

{
    'id': '115',
    'country': 'Italy',
    'confirmed': 24747,
    'active': 20603,
    'deaths': 1809,
    'recovered': 2335,
    'latitude': 41.8719,
    'longitude': 12.5674,
    'last_update': 1584318130000
}

Get Total Confirmed cases

confirmed = covid.get_total_confirmed_cases()

Get Total Deaths

deaths = covid.get_total_deaths()

Getting data from Worldometers.info

worldometers

covid = Covid(source="worldometers")

Get Data

covid.get_data()

Result

[
    {
        'country': 'USA',
        'confirmed': 311637,
        'new_cases': 280,
        'deaths': 8454,
        'recovered': 14828,
        'active': 288355,
        'critical': 8206,
        'new_deaths': 2,
        'total_tests': 1656897,
        'total_tests_per_million': Decimal('0'),
        'total_cases_per_million': Decimal('941'),
        'total_deaths_per_million': Decimal('26')
    },
    {
        'active': 1376,
        'confirmed': 81669,
        'country': 'China',
        'critical': 295,
        'deaths': 3329,
        'new_cases': 30,
        'new_deaths': 3,
        'recovered': 76964,
        'total_cases_per_million': Decimal('57'),
        'total_deaths_per_million': Decimal('2'),
        'total_tests': 0,
        'total_tests_per_million': Decimal('0')
    }
    ...
]

Get Status By Country Name

covid.get_status_by_country_name("italy")

Result

{
    'active': 88274,
    'confirmed': 124632,
    'country': 'Italy',
    'critical': 3994,
    'deaths': 15362,
    'new_cases': 0,
    'new_deaths': 0,
    'recovered': 20996,
    'total_cases_per_million': Decimal('2061'),
    'total_deaths_per_million': Decimal('254'),
    'total_tests': 657224,
    'total_tests_per_million': Decimal('0')
 }

List Countries

countries = covid.list_countries()

Result

[
    'china',
    'italy',
    'usa',
    'spain',
    'germany',
...
]

Get Total Active cases

active = covid.get_total_active_cases()

Get Total Confirmed cases

confirmed = covid.get_total_confirmed_cases()

Get Total Recovered cases

recovered = covid.get_total_recovered()

Get Total Deaths

deaths = covid.get_total_deaths()

CLI 2.0 (New)

covid --help

Get all data

John Hopkins source (default)

covid

or

covid -s john_hopkins

Worldometers source

covid -s worldometers

List Countries

This comes in handy when you need to know the available names of countries when using covid -s 'source' -c 'country_name', eg. "The Republic of Moldova" or just "Moldova" So use this when you need to know the country exact name that you can use.

covid -s worldometers --list-countries

Get Status By Country Name

covid -s worldometers -c sweden

Get Total Active cases

covid -s worldometers -o active

Get Total Confirmed cases

covid -s worldometers -o confirmed

Get Total Recovered cases

covid -s worldometers -o recovered

Get Total Deaths

covid -s worldometers -o deaths

covid's People

Contributors

hellosaumil avatar nf1s avatar nkitanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

covid's Issues

ModuleNotFoundError: No module named 'covid.john_hopkins'

Environment: Manjaro Linux [x64]
Python version: 3.8.2 (on base system AND virtualenv)
Covid library version: 2.0.1 (from PyPI)
File/Script: https://github.com/kandnub/TG-UserBot/blob/master/userbot/plugins/covid.py

Here's the traceback of the error [on my virtualenv, Python 3.8.2]

[11:12:18 / ERROR] userbot.utils.pluginManager: Failed to import userbot.plugins.covid due to the error(s) below.
[11:12:18 / ERROR] userbot.utils.pluginManager: No module named 'covid.john_hopkins'
Traceback (most recent call last):
  File "/mnt/sda4/GitHub Repos/PaperplaneRemix/userbot/utils/pluginManager.py", line 250, in _import_module
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/mnt/sda4/GitHub Repos/PaperplaneRemix/userbot/plugins/covid.py", line 17, in <module>
    from covid import Covid
  File "/mnt/sda4/PPRPLNRMX/lib/python3.8/site-packages/covid/__init__.py", line 2, in <module>
    from .john_hopkins import Covid as JohnHopkinsCovid
ModuleNotFoundError: No module named 'covid.john_hopkins'

The same on my base system [Manjaro Linux (latest x64, KDE), Python 3.8.2]

[avinash@rustbucket-manjaro ~]$ python3 --version
Python 3.8.2

[avinash@rustbucket-manjaro ~]$ pip3 --version
pip 20.0.2 from /usr/lib/python3.8/site-packages/pip (python 3.8)

[avinash@rustbucket-manjaro ~]$ pip3 install --user covid
Processing ./.cache/pip/wheels/e3/67/27/ca2216ad133cb7d84eb2cc7a68fd322ae65531e071779ee798/covid-2.0.1-py3-none-any.whl
Requirement already satisfied: requests in /usr/lib/python3.8/site-packages (from covid) (2.23.0)
Collecting pydantic
  Using cached pydantic-1.4-cp38-cp38-manylinux2010_x86_64.whl (9.6 MB)
Requirement already satisfied: chardet>=3.0.2 in /usr/lib/python3.8/site-packages (from requests->covid) (3.0.4)
Requirement already satisfied: idna>=2.5 in /usr/lib/python3.8/site-packages (from requests->covid) (2.9)
Requirement already satisfied: urllib3>=1.21.1 in /usr/lib/python3.8/site-packages (from requests->covid) (1.25.8)
Installing collected packages: pydantic, covid
Successfully installed covid-2.0.1 pydantic-1.4

[avinash@rustbucket-manjaro ~]$ python
Python 3.8.2 (default, Feb 26 2020, 22:21:03) 
[GCC 9.2.1 20200130] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import covid
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/avinash/.local/lib/python3.8/site-packages/covid/__init__.py", line 2, in <module>
    from .john_hopkins import Covid as JohnHopkinsCovid
ModuleNotFoundError: No module named 'covid.john_hopkins'
>>> 

Error on startup!

  File "app.py", line 13, in <module>
    from covid import covid
  File "/home/runner/api/covid/covid/__init__.py", line 2, in <module>
    from covid import config
ImportError: cannot import name 'config' from 'covid' (unknown location)
 

Validation error

api = Covid(source="worldometers")
api.get_status_by_country_name('india')
Traceback (most recent call last):
File "", line 1, in
File "/home/amritendu/.local/lib/python3.6/site-packages/covid/worldometers/covid.py", line 60, in get_status_by_country_name
return CovidModel(**country_data).dict()
File "pydantic/main.py", line 283, in pydantic.main.BaseModel.init
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
TotDeaths/1M pop
field required (type=value_error.missing)

Wordometers get by country not working

Since today if wordmeters is used as a source get by country is not working. Also list_countries returns no counties only list of numbers. I guess wordometers changed something in html and BS is not parsing it correctly.


In [19]: covid.list_countries()
Out[19]:
['',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
...


covid.get_status_by_country_name('Belgium')
....
KeyError: 'belgium'```

Can provide yesterday's data?

First, thanks for you effort.

Is it possible to provide data in tab "Yesterday" and "2 Days Ago" ? I want to make a trend tracker for daily changes.

StopIteration when calling covid.get_status_by_country_name("US")

Getting the following error when trying to call get_status_by_country_name on "US" works for other countries!

>>> covid.get_status_by_country_name("US")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../.conda/envs/py36/lib/python3.6/site-packages/covid/__init_
_.py", line 281, in get_status_by_country_name
    country = next(country)
StopIteration

USA not working.

When I try to get USA data, it says Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/covid/john_hopkins/covid.py", line 237, in get_status_by_country_name
return CovidModel(**case).dict()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pydantic/main.py", line 362, in init
raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
Active
none is not an allowed value (type=type_error.none.not_allowed)

cases = covid.get_status_by_country_name("US")
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/covid/john_hopkins/covid.py", line 237, in get_status_by_country_name
return CovidModel(**case).dict()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pydantic/main.py", line 362, in init
raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
Active
none is not an allowed value (type=type_error.none.not_allowed)

John Hopkins Retrieval For Sweden and MS Zaandam No Longer Succeeds

Hello @ahmednafies ,
Thanks for the python module. It has been working for me for all countries but just today I discovered that for some reason retrieval for Sweden fails. The error observed is:

Netherlands

{'id': '12', 'country': 'Netherlands', 'confirmed': 47945, 'active': 41729, 'deaths': 6035, 'recovered': 181, 'latitude': 52.3167, 'longitude': 5.55, 'last_update': 1591652013000}

Sweden

Traceback (most recent call last):
  File "nuertey_covid19_final.py", line 76, in <module>
    combined_output = [retrieve_covid_statistics(country) for country in data['name']] 
  File "nuertey_covid19_final.py", line 76, in <listcomp>
    combined_output = [retrieve_covid_statistics(country) for country in data['name']] 
  File "nuertey_covid19_final.py", line 68, in retrieve_covid_statistics
    country_status = cov_19.get_status_by_country_name(country_name_input)
  File "/home/rena/MyEnv37/lib/python3.7/site-packages/covid/john_hopkins/covid.py", line 238, in get_status_by_country_name
    return CovidModel(**case).dict()
  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
Recovered
  none is not an allowed value (type=type_error.none.not_allowed)

My package versions are as follows:

pip show pydantic
Name: pydantic
Version: 1.4
Summary: Data validation and settings management using python 3.6 type hinting
Home-page: https://github.com/samuelcolvin/pydantic
Author: Samuel Colvin
Author-email: [email protected]
License: MIT
Location: /home/rena/MyEnv37/lib/python3.7/site-packages
Requires: 
Required-by: covid

pip show requests
Name: requests
Version: 2.22.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /home/rena/MyEnv37/lib/python3.7/site-packages
Requires: chardet, idna, urllib3, certifi
Required-by: world-bank-data, tensorboard, scrapelib, requests-oauthlib, rebound-cli, Quandl, percy, pandas-datareader, gspread, google-api-core, folium, dash, covid, alpha-vantage

pip show covid
Name: covid
Version: 2.2.0
Summary: Python SDK to get information regarding the novel corona virus provided by Johns Hopkins university and worldometers.info
Home-page: https://ahmednafies.github.io/covid/
Author: Ahmed Nafies
Author-email: [email protected]
License: MIT
Location: /home/rena/MyEnv37/lib/python3.7/site-packages
Requires: requests, pydantic, beautifulsoup4
Required-by: 

Am I missing something?
Thanks
Nuertey

[worldometers] Error while fetching data by country name.

Environment: Manjaro Linux [x64]
Python version: 3.8.2 (on base system AND virtualenv)
Library version: 2.0.5 (from PyPI)

Here's a log of my shell session, contains the traceback.

Python 3.8.2 (default, Feb 26 2020, 22:21:03) 
[GCC 9.2.1 20200130] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from covid import Covid
>>> covid = Covid(source="worldometers")
>>> covid.get_status_by_country_name("India")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/sda4/PPRPLNRMX/lib/python3.8/site-packages/covid/worldometers/covid.py", line 60, in get_status_by_country_name
    return CovidModel(**country_data).dict()
  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
TotDeaths/1M pop
  field required (type=value_error.missing)
>>> covid.get_status_by_country_name("italy")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/sda4/PPRPLNRMX/lib/python3.8/site-packages/covid/worldometers/covid.py", line 60, in get_status_by_country_name
    return CovidModel(**country_data).dict()
  File "pydantic/main.py", line 283, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for CovidModel
TotDeaths/1M pop
  field required (type=value_error.missing)
>>>

Recovered shows - none

Getting this issue while calling the function. Everything seems fine except active and recovered. Both of them show none.
Can it be fixed by any means?
Thanks for such an awesome package!

{'id': '80', 'country': 'India', 'confirmed': 32156493, 'active': None, 'deaths': 430732, 'recovered': None, 'latitude': 20.593684, 'longitude': 78.96288, 'last_update': 1628918468000}
{'id': '186', 'country': 'United Kingdom', 'confirmed': 6241447, 'active': None, 'deaths': 131116, 'recovered': None, 'latitude': 55.0, 'longitude': -3.0, 'last_update': 1628918468000}

information about Active and Recovered cases are not longer available

Hi,

The the information about Active and Recovered cases are not longer available, the functions return None instead

To reproduce :

import covid
c = Covid()

c.get_status_by_country_name('us')
{'id': '182', 'country': 'US', 'confirmed': 45959154, 'active': None, 'deaths': 745725, 'recovered': None, 'latitude': 40.0, 'longitude': -100.0, 'last_update': 1635697327000}


c.get_status_by_country_name('france')
{'id': '63', 'country': 'France', 'confirmed': 7262198, 'active': None, 'deaths': 118613, 'recovered': None, 'latitude': 46.2276, 'longitude': 2.2137, 'last_update': 1635697327000}

c.get_status_by_country_name('italy')
{'id': '86', 'country': 'Italy', 'confirmed': 4767440, 'active': None, 'deaths': 132074, 'recovered': None, 'latitude': 41.8719, 'longitude': 12.5674, 'last_update': 1635697327000}

Using the get_data() function, you can validate that those information are not longer available

Kind Regards

Error for Jhu source

Exception: {'error': {'code': 400, 'message': 'Invalid URL', 'details': ['Invalid URL']}}

It seems jhu is dead for now

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.