apply Black v23.1.0 formatting changes
This commit is contained in:
parent
8e1988eea2
commit
8a8bfce89d
7 changed files with 3 additions and 35 deletions
|
|
@ -119,7 +119,6 @@ async def apply_db_patches(db: Database):
|
||||||
raise RuntimeError("No statement found.")
|
raise RuntimeError("No statement found.")
|
||||||
|
|
||||||
async with db.transaction():
|
async with db.transaction():
|
||||||
|
|
||||||
for query in queries:
|
for query in queries:
|
||||||
await db.execute(query)
|
await db.execute(query)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,12 +35,10 @@ log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def refresh_user_ratings_from_imdb(stop_on_dupe=True):
|
async def refresh_user_ratings_from_imdb(stop_on_dupe=True):
|
||||||
|
|
||||||
with session() as s:
|
with session() as s:
|
||||||
s.headers["Accept-Language"] = "en-US, en;q=0.5"
|
s.headers["Accept-Language"] = "en-US, en;q=0.5"
|
||||||
|
|
||||||
for user in await db.get_all(User):
|
for user in await db.get_all(User):
|
||||||
|
|
||||||
log.info("⚡️ Loading data for %s ...", user.name)
|
log.info("⚡️ Loading data for %s ...", user.name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -97,7 +95,6 @@ find_movie_id = re.compile(r"/title/(?P<id>tt\d+)/").search
|
||||||
|
|
||||||
|
|
||||||
def movie_and_rating_from_item(item) -> tuple[Movie, Rating]:
|
def movie_and_rating_from_item(item) -> tuple[Movie, Rating]:
|
||||||
|
|
||||||
genres = (genre := item.find("span", "genre")) and genre.string or ""
|
genres = (genre := item.find("span", "genre")) and genre.string or ""
|
||||||
movie = Movie(
|
movie = Movie(
|
||||||
title=item.h3.a.string.strip(),
|
title=item.h3.a.string.strip(),
|
||||||
|
|
@ -169,7 +166,6 @@ async def parse_page(url) -> tuple[list[Rating], str | None]:
|
||||||
|
|
||||||
items = soup.find_all("div", "lister-item-content")
|
items = soup.find_all("div", "lister-item-content")
|
||||||
for i, item in enumerate(items):
|
for i, item in enumerate(items):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
movie, rating = movie_and_rating_from_item(item)
|
movie, rating = movie_and_rating_from_item(item)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
|
@ -199,7 +195,6 @@ async def load_ratings(user_id):
|
||||||
next_url = user_ratings_url(user_id)
|
next_url = user_ratings_url(user_id)
|
||||||
|
|
||||||
while next_url:
|
while next_url:
|
||||||
|
|
||||||
ratings, next_url = await parse_page(next_url)
|
ratings, next_url = await parse_page(next_url)
|
||||||
|
|
||||||
for i, rating in enumerate(ratings):
|
for i, rating in enumerate(ratings):
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,6 @@ def count_lines(path: Path) -> int:
|
||||||
buf_size = 8 * one_mb # 8 MiB seems to give a good read/process performance.
|
buf_size = 8 * one_mb # 8 MiB seems to give a good read/process performance.
|
||||||
|
|
||||||
with gzip.open(path, "rt") as f:
|
with gzip.open(path, "rt") as f:
|
||||||
|
|
||||||
while buf := f.read(buf_size):
|
while buf := f.read(buf_size):
|
||||||
i += buf.count("\n")
|
i += buf.count("\n")
|
||||||
|
|
||||||
|
|
@ -138,7 +137,6 @@ def read_imdb_tsv(
|
||||||
|
|
||||||
def read_imdb_tsv(path: Path, row_type, *, unpack=True):
|
def read_imdb_tsv(path: Path, row_type, *, unpack=True):
|
||||||
with gzip.open(path, "rt", newline="") as f:
|
with gzip.open(path, "rt", newline="") as f:
|
||||||
|
|
||||||
rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
||||||
|
|
||||||
# skip header line
|
# skip header line
|
||||||
|
|
@ -205,7 +203,6 @@ async def import_from_file(*, basics_path: Path, ratings_path: Path):
|
||||||
chunk = []
|
chunk = []
|
||||||
|
|
||||||
for i, m in enumerate(read_basics(basics_path)):
|
for i, m in enumerate(read_basics(basics_path)):
|
||||||
|
|
||||||
perc = 100 * i / total
|
perc = 100 * i / total
|
||||||
if perc >= perc_next_report:
|
if perc >= perc_next_report:
|
||||||
await db.set_import_progress(perc)
|
await db.set_import_progress(perc)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,6 @@ def fields(class_or_instance):
|
||||||
# XXX this might be a little slow (not sure), if so, memoize
|
# XXX this might be a little slow (not sure), if so, memoize
|
||||||
|
|
||||||
for f in _fields(class_or_instance):
|
for f in _fields(class_or_instance):
|
||||||
|
|
||||||
if f.name == "_is_lazy":
|
if f.name == "_is_lazy":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -108,7 +107,6 @@ def asplain(
|
||||||
|
|
||||||
d: JSONObject = {}
|
d: JSONObject = {}
|
||||||
for f in fields(o):
|
for f in fields(o):
|
||||||
|
|
||||||
if filter_fields is not None and f.name not in filter_fields:
|
if filter_fields is not None and f.name not in filter_fields:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -156,7 +154,6 @@ def fromplain(cls: Type[T], d: dict[str, Any], *, serialized: bool = False) -> T
|
||||||
|
|
||||||
dd: JSONObject = {}
|
dd: JSONObject = {}
|
||||||
for f in fields(cls):
|
for f in fields(cls):
|
||||||
|
|
||||||
target = f.type
|
target = f.type
|
||||||
otype = optional_type(f.type)
|
otype = optional_type(f.type)
|
||||||
is_opt = otype is not None
|
is_opt = otype is not None
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,6 @@ def Session() -> requests.Session:
|
||||||
def throttle(
|
def throttle(
|
||||||
times: int, per_seconds: float, jitter: Callable[[], float] | None = None
|
times: int, per_seconds: float, jitter: Callable[[], float] | None = None
|
||||||
) -> Callable[[Callable], Callable]:
|
) -> Callable[[Callable], Callable]:
|
||||||
|
|
||||||
calls: deque[float] = deque(maxlen=times)
|
calls: deque[float] = deque(maxlen=times)
|
||||||
|
|
||||||
if jitter is None:
|
if jitter is None:
|
||||||
|
|
@ -86,7 +85,6 @@ def throttle(
|
||||||
def decorator(func: Callable) -> Callable:
|
def decorator(func: Callable) -> Callable:
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def inner(*args, **kwds):
|
def inner(*args, **kwds):
|
||||||
|
|
||||||
# clean up
|
# clean up
|
||||||
while calls:
|
while calls:
|
||||||
if calls[0] + per_seconds > time():
|
if calls[0] + per_seconds > time():
|
||||||
|
|
@ -151,7 +149,6 @@ def cache_path(req) -> Path | None:
|
||||||
|
|
||||||
@throttle(1, 1, random)
|
@throttle(1, 1, random)
|
||||||
def http_get(s: requests.Session, url: str, *args, **kwds) -> requests.Response:
|
def http_get(s: requests.Session, url: str, *args, **kwds) -> requests.Response:
|
||||||
|
|
||||||
req = s.prepare_request(requests.Request("GET", url, *args, **kwds))
|
req = s.prepare_request(requests.Request("GET", url, *args, **kwds))
|
||||||
|
|
||||||
cachefile = cache_path(req) if config.debug else None
|
cachefile = cache_path(req) if config.debug else None
|
||||||
|
|
@ -244,7 +241,6 @@ def download(
|
||||||
raise FileExistsError(23, "Would replace existing file", str(file_path))
|
raise FileExistsError(23, "Would replace existing file", str(file_path))
|
||||||
|
|
||||||
with session() as s:
|
with session() as s:
|
||||||
|
|
||||||
headers = {}
|
headers = {}
|
||||||
if file_exists and only_if_newer:
|
if file_exists and only_if_newer:
|
||||||
assert file_path
|
assert file_path
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,6 @@ route.registered = _routes
|
||||||
|
|
||||||
@route("/groups/{group_id}/ratings")
|
@route("/groups/{group_id}/ratings")
|
||||||
async def get_ratings_for_group(request):
|
async def get_ratings_for_group(request):
|
||||||
|
|
||||||
group_id = as_ulid(request.path_params["group_id"])
|
group_id = as_ulid(request.path_params["group_id"])
|
||||||
group = await db.get(Group, id=str(group_id))
|
group = await db.get(Group, id=str(group_id))
|
||||||
|
|
||||||
|
|
@ -256,7 +255,6 @@ def not_implemented():
|
||||||
@route("/movies")
|
@route("/movies")
|
||||||
@requires(["authenticated"])
|
@requires(["authenticated"])
|
||||||
async def list_movies(request):
|
async def list_movies(request):
|
||||||
|
|
||||||
params = request.query_params
|
params = request.query_params
|
||||||
|
|
||||||
user = await auth_user(request)
|
user = await auth_user(request)
|
||||||
|
|
@ -324,7 +322,6 @@ async def list_movies(request):
|
||||||
@route("/movies", methods=["POST"])
|
@route("/movies", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def add_movie(request):
|
async def add_movie(request):
|
||||||
|
|
||||||
not_implemented()
|
not_implemented()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -366,7 +363,6 @@ _import_lock = asyncio.Lock()
|
||||||
@route("/movies/_reload_imdb", methods=["POST"])
|
@route("/movies/_reload_imdb", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def load_imdb_movies(request):
|
async def load_imdb_movies(request):
|
||||||
|
|
||||||
params = request.query_params
|
params = request.query_params
|
||||||
force = truthy(params.get("force"))
|
force = truthy(params.get("force"))
|
||||||
|
|
||||||
|
|
@ -389,7 +385,6 @@ async def load_imdb_movies(request):
|
||||||
@route("/users")
|
@route("/users")
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def list_users(request):
|
async def list_users(request):
|
||||||
|
|
||||||
users = await db.get_all(User)
|
users = await db.get_all(User)
|
||||||
|
|
||||||
return JSONResponse([asplain(u) for u in users])
|
return JSONResponse([asplain(u) for u in users])
|
||||||
|
|
@ -398,7 +393,6 @@ async def list_users(request):
|
||||||
@route("/users", methods=["POST"])
|
@route("/users", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def add_user(request):
|
async def add_user(request):
|
||||||
|
|
||||||
name, imdb_id = await json_from_body(request, ["name", "imdb_id"])
|
name, imdb_id = await json_from_body(request, ["name", "imdb_id"])
|
||||||
|
|
||||||
# XXX restrict name
|
# XXX restrict name
|
||||||
|
|
@ -420,7 +414,6 @@ async def add_user(request):
|
||||||
@route("/users/{user_id}")
|
@route("/users/{user_id}")
|
||||||
@requires(["authenticated"])
|
@requires(["authenticated"])
|
||||||
async def show_user(request):
|
async def show_user(request):
|
||||||
|
|
||||||
user_id = as_ulid(request.path_params["user_id"])
|
user_id = as_ulid(request.path_params["user_id"])
|
||||||
|
|
||||||
if is_admin(request):
|
if is_admin(request):
|
||||||
|
|
@ -449,7 +442,6 @@ async def show_user(request):
|
||||||
@route("/users/{user_id}", methods=["DELETE"])
|
@route("/users/{user_id}", methods=["DELETE"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def remove_user(request):
|
async def remove_user(request):
|
||||||
|
|
||||||
user_id = as_ulid(request.path_params["user_id"])
|
user_id = as_ulid(request.path_params["user_id"])
|
||||||
|
|
||||||
user = await db.get(User, id=str(user_id))
|
user = await db.get(User, id=str(user_id))
|
||||||
|
|
@ -467,7 +459,6 @@ async def remove_user(request):
|
||||||
@route("/users/{user_id}", methods=["PATCH"])
|
@route("/users/{user_id}", methods=["PATCH"])
|
||||||
@requires(["authenticated"])
|
@requires(["authenticated"])
|
||||||
async def modify_user(request):
|
async def modify_user(request):
|
||||||
|
|
||||||
user_id = as_ulid(request.path_params["user_id"])
|
user_id = as_ulid(request.path_params["user_id"])
|
||||||
|
|
||||||
if is_admin(request):
|
if is_admin(request):
|
||||||
|
|
@ -515,7 +506,6 @@ async def modify_user(request):
|
||||||
@route("/users/{user_id}/groups", methods=["POST"])
|
@route("/users/{user_id}/groups", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def add_group_to_user(request):
|
async def add_group_to_user(request):
|
||||||
|
|
||||||
user_id = as_ulid(request.path_params["user_id"])
|
user_id = as_ulid(request.path_params["user_id"])
|
||||||
|
|
||||||
user = await db.get(User, id=str(user_id))
|
user = await db.get(User, id=str(user_id))
|
||||||
|
|
@ -540,21 +530,18 @@ async def add_group_to_user(request):
|
||||||
@route("/users/{user_id}/ratings")
|
@route("/users/{user_id}/ratings")
|
||||||
@requires(["private"])
|
@requires(["private"])
|
||||||
async def ratings_for_user(request):
|
async def ratings_for_user(request):
|
||||||
|
|
||||||
not_implemented()
|
not_implemented()
|
||||||
|
|
||||||
|
|
||||||
@route("/users/{user_id}/ratings", methods=["PUT"])
|
@route("/users/{user_id}/ratings", methods=["PUT"])
|
||||||
@requires("authenticated")
|
@requires("authenticated")
|
||||||
async def set_rating_for_user(request):
|
async def set_rating_for_user(request):
|
||||||
|
|
||||||
not_implemented()
|
not_implemented()
|
||||||
|
|
||||||
|
|
||||||
@route("/users/_reload_ratings", methods=["POST"])
|
@route("/users/_reload_ratings", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def load_imdb_user_ratings(request):
|
async def load_imdb_user_ratings(request):
|
||||||
|
|
||||||
ratings = [rating async for rating in imdb.refresh_user_ratings_from_imdb()]
|
ratings = [rating async for rating in imdb.refresh_user_ratings_from_imdb()]
|
||||||
|
|
||||||
return JSONResponse({"new_ratings": [asplain(r) for r in ratings]})
|
return JSONResponse({"new_ratings": [asplain(r) for r in ratings]})
|
||||||
|
|
@ -563,7 +550,6 @@ async def load_imdb_user_ratings(request):
|
||||||
@route("/groups")
|
@route("/groups")
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def list_groups(request):
|
async def list_groups(request):
|
||||||
|
|
||||||
groups = await db.get_all(Group)
|
groups = await db.get_all(Group)
|
||||||
|
|
||||||
return JSONResponse([asplain(g) for g in groups])
|
return JSONResponse([asplain(g) for g in groups])
|
||||||
|
|
@ -572,7 +558,6 @@ async def list_groups(request):
|
||||||
@route("/groups", methods=["POST"])
|
@route("/groups", methods=["POST"])
|
||||||
@requires(["authenticated", "admin"])
|
@requires(["authenticated", "admin"])
|
||||||
async def add_group(request):
|
async def add_group(request):
|
||||||
|
|
||||||
(name,) = await json_from_body(request, ["name"])
|
(name,) = await json_from_body(request, ["name"])
|
||||||
|
|
||||||
# XXX restrict name
|
# XXX restrict name
|
||||||
|
|
@ -586,7 +571,6 @@ async def add_group(request):
|
||||||
@route("/groups/{group_id}/users", methods=["POST"])
|
@route("/groups/{group_id}/users", methods=["POST"])
|
||||||
@requires(["authenticated"])
|
@requires(["authenticated"])
|
||||||
async def add_user_to_group(request):
|
async def add_user_to_group(request):
|
||||||
|
|
||||||
group_id = as_ulid(request.path_params["group_id"])
|
group_id = as_ulid(request.path_params["group_id"])
|
||||||
group = await db.get(Group, id=str(group_id))
|
group = await db.get(Group, id=str(group_id))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue