-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom translation pickers to be supplied
- Loading branch information
1 parent
efaf1ab
commit 1c776d2
Showing
6 changed files
with
61 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from ._extractor import PickerListType # noqa: F401 | ||
from ._extractor import extract_translations # noqa: F401 | ||
from ._pickers import LangPicker # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,49 @@ | ||
import logging | ||
from typing import Optional | ||
from typing import List, Optional, Type | ||
|
||
import pandas as pd | ||
from bs4 import BeautifulSoup | ||
|
||
import extractor.parse.translations._pickers as pickers | ||
|
||
RESOLVERS = [pickers.Polylang, pickers.GenericLangSwitcher] | ||
PICKERS = [pickers.Polylang, pickers.GenericLangSwitcher] | ||
PickerListType = List[Type[pickers.LangPicker]] | ||
|
||
PageTranslationData = pd.Series | ||
|
||
|
||
def extract_translations( | ||
page_doc: Optional[BeautifulSoup], link: str | ||
page_doc: Optional[BeautifulSoup], | ||
link: str, | ||
translation_pickers: Optional[PickerListType], | ||
) -> PageTranslationData: | ||
"""Get a list of URLs linked as translations. | ||
Args: | ||
page_doc: The full scrape document | ||
link: The link to the post | ||
translation_pickers: A list of translation pickers to use | ||
Returns: | ||
The doc's language and list of translation links | ||
""" | ||
if translation_pickers is None: | ||
translation_pickers = PICKERS | ||
|
||
if page_doc is None: | ||
return pd.Series([None, []]) | ||
for resolver_class in RESOLVERS: | ||
resolver = resolver_class(page_doc) | ||
for picker_class in translation_pickers: | ||
picker = picker_class(page_doc) | ||
|
||
if not resolver.matches(): | ||
if not picker.matches(): | ||
continue | ||
|
||
resolver.extract() | ||
picker.extract() | ||
|
||
return pd.Series([resolver.current_language, resolver.translations]) | ||
return pd.Series([picker.current_language, picker.translations]) | ||
|
||
logging.debug(f'No resolvers matched "{link}", unable to extract translations.') | ||
logging.debug( | ||
f'No translation pickers matched "{link}", unable to extract translations.' | ||
) | ||
|
||
return pd.Series([None, []]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters