Compare commits
3 commits
dd9b7be198
...
113dfd3630
Author | SHA1 | Date | |
---|---|---|---|
Edward Betts | 113dfd3630 | ||
Edward Betts | 98ea589c58 | ||
Edward Betts | d76c74395b |
|
@ -4,7 +4,7 @@ import typing
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from . import wikidata_oauth
|
from . import mediawiki_oauth
|
||||||
|
|
||||||
wiki_hostname = "en.wikipedia.org"
|
wiki_hostname = "en.wikipedia.org"
|
||||||
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
|
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
|
||||||
|
@ -29,7 +29,7 @@ def parse_page(enwiki: str) -> dict[str, Any]:
|
||||||
|
|
||||||
def call(params: dict[str, str | int]) -> dict[str, typing.Any]:
|
def call(params: dict[str, str | int]) -> dict[str, typing.Any]:
|
||||||
"""Make GET request to mediawiki API."""
|
"""Make GET request to mediawiki API."""
|
||||||
data = wikidata_oauth.api_post_request(params)
|
data = mediawiki_oauth.api_post_request(params)
|
||||||
return cast(dict[str, Any], data.json())
|
return cast(dict[str, Any], data.json())
|
||||||
|
|
||||||
|
|
||||||
|
|
39
web_view.py
39
web_view.py
|
@ -84,24 +84,25 @@ def article_url(title: str) -> str:
|
||||||
return flask.url_for("article_page", url_title=title.replace(" ", "_"))
|
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:
|
def search_count(q: str) -> int:
|
||||||
"""How often does this article title appear in Wikipedia."""
|
"""How often does this article title appear in Wikipedia."""
|
||||||
query = run_search(article_title_to_search_query(q), limit=0)
|
return get_hit_count(article_title_to_search_query(q)) - 1
|
||||||
return typing.cast(int, query["searchinfo"]["totalhits"]) - 1
|
|
||||||
|
|
||||||
|
|
||||||
def search_count_with_link(q: str) -> int:
|
def search_count_with_link(q: str) -> int:
|
||||||
"""How often does this article title appear in Wikipedia."""
|
"""Articles in Wikipedia that include this search term and a link."""
|
||||||
query = run_search(article_title_to_search_query(q) + f' linksto:"{q}"', limit=0)
|
return get_hit_count(article_title_to_search_query(q) + f' linksto:"{q}"')
|
||||||
return typing.cast(int, query["searchinfo"]["totalhits"])
|
|
||||||
|
|
||||||
|
|
||||||
def search_no_link(q: str) -> tuple[int, list[Hit]]:
|
def search_no_link(q: str) -> tuple[int, list[Hit]]:
|
||||||
"""Search for mentions of article title with no link included."""
|
"""Search for mentions of article title with no link included."""
|
||||||
query = run_search(article_title_to_search_query(q) + f' -linksto:"{q}"', "max")
|
query = run_search(article_title_to_search_query(q) + f' -linksto:"{q}"', "max")
|
||||||
totalhits = query["searchinfo"]["totalhits"]
|
return (query["searchinfo"]["totalhits"], query["search"])
|
||||||
results = query["search"]
|
|
||||||
return (totalhits, results)
|
|
||||||
|
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
|
@ -132,7 +133,27 @@ def index() -> str | Response:
|
||||||
|
|
||||||
|
|
||||||
def case_flip(s: str) -> str:
|
def case_flip(s: str) -> str:
|
||||||
"""Switch case of character."""
|
"""
|
||||||
|
Switch the case of a single character.
|
||||||
|
|
||||||
|
If the character is lowercase, it is converted to uppercase. If it is uppercase,
|
||||||
|
it is converted to lowercase. Non-alphabetic characters remain unchanged.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
s (str): A single character string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The character with its case flipped, or the original character if it's
|
||||||
|
not a letter.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> case_flip('a')
|
||||||
|
'A'
|
||||||
|
>>> case_flip('A')
|
||||||
|
'a'
|
||||||
|
>>> case_flip('1')
|
||||||
|
'1'
|
||||||
|
"""
|
||||||
if s.islower():
|
if s.islower():
|
||||||
return s.upper()
|
return s.upper()
|
||||||
if s.isupper():
|
if s.isupper():
|
||||||
|
|
Loading…
Reference in a new issue