Refactor: new get_hit_count function
This commit is contained in:
parent
98ea589c58
commit
113dfd3630
17
web_view.py
17
web_view.py
|
@ -84,24 +84,25 @@ def article_url(title: str) -> str:
|
|||
return flask.url_for("article_page", url_title=title.replace(" ", "_"))
|
||||
|
||||
|
||||
def get_hit_count(q: str) -> int:
|
||||
"""Search Wikipedia and return hit count."""
|
||||
return typing.cast(int, run_search(q, limit=0)["searchinfo"]["totalhits"])
|
||||
|
||||
|
||||
def search_count(q: str) -> int:
|
||||
"""How often does this article title appear in Wikipedia."""
|
||||
query = run_search(article_title_to_search_query(q), limit=0)
|
||||
return typing.cast(int, query["searchinfo"]["totalhits"]) - 1
|
||||
return get_hit_count(article_title_to_search_query(q)) - 1
|
||||
|
||||
|
||||
def search_count_with_link(q: str) -> int:
|
||||
"""How often does this article title appear in Wikipedia."""
|
||||
query = run_search(article_title_to_search_query(q) + f' linksto:"{q}"', limit=0)
|
||||
return typing.cast(int, query["searchinfo"]["totalhits"])
|
||||
"""Articles in Wikipedia that include this search term and a link."""
|
||||
return get_hit_count(article_title_to_search_query(q) + f' linksto:"{q}"')
|
||||
|
||||
|
||||
def search_no_link(q: str) -> tuple[int, list[Hit]]:
|
||||
"""Search for mentions of article title with no link included."""
|
||||
query = run_search(article_title_to_search_query(q) + f' -linksto:"{q}"', "max")
|
||||
totalhits = query["searchinfo"]["totalhits"]
|
||||
results = query["search"]
|
||||
return (totalhits, results)
|
||||
return (query["searchinfo"]["totalhits"], query["search"])
|
||||
|
||||
|
||||
@app.before_request
|
||||
|
|
Loading…
Reference in a new issue