Skip to content

Commit

Permalink
chore: linting fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hurshd0 committed Apr 26, 2020
1 parent 256f183 commit 76b07da
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 80 deletions.
114 changes: 43 additions & 71 deletions api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
from api.utils import read_country_data
from api.utils import read_county_stats
from api.utils import read_states

# from api.utils import read_county_stats_zip_ny
from api.config import DataReadingError

# Starts the FastAPI Router to be used by the FastAPI app.
router = APIRouter()
_logger = get_logger(logger_name=__name__)
tm = TwitterMongo(app_config.DB_NAME,
app_config.COLLECTION_TWITTER,
verbose=False)
tm = TwitterMongo(app_config.DB_NAME, app_config.COLLECTION_TWITTER, verbose=False)


###############################################################################
Expand All @@ -46,7 +45,7 @@ class RootOutput(BaseModel):
message: str


@router.get("/")#, response_model=RootOutput)
@router.get("/") # , response_model=RootOutput)
async def root() -> JSONResponse:
"""Root URL, redirect to ReDoc API doc
"""
Expand Down Expand Up @@ -78,9 +77,7 @@ class NewsOut(BaseModel):
message: List[News]


@router.get("/news",
response_model=NewsOut,
responses={404: {"model": Message}})
@router.get("/news", response_model=NewsOut, responses={404: {"model": Message}})
async def get_gnews() -> JSONResponse:
"""Fetch US news from Google News API and return the results in JSON
"""
Expand All @@ -98,9 +95,7 @@ async def get_gnews() -> JSONResponse:
return json_data


@router.post("/news",
response_model=NewsOut,
responses={404: {"model": Message}})
@router.post("/news", response_model=NewsOut, responses={404: {"model": Message}})
async def post_gnews(news: NewsInput) -> JSONResponse:
"""Fetch specific state and topic news from Google News API and return the
results in JSON
Expand All @@ -114,8 +109,7 @@ async def post_gnews(news: NewsInput) -> JSONResponse:
except DataReadingError as ex:
_logger.warning(f"Endpoint: /news --- POST --- {ex}")
return JSONResponse(
status_code=404,
content={"message": f"[Error] post /News API: {ex}"}
status_code=404, content={"message": f"[Error] post /News API: {ex}"}
)

return json_data
Expand Down Expand Up @@ -147,9 +141,9 @@ class TwitterOutput(BaseModel):
message: UserTweets


@router.get("/twitter",
response_model=TwitterOutput,
responses={404: {"model": Message}})
@router.get(
"/twitter", response_model=TwitterOutput, responses={404: {"model": Message}}
)
async def get_twitter() -> JSONResponse:
"""Fetch and return Twitter data from MongoDB connection."""
try:
Expand All @@ -164,23 +158,20 @@ async def get_twitter() -> JSONResponse:

json_data = {
"success": True,
"message": {"username": username,
"full_name": full_name,
"tweets": tweets},
"message": {"username": username, "full_name": full_name, "tweets": tweets},
}
del tweets
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /twitter --- GET --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get /twitter API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get /twitter API: {ex}")

return json_data


@router.post("/twitter",
response_model=TwitterOutput,
responses={404: {"model": Message}})
@router.post(
"/twitter", response_model=TwitterOutput, responses={404: {"model": Message}}
)
async def post_twitter(twyuser: TwitterInput) -> JSONResponse:
"""Fetch and return Twitter data from MongoDB connection."""
try:
Expand All @@ -193,17 +184,14 @@ async def post_twitter(twyuser: TwitterInput) -> JSONResponse:
tweets = sorted(tweets, key=lambda i: i["created_at"], reverse=True)
json_data = {
"success": True,
"message": {"username": username,
"full_name": full_name,
"tweets": tweets},
"message": {"username": username, "full_name": full_name, "tweets": tweets},
}

del tweets
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /twitter --- POST --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] post /twitter API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] post /twitter API: {ex}")

return json_data

Expand Down Expand Up @@ -237,9 +225,7 @@ class CountyOut(BaseModel):


@cached(cache=TTLCache(maxsize=1, ttl=3600))
@router.get("/county",
response_model=CountyOut,
responses={404: {"model": Message}})
@router.get("/county", response_model=CountyOut, responses={404: {"model": Message}})
async def get_county_data() -> JSONResponse:
"""Get all US county data and return it as a big fat json string. Respond
with 404 if run into error.
Expand All @@ -252,15 +238,12 @@ async def get_county_data() -> JSONResponse:
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /county --- GET --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get '/county' API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get '/county' API: {ex}")

