Clean up search engine

Notable changes:
1. Prevent excessive engine module imports.
2. Replace trivial usage of `join()`.
3. Keep the output text sorted whenever possible.
4. Close handles properly.
5. Print error to stderr, not stdout.
6. Report search job exit code.
7. Print exception message to stderr if exception was thrown when
   running a search job.
8. Utilize XML library to build XML data
   And use 2 spaces as indentation.

PR #21098.
This commit is contained in:
Chocobo1
2024-07-22 16:51:57 +08:00
committed by GitHub
parent 3c5baac150
commit 69a829dfb0
4 changed files with 168 additions and 198 deletions

View File

@@ -1,4 +1,4 @@
#VERSION: 1.50
#VERSION: 1.51
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
@@ -25,21 +25,18 @@
# POSSIBILITY OF SUCH DAMAGE.
import re
from collections.abc import Mapping
from typing import Any, Union
from typing import TypedDict, Union
# TODO: enable the following when using Python >= 3.8
#SearchResults = TypedDict('SearchResults', {
# 'link': str,
# 'name': str,
# 'size': Union[float, int, str],
# 'seeds': int,
# 'leech': int,
# 'engine_url': str,
# 'desc_link': str, # Optional # TODO: use `NotRequired[str]` when using Python >= 3.11
# 'pub_date': int # Optional # TODO: use `NotRequired[int]` when using Python >= 3.11
#})
SearchResults = Mapping[str, Any]
SearchResults = TypedDict('SearchResults', {
'link': str,
'name': str,
'size': Union[float, int, str], # TODO: use `float | int | str` when using Python >= 3.10
'seeds': int,
'leech': int,
'engine_url': str,
'desc_link': str, # Optional # TODO: use `NotRequired[str]` when using Python >= 3.11
'pub_date': int # Optional # TODO: use `NotRequired[int]` when using Python >= 3.11
})
def prettyPrinter(dictionary: SearchResults) -> None:
@@ -62,6 +59,7 @@ def prettyPrinter(dictionary: SearchResults) -> None:
sizeUnitRegex: re.Pattern[str] = re.compile(r"^(?P<size>\d*\.?\d+) *(?P<unit>[a-z]+)?", re.IGNORECASE)
# TODO: use `float | int | str` when using Python >= 3.10
def anySizeToBytes(size_string: Union[float, int, str]) -> int:
"""
Convert a string like '1 KB' to '1024' (bytes)