Skip to content

Commit

Permalink
rename to fix syntaxerror
Browse files Browse the repository at this point in the history
  • Loading branch information
apex2504 committed Mar 19, 2020
1 parent 2836197 commit 8bee267
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 120 deletions.
4 changes: 2 additions & 2 deletions corona-api/__init__.py → corona_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .client import Client
from .statistics import GlobalStatistics, CountryStatistics
from .client import Client
from .statistics import GlobalStatistics, CountryStatistics
from .utils import format_date, format_number
172 changes: 86 additions & 86 deletions corona-api/client.py → corona_api/client.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
from datetime import datetime
import aiohttp
from .utils import format_number
from .statistics import GlobalStatistics, CountryStatistics

class APIerror(Exception):
pass

class Client:
"""
Handles interactions with the corona.lmao.ninja API
"""
def __init__(self):
self.session = aiohttp.ClientSession()
self.global_data = 'https://corona.lmao.ninja/all'
self.all_countries = 'https://corona.lmao.ninja/countries'
self.country_data = 'https://corona.lmao.ninja/countries/{}'

async def all(self):
"""
Get the global stats for Coronavirus COVID-19
"""
async with self.session.get(self.global_data) as resp:
if not resp.status == 200:
raise APIerror('Failed to get global data.')

global_data = await resp.json()

async with self.session.get(self.all_countries) as resp:
if not resp.status == 200:
raise APIerror('Failed to get global data.')

country_data = await resp.json()

cases = global_data.get("cases", 0)
deaths = global_data.get("deaths", 0)
recoveries = global_data.get("recovered", 0)
updated_epoch = global_data.get("updated", None)
updated = datetime.fromtimestamp(updated_epoch/1000.0)

today_cases = 0
today_deaths = 0
total_critical = 0

for c in country_data:
today_cases += c["todayCases"] if c["todayCases"] else 0
today_deaths += c["todayDeaths"] if c["todayDeaths"] else 0
total_critical += c["critical"] if c["critical"] else 0

return GlobalStatistics(cases,
deaths,
recoveries,
today_cases,
today_deaths,
total_critical,
updated
)


async def get_country_data(self, country):
"""
Get the data for a specific country.
"""
async with self.session.get(self.country_data.format(country)) as resp:
if not resp.status == 200:
raise APIerror('Failed to get country data data.')

country_stats = await resp.json()

country_name = country_stats.get("country", "Null")
total_country_cases = country_stats.get("cases", 0)
total_country_deaths = country_stats.get("deaths", 0)
total_country_recoveries = country_stats.get("recovered", 0)
today_cases = country_stats.get("todayCases", 0)
today_deaths = country_stats.get("todayDeaths", 0)
total_critical = country_stats.get("critical", 0)
cases_per_million = country_stats.get("casesPerOneMillion", 0)

return CountryStatistics(country_name,
total_country_cases,
total_country_deaths,
total_country_recoveries,
today_cases, today_deaths,
total_critical,
cases_per_million
)
from datetime import datetime
import aiohttp
from .utils import format_number
from .statistics import GlobalStatistics, CountryStatistics

class APIerror(Exception):
pass

class Client:
"""
Handles interactions with the corona.lmao.ninja API
"""
def __init__(self):
self.session = aiohttp.ClientSession()
self.global_data = 'https://corona.lmao.ninja/all'
self.all_countries = 'https://corona.lmao.ninja/countries'
self.country_data = 'https://corona.lmao.ninja/countries/{}'

async def all(self):
"""
Get the global stats for Coronavirus COVID-19
"""
async with self.session.get(self.global_data) as resp:
if not resp.status == 200:
raise APIerror('Failed to get global data.')

global_data = await resp.json()

async with self.session.get(self.all_countries) as resp:
if not resp.status == 200:
raise APIerror('Failed to get global data.')

country_data = await resp.json()

cases = global_data.get("cases", 0)
deaths = global_data.get("deaths", 0)
recoveries = global_data.get("recovered", 0)
updated_epoch = global_data.get("updated", None)
updated = datetime.fromtimestamp(updated_epoch/1000.0)

today_cases = 0
today_deaths = 0
total_critical = 0

for c in country_data:
today_cases += c["todayCases"] if c["todayCases"] else 0
today_deaths += c["todayDeaths"] if c["todayDeaths"] else 0
total_critical += c["critical"] if c["critical"] else 0

return GlobalStatistics(cases,
deaths,
recoveries,
today_cases,
today_deaths,
total_critical,
updated
)


async def get_country_data(self, country):
"""
Get the data for a specific country.
"""
async with self.session.get(self.country_data.format(country)) as resp:
if not resp.status == 200:
raise APIerror('Failed to get country data data.')

country_stats = await resp.json()

country_name = country_stats.get("country", "Null")
total_country_cases = country_stats.get("cases", 0)
total_country_deaths = country_stats.get("deaths", 0)
total_country_recoveries = country_stats.get("recovered", 0)
today_cases = country_stats.get("todayCases", 0)
today_deaths = country_stats.get("todayDeaths", 0)
total_critical = country_stats.get("critical", 0)
cases_per_million = country_stats.get("casesPerOneMillion", 0)

return CountryStatistics(country_name,
total_country_cases,
total_country_deaths,
total_country_recoveries,
today_cases, today_deaths,
total_critical,
cases_per_million
)

40 changes: 20 additions & 20 deletions corona-api/statistics.py → corona_api/statistics.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
class GlobalStatistics:
def __init__(self, cases, deaths, recoveries, today_cases, today_deaths, total_critical, updated):
self.cases = cases
self.deaths = deaths
self.recoveries = recoveries
self.today_cases = today_cases
self.today_deaths = today_deaths
self.critical = total_critical
self.updated = updated


class CountryStatistics:
def __init__(self, name, cases, deaths, recoveries, today_cases, today_deaths, critical, cases_per_million):
self.name = name
self.cases = cases
self.deaths = deaths
self.recoveries = recoveries
self.today_cases = today_cases
self.today_deaths = today_deaths
self.critical = critical
class GlobalStatistics:
def __init__(self, cases, deaths, recoveries, today_cases, today_deaths, total_critical, updated):
self.cases = cases
self.deaths = deaths
self.recoveries = recoveries
self.today_cases = today_cases
self.today_deaths = today_deaths
self.critical = total_critical
self.updated = updated


class CountryStatistics:
def __init__(self, name, cases, deaths, recoveries, today_cases, today_deaths, critical, cases_per_million):
self.name = name
self.cases = cases
self.deaths = deaths
self.recoveries = recoveries
self.today_cases = today_cases
self.today_deaths = today_deaths
self.critical = critical
self.cases_per_million = cases_per_million
12 changes: 6 additions & 6 deletions corona-api/utils.py → corona_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime

def format_date(dt):
return dt.strftime('%d %b %Y %H:%M')

def format_number(number):
from datetime import datetime

def format_date(dt):
return dt.strftime('%d %b %Y %H:%M')

def format_number(number):
return '{:,d}'.format(number)
10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from distutils.core import setup

setup(
name = 'corona-api',
packages = ['corona-api'],
version = '0.1',
name = 'corona_api',
packages = ['corona_api'],
version = '0.1.1',
license='MIT',
description = 'An asynchronous wrapper for the corona.lmao.ninja API written in Python.',
author = 'Rob Wainwright',
Expand All @@ -14,9 +14,7 @@
'aiohttp',
],
classifiers=[
'Development Status :: 4 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
Expand Down

0 comments on commit 8bee267

Please sign in to comment.