sok.server.search
The search of the rows of S.O.K Server, and what the tabs show of them.
The batch engine of the desktop app (BatchMatcher) searches each row, as the batch pages of the desktop app do: a movie, a track, a book or a game by its file name, a series by its title, an album by its tags or its folder. A series that is Ready, or picked by hand, gets its details and the titles of its episodes, which name the episodes, and the folder it already has in a TV destination (refresh_folders): one folder found is used as it is, several wait for a choice (rows.folder_pending). A missing key or a used quota stops the search (SearchStopped); the rows keep what they had.
InvalidChange
class InvalidChange(code: str)Bases: ValueError
A change of a row that cannot be: invalid_selection, invalid_destination, invalid_folder.
Parameters
| Name | Type | Description |
|---|---|---|
code | str |
Attributes
| Name |
|---|
code |
Searchdataclass
class Search( content_type: ContentType, media_type: MediaType, parse: QueryParser, factory_type: str, same_work: Optional[SameWork] = None, localized: bool = True,)How the rows of one kind are searched and named.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
content_type | ContentType | ||
media_type | MediaType | ||
parse | QueryParser | ||
factory_type | str | ||
same_work | Optional[SameWork] | None | |
localized | bool | True |
Attributes
| Name | Type |
|---|---|
content_type | ContentType |
factory_type | str |
localized | bool |
media_type | MediaType |
parse | QueryParser |
same_work | Optional[SameWork] |
SearchStopped
class SearchStopped(code: str, service: str)Bases: Exception
A search that cannot go on: a missing or refused key, a used quota.
After a search of several kinds, others lists the services that stopped after this one.
Parameters
| Name | Type | Description |
|---|---|---|
code | str | |
service | str |
Attributes
| Name | Type |
|---|---|
code | |
others | List[SearchStopped] |
service |
Searcher
class Searcher(db: Database, config: ConfigManager, settings: Settings)Search the rows, change them, and describe them for the tabs.
Parameters
| Name | Type | Description |
|---|---|---|
db | Database | |
config | ConfigManager | |
settings | Settings |
batch_ops
def batch_ops(media_type: str) -> Any:The file operations that rename a media type.
Parameters
| Name | Type | Description |
|---|---|---|
media_type | str |
changeasync
async def change( row_id: int, selected: Optional[int] = None, destination: Optional[int] = None, destinations: int = 1, folder: Optional[Union[int, str]] = None, track_language: Optional[Dict[str, Any]] = None, languages_forced: Optional[bool] = None,) -> None:Pick a candidate (the row is then Ready), a destination folder, the folder of a series that has several, or the language of a track.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
row_id | int | The row. | |
selected | Optional[int] | None | Index of the candidate picked. |
destination | Optional[int] | None | Index of the destination folder of its media type. |
destinations | int | 1 | How many destination folders the media type has. |
folder | Optional[Union[int, str]] | None | Index of the existing folder of the series picked, or NEW_FOLDER for a new one. |
track_language | Optional[Dict[str, Any]] | None | {"kind", "position", "language"}: the language of a track of its videos (None: back to automatic), for all its files. |
languages_forced | Optional[bool] | None | True sorts the row without the languages it waits for (unsure_languages); False makes it wait again. |
Raises
UnknownRowNo row has this id.InvalidChangeinvalid_selection, invalid_destination, invalid_folder or invalid_track_language.
chosen_folderstaticmethod
def chosen_folder(row: Any) -> Optional[Dict[str, Any]]:The existing folder a row's series goes into, or None for a new one (also when another destination was picked since).
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
describe
def describe(row: Any) -> Dict[str, Any]:A row with its candidates and, once matched, the new names.
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
destination
def destination(row: Any) -> Optional[str]:The destination folder of a row, if its media type has one.
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
known_folders
def known_folders(ids: Dict[str, str]) -> List[str]:The folders S.O.K put a series in before, from its ids.
Parameters
| Name | Type | Description |
|---|---|---|
ids | Dict[str, str] |
media_item
def media_item(row: Any) -> Any:The media item of the candidate picked for a row, or None before a match.
A series gets the titles of its episodes and the folders of its seasons ("Season 2 - The Name", in the language of the settings).
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
movie_detailsasync
async def movie_details(row: Any) -> Optional[Dict[str, Any]]:The details of the movie picked for a row, with its credits: the director and the genres a rename writes into the file (write_metadata). None when they fail: the file then gets what the search result gives.
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
new_names
def new_names( media_type: str, item: Any, files: List[str], destination: Optional[str],) -> List[str]:The new path of each file of a row under its destination.
A movie gets its file name, an episode its series and season folders (an existing season folder of the destination first), the other kinds the folders and the name the desktop batches give them (batch_target): the artist and album folder of a track, the author folder of a book, the platform folder of a game. A video converted to MKV (convert_to_mkv) gets .mkv.
Parameters
| Name | Type | Description |
|---|---|---|
media_type | str | |
item | Any | |
files | List[str] | |
destination | Optional[str] |
refresh_folders
def refresh_folders(row_ids: Optional[Iterable[int]] = None) -> None:Look again for the folder each series already has.
One folder found is chosen (and its destination with it); several wait for a choice. A choice made by hand stays while the same folders are found.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
row_ids | Optional[Iterable[int]] | None | These rows only; every episode row when None. |
refresh_languages
def refresh_languages( row_ids: Optional[Iterable[int]] = None, release: bool = False,) -> None:Say again which video rows wait for the languages of their tracks (language_pending, the unsure_languages setting): after an analysis, a choice, a search, or a change of the settings.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
row_ids | Optional[Iterable[int]] | None | These rows only; every video row when None. |
release | bool | False | A change of the user (the settings): a row that waited and no longer does is released for the automatic sorting. |
remember_folder
def remember_folder(row: Any, folder: str, date: str) -> None:Keep the folder a series was put in, for its next episodes.
Parameters
| Name | Type | Description |
|---|---|---|
row | Any | |
folder | str | |
date | str |
row
def row(row_id: int) -> Dict[str, Any]:Parameters
| Name | Type | Description |
|---|---|---|
row_id | int |
rows
def rows(media_type: str) -> List[Dict[str, Any]]:The rows of a tab, as the interface shows them.
Parameters
| Name | Type | Description |
|---|---|---|
media_type | str |
search_pendingasync
async def search_pending( progress: Optional[Callable[[int, int], None]] = None,) -> int:Search every row not searched yet, kind by kind.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
progress | Optional[Callable[[int, int], None]] | None | Called with (done, total) after each row. |
Returns
intThe number of rows searched.
Raises
SearchStoppedA key is missing or refused, or the quota is used. The kinds of the other services are searched first: a used quota of Google Books does not keep the games from their search.
search_rowasync
async def search_row(row_id: int, query: Optional[str] = None) -> None:Search one row again, with the text typed or its own text.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
row_id | int | ||
query | Optional[str] | None |
Raises
UnknownRowNo row has this id.SearchStoppedA key is missing or refused, or the quota is used.
UnknownRow
class UnknownRowBases: LookupError
No row has this id.
SourceFunctions
api_language
def api_language(language: str) -> str:The language of the results: "fr" → "fr-FR", as the desktop app asks.
Parameters
| Name | Type | Description |
|---|---|---|
language | str |
poster_of
def poster_of(row: Any) -> Optional[str]:The poster of the result picked for a row, if it has one.
Parameters
| Name | Type | Description |
|---|---|---|
row | Any |
search_error
def search_error(exc: SearchStopped) -> Dict[str, Any]:{"code", "service"}, and "others" when other services stopped too.
Parameters
| Name | Type | Description |
|---|---|---|
exc | SearchStopped |
service_id
def service_id(api_name: str) -> str:The id of a service from the name of its API class: GoogleBooks → google_books.
Parameters
| Name | Type | Description |
|---|---|---|
api_name | str |
Attributes
| Name | Type | Value |
|---|---|---|
KEY_REFUSED | 'key_refused' | |
MISSING_KEY | 'missing_key' | |
NEW_FOLDER | 'new' | |
OVERVIEW_LENGTH | 300 | |
PREFERRED_API | {MediaType.VIDEO: ('preferred_api_video', 'tmdb'), MediaType.MUSIC: ('preferred_api_music', 'deezer'), MediaType.BOOK: ('preferred_api_books', 'google_books'), MediaType.GAME: ('preferred_api_games', 'igdb')} | |
RATE_LIMITED | 'rate_limited' | |
SEARCHES | Dict[str, Search] | {'movie': Search(ContentType.MOVIE, MediaType.VIDEO, parse_movie, 'movie'), 'episode': Search(ContentType.TV_SERIES, MediaType.VIDEO, parse_series, 'tv'), 'album': Search(ContentType.ALBUM, MediaType.MUSIC, parse_album, 'album', same_album, False), 'track': Search(ContentType.TRACK, MediaType.MUSIC, parse_track, 'track', same_track, False), 'book': Search(ContentType.BOOK, MediaType.BOOK, parse_book, 'book', same_book, False), 'game': Search(ContentType.GAME, MediaType.GAME, parse_game, 'game')} |
logger | logging.getLogger(__name__) |