return json_data


@router.post("/county",
response_model=CountyOut,
responses={404: {"model": Message}})
@router.post("/county", response_model=CountyOut, responses={404: {"model": Message}})
def post_county(county: CountyInput) -> JSONResponse:
"""Get all US county data and return it as a big fat json string. Respond
with 404 if run into error.
Expand All @@ -272,8 +255,7 @@ def post_county(county: CountyInput) -> JSONResponse:
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /county --- POST --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get '/county' API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get '/county' API: {ex}")

return json_data

Expand All @@ -299,9 +281,7 @@ class StateOutput(BaseModel):


@cached(cache=TTLCache(maxsize=3, ttl=3600))
@router.post("/state",
response_model=StateOutput,
responses={404: {"model": Message}})
@router.post("/state", response_model=StateOutput, responses={404: {"model": Message}})
async def post_state(state: StateInput) -> JSONResponse:
"""Fetch state level data time series for a single state, ignoring the
unattributed and out of state cases.
Expand All @@ -315,8 +295,7 @@ async def post_state(state: StateInput) -> JSONResponse:
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /state --- POST --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get /country API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get /country API: {ex}")

return json_data

Expand All @@ -342,9 +321,9 @@ class CountryOutput(BaseModel):


@cached(cache=TTLCache(maxsize=3, ttl=3600))
@router.post("/country",
response_model=CountryOutput,
responses={404: {"model": Message}})
@router.post(
"/country", response_model=CountryOutput, responses={404: {"model": Message}}
)
async def get_country(country: CountryInput) -> JSONResponse:
"""Fetch country level data time series for a single country.
- Cached for 1 hour
Expand All @@ -355,11 +334,11 @@ async def get_country(country: CountryInput) -> JSONResponse:
json_data = {"success": True, "message": data}
except Exception as ex:
_logger.warning(f"Endpoint: /country --- GET --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get /country API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get /country API: {ex}")

return json_data


###############################################################################
#
# Stats Endpoints
Expand All @@ -383,9 +362,7 @@ class StatsOutput(BaseModel):
message: Stats


@router.get("/stats",
response_model=StatsOutput,
responses={404: {"model": Message}})
@router.get("/stats", response_model=StatsOutput, responses={404: {"model": Message}})
async def get_stats() -> JSONResponse:
"""Get overall tested, confirmed, and deaths stats from the database
and return it as a JSON string.
Expand All @@ -395,14 +372,11 @@ async def get_stats() -> JSONResponse:
json_data = {"success": True, "message": data}
except Exception as ex:
_logger.warning(f"Endpoint: /stats --- GET --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get /stats API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get /stats API: {ex}")
return json_data


