Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/metadata_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def get_metadatas(
field: str,
strategy: "TYPES_STRATEGY" = None,
encoder: Optional["TYPE_ENCODER"] = None,
) -> Optional[Dict[str, Union[Dict, List]]]:
) -> Optional[Dict[str, List[str]]]:
"""
looks for the field in various stores. defaults to the core
strategy, though you may specify a certain item. if you search for
Expand Down Expand Up @@ -727,6 +727,17 @@ def encode(value: Union[str, dict], store:Optional[str]=None) -> str:
simplify dc elements into just the 'content' text, not dict
:type encoder:
bool

EXAMPLE:

metadataParserPage.parsed_result.get_metadatas("title",strategy=None)
>>> {'page': ['Title: Destination'],
>>> 'og': ['meta.property=og:title'],
>>> 'twitter': ['meta.name=twitter:title']}

metadataParserPage.parsed_result.get_metadatas("title",strategy=["og"])
>>> {'og': ['meta.property=og:title']}

"""
# normalize a strategy into a list of valid options.
strategy = self._coerce_validate_strategy(strategy)
Expand All @@ -737,7 +748,7 @@ def encode(value: Union[str, dict], store:Optional[str]=None) -> str:
elif encoder == "raw":
encoder = None

def _lookup(store: str) -> Optional[List]:
def _lookup(store: str) -> Optional[List[str]]:
if field in self.metadata[store]:
val = self.metadata[store][field]
if not isinstance(val, list):
Expand All @@ -748,7 +759,7 @@ def _lookup(store: str) -> Optional[List]:
return None

# returns List or None
rval: Dict = {}
rval: Dict[str, List[str]] = {}
for store in strategy:
if store in self.metadata:
val = _lookup(store)
Expand Down Expand Up @@ -1833,7 +1844,9 @@ def get_url_canonical(
candidates = [c for c in candidates if c] if candidates else []
if not candidates:
return None
canonical = candidates[0]
canonical: Optional[str] = candidates[0]
if TYPE_CHECKING:
assert canonical

# does the canonical have valid characters?
# some websites, even BIG PROFESSIONAL ONES, will put html in here.
Expand Down Expand Up @@ -1911,7 +1924,9 @@ def get_url_opengraph(
candidates = [c for c in candidates if c] if candidates else []
if not candidates:
return None
og = candidates[0]
og: Optional[str] = candidates[0]
if TYPE_CHECKING:
assert og

# does the og have valid characters?
# some websites, even BIG PROFESSIONAL ONES, will put html in here.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_document_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class _TestDocumentParsingCore:

def _MakeOne(self, filename):
"""lazy cache of files as needed"""
global CACHED_FILESYSTEM_DOCUMENTS
# global CACHED_FILESYSTEM_DOCUMENTS
if filename not in CACHED_FILESYSTEM_DOCUMENTS:
CACHED_FILESYSTEM_DOCUMENTS[filename] = open(
os.path.join(_examples_dir, filename)
Expand Down