Skip to content

Commit

Permalink
Fix 404 warning on s2 DOI not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dlesbre committed Dec 19, 2023
1 parent ed8dc82 commit 143be97
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
7 changes: 7 additions & 0 deletions bibtexautocomplete/APIs/semantic_scholar.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class SemanticScholarLookup(JSON_DT_Lookup):
# so an average of 1 query every 3 seconds
query_delay: float = 3.0 # = (5 * 60) / 100

def get_no_warning_codes(self) -> List[int]:
"""Ignore 404 returned on invalid DOIs"""
codes = super().get_no_warning_codes()
if self.doi is not None:
codes.append(404)
return codes

def get_base_path(self) -> str:
if self.doi is not None:
return self.path + "/DOI:" + self.doi
Expand Down
7 changes: 5 additions & 2 deletions bibtexautocomplete/bibtex/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@


def author_set(entry: BibtexEntry) -> Set[str]:
"""Returns the set of normalized author lastname from entry"""
"""Returns the set of normalized author lastname from entry
I.E. lowercase loast names, with spaces removed"""
authors = entry.author
last_names: Set[str] = set()
for author in authors:
Expand All @@ -42,7 +43,9 @@ def author_set(entry: BibtexEntry) -> Set[str]:

def common_authors(a: BibtexEntry, b: BibtexEntry) -> Tuple[int, int, int]:
"""Returns:
number of common authors, number of a authors, number of authors of b"""
- number of common authors;
- number of authors of a only,
- number of authors of b only"""
authors_a = author_set(a)
authors_b = author_set(b)
common = authors_a.intersection(authors_b)
Expand Down
25 changes: 19 additions & 6 deletions bibtexautocomplete/lookups/search_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ class SearchResultMixin(Generic[result]):
defaults to [200]
"""

# HTTP codes that indicate we (likely) got valid data
ok_codes: List[int] = [200]

# HTTP codes that shouldn't raise a warning, but indicate we got no data
# Typically 404 for websites that return 404 on unknown DOIs.
no_warning_codes: List[int] = []

def get_no_warning_codes(self) -> List[int]:
"""return a list of HTTP codes that shouldn't raise a warning, but
indicate we got no data. Typically 404 for websites that return 404 on
unknown DOIs. Override this for dynamic setting, else just change
the no_warning_codes attribute"""
return self.no_warning_codes

def get_results(self, data: bytes) -> Optional[Iterable[result]]:
"""Parse the data into a list of results to check
Return None if no results/invalid data"""
Expand All @@ -59,12 +71,13 @@ def match_score(self, entry: BibtexEntry, res: result) -> int:
def process_data(self, data: Data) -> Optional[BibtexEntry]:
"""Iterate through results until one matches"""
if data.code not in self.ok_codes:
logger.warn(
"response: {FgYellow}{status}{reason}{Reset} in {delay}s",
status=data.code,
reason=" " + data.reason if data.reason else "",
delay=data.delay,
)
if data.code not in self.get_no_warning_codes():
logger.warn(
"response: {FgYellow}{status}{reason}{Reset} in {delay}s",
status=data.code,
reason=" " + data.reason if data.reason else "",
delay=data.delay,
)
return None
results = self.get_results(data.data)
if results is None:
Expand Down

0 comments on commit 143be97

Please sign in to comment.