@router.post("/stats",
response_model=StatsOutput,
responses={404: {"model": Message}})
@router.post("/stats", response_model=StatsOutput, responses={404: {"model": Message}})
async def post_stats(stats: StatsInput) -> JSONResponse:
"""Get overall tested, confirmed, and deaths stats from the database
and return it as a JSON string.
Expand All @@ -412,10 +386,10 @@ async def post_stats(stats: StatsInput) -> JSONResponse:
json_data = {"success": True, "message": data}
except Exception as ex:
_logger.warning(f"Endpoint: /stats --- POST --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] post /stats API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] post /stats API: {ex}")
return json_data


###############################################################################
#
# ZIP Endpoint
Expand All @@ -424,6 +398,7 @@ async def post_stats(stats: StatsInput) -> JSONResponse:
class ZIPInput(BaseModel):
zip_code: str = "70030"


class ZIPStats(BaseModel):
county_name: str = "St. Charles"
state_name: str = "Louisiana"
Expand All @@ -436,35 +411,33 @@ class ZIPStats(BaseModel):
longitude: float
last_update: str = "2020-04-17 19:50 EDT"


class ZIPOutput(BaseModel):
success: bool
message: ZIPStats


@router.post("/zip",
response_model=ZIPOutput,
responses={404: {"model": Message}})
@router.post("/zip", response_model=ZIPOutput, responses={404: {"model": Message}})
def post_zip(zip_code: ZIPInput) -> JSONResponse:
"""Returns county stats for the zip code input.
"""

try:
zip_info = zipcodes.matching(zip_code.zip_code)[0]
except Exception as ex:
message = (f"ZIP code {zip_code.zip_code}"
" not found in US Zipcode database.")
message = f"ZIP code {zip_code.zip_code}" " not found in US Zipcode database."
_logger.warning(f"Endpoint: /zip --- POST --- {ex} {message}")
raise HTTPException(status_code=422,
detail=f"[Error] POST '/zip' {ex} {message}")

raise HTTPException(
status_code=422, detail=f"[Error] POST '/zip' {ex} {message}"
)

try:
county = zip_info['county'].rsplit(' ', 1)[0]
state = zip_info['state']
county = zip_info["county"].rsplit(" ", 1)[0]
state = zip_info["state"]
_logger.info(f"State: {state}, County: {county}")
if state == "NY":
nyc_counties = ["Bronx", "Kings", "Queens", "Richmond"]
if county in nyc_counties:
if county in nyc_counties:
county = "New York"
data = read_county_stats(state, county)[0]
else:
Expand All @@ -474,7 +447,6 @@ def post_zip(zip_code: ZIPInput) -> JSONResponse:
gc.collect()
except Exception as ex:
_logger.warning(f"Endpoint: /zip --- POST --- {ex}")
raise HTTPException(status_code=404,
detail=f"[Error] get '/zip' API: {ex}")
raise HTTPException(status_code=404, detail=f"[Error] get '/zip' API: {ex}")

return json_data
15 changes: 6 additions & 9 deletions api/utils/gnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from api.config import app_config


def get_state_topic_google_news(state: str,
topic: str,
max_rows: int = 10) -> Dict:
def get_state_topic_google_news(state: str, topic: str, max_rows: int = 10) -> Dict:
"""This function takes a US State name (string dtype) and a topic of
interest (string dtype). The output is a pandas DataFrame with articles,
urls, and publishing times for articles containing the state and topic
Expand All @@ -23,8 +21,9 @@ def get_state_topic_google_news(state: str,
type checking.
"""

url = ("https://news.google.com/rss/search?"
f"q={state}+{topic}&hl=en-US&gl=US&ceid=US:en"
url = (
"https://news.google.com/rss/search?"
f"q={state}+{topic}&hl=en-US&gl=US&ceid=US:en"
)
list_of_titles = []
list_of_article_links = []
Expand All @@ -46,8 +45,7 @@ def get_state_topic_google_news(state: str,
state_id_for_articles.append(state)

df = pd.DataFrame(
[list_of_titles, list_of_article_links,
list_of_pubdates, state_id_for_articles]
[list_of_titles, list_of_article_links, list_of_pubdates, state_id_for_articles]
).T
df.columns = ["title", "url", "published", "state"]
df["source"] = df["title"].str.split("-").str[-1]
Expand All @@ -74,8 +72,7 @@ def get_us_news(max_rows: int = 50) -> Dict:
df = pd.DataFrame(df[["title", "url", "publishedAt"]])
df = df.rename(columns={"publishedAt": "published"})
# Infer datetime
df["published"] = pd.to_datetime(df["published"],
infer_datetime_format=True)
df["published"] = pd.to_datetime(df["published"], infer_datetime_format=True)
# Assuming timedelta of 5 hr based on what comparison between CNN and API.
df["published"] = df["published"] - pd.Timedelta("5 hours")

Expand Down

0 comments on commit 76b07da

Please sign in to comment.