diff --git a/setup.py b/setup.py index d31c328..d536154 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="tdk-py", - version="1.1.0.post1", + version="1.1.1", author="Emre Özcan", author_email="justsomechars@gmail.com", description="Python API for the Turkish Language Foundation", diff --git a/src/tdk/gts/__init__.py b/src/tdk/gts/__init__.py index d07daa7..4056a5e 100644 --- a/src/tdk/gts/__init__.py +++ b/src/tdk/gts/__init__.py @@ -1,5 +1,4 @@ import json -import urllib.request from typing import List from . import remote_paths @@ -7,18 +6,19 @@ from ..exceptions import TdkIdLookupErrorException, TdkIdLookupUnexpectedResponseException, \ TdkSearchUnexpectedResponseException, TdkSearchErrorException from ..models import Entry +from ..networking import make_request from ..tools import lowercase def index() -> List[str]: - with urllib.request.urlopen(url=remote_paths.autocomplete_index()) as response: + with make_request(url=remote_paths.autocomplete_index()) as response: autocomplete_index = json.loads(response.read()) return parse_index(autocomplete_index) def search(query: str) -> List[Entry]: query = lowercase(query, remove_unknown_characters=False) - with urllib.request.urlopen(url=remote_paths.general_search(query)) as response: + with make_request(url=remote_paths.general_search(query)) as response: words = json.loads(response.read()) if not isinstance(words, list): if "error" in words: @@ -31,7 +31,7 @@ def search(query: str) -> List[Entry]: def get(_id: int) -> Entry: - with urllib.request.urlopen(url=remote_paths.get_with_id(_id)) as response: + with make_request(url=remote_paths.get_with_id(_id)) as response: word = json.loads(response.read()) if not isinstance(word, list): if "error" in word: @@ -44,6 +44,6 @@ def get(_id: int) -> Entry: def suggest(query: str) -> List[str]: - with urllib.request.urlopen(url=remote_paths.suggest(query)) as response: + with make_request(url=remote_paths.suggest(query)) as response: index = json.loads(response.read()) return parse_index(index) diff --git a/src/tdk/networking.py b/src/tdk/networking.py new file mode 100644 index 0000000..d9e9a18 --- /dev/null +++ b/src/tdk/networking.py @@ -0,0 +1,23 @@ +import urllib.request + +_http_headers = { + "Accept": "application/json, text/javascript, */*; q=0.01", + "Referer": "https://sozluk.gov.tr/", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/99.0.4844.51 " + "Safari/537.36", + "X-Requested-With": "XMLHttpRequest", +} + + +def make_request(*args, **kwargs): + """ + Helper function to add default headers to a urllib request. + + All arguments are passed down to urllib.request.Request, + Default headers are added if the "headers" keyword argument is not given. + """ + if "headers" not in kwargs: + kwargs["headers"] = _http_headers + return urllib.request.urlopen(urllib.request.Request(*args, **kwargs))