Trim dependencies, improve command-line output
This commit is contained in:
parent
829dc5dcce
commit
b924dd8a0b
|
@ -1,12 +1,11 @@
|
|||
import csv
|
||||
import functools
|
||||
import time
|
||||
from typing import List
|
||||
from datetime import datetime
|
||||
from typing import List, Tuple
|
||||
|
||||
import cachetools.func
|
||||
import httpx
|
||||
import hyperlink
|
||||
import pendulum
|
||||
import typer
|
||||
from pydantic import BaseModel, BaseSettings
|
||||
|
||||
|
@ -26,7 +25,7 @@ config = AppConfig()
|
|||
|
||||
|
||||
@cachetools.func.ttl_cache(ttl=config.poll)
|
||||
def get_data(base_url: str):
|
||||
def get_data(base_url: str) -> Tuple[str]:
|
||||
with httpx.Client() as client:
|
||||
resp = client.get(base_url)
|
||||
resp.raise_for_status()
|
||||
|
@ -38,22 +37,26 @@ class FedSite(BaseModel):
|
|||
page_title: str
|
||||
active_visitors: int
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return hyperlink.parse(self.page)
|
||||
|
||||
class Config:
|
||||
frozen = True
|
||||
|
||||
|
||||
@functools.lru_cache()
|
||||
def parse_data(data: List[str]):
|
||||
def parse_data(data: Tuple[str]) -> Tuple[FedSite]:
|
||||
return tuple(FedSite.parse_obj(line) for line in csv.DictReader(data))
|
||||
|
||||
|
||||
@functools.lru_cache()
|
||||
def analyze_data(data: List[FedSite]):
|
||||
return data[0]
|
||||
def analyze_data(data: Tuple[FedSite]) -> Tuple[FedSite]:
|
||||
usps = {x for x in data if "tools.usps.com" in x.page}
|
||||
usps_visitors = max(x.active_visitors for x in usps)
|
||||
hot = tuple(
|
||||
sorted(
|
||||
(x for x in data if x.active_visitors >= usps_visitors and x not in usps),
|
||||
key=lambda x: x.active_visitors,
|
||||
)
|
||||
)
|
||||
return hot
|
||||
|
||||
|
||||
@app.command()
|
||||
|
@ -64,6 +67,7 @@ def main():
|
|||
data = parse_data(data)
|
||||
summary = analyze_data(data)
|
||||
if summary != prev:
|
||||
typer.echo(f"{pendulum.now()}: {summary}")
|
||||
if summary:
|
||||
typer.echo(f"{datetime.now().isoformat()}: {summary}")
|
||||
prev = summary
|
||||
time.sleep(config.poll)
|
||||
|
|
|
@ -10,8 +10,6 @@ typer = {extras = ["all"], version = "^0.4.0"}
|
|||
cachetools = "^5.0.0"
|
||||
httpx = "^0.21.3"
|
||||
pydantic = "^1.9.0"
|
||||
hyperlink = "^21.0.0"
|
||||
pendulum = "^2.1.2"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
pytest = "^5.2"
|
||||
|
|
Loading…
Reference in New Issue