Skip to content

Commit 7e64495

Browse files
Copilotbact
andcommitted
Use standard browser User-Agent string for HTTP requests
Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent d4a4774 commit 7e64495

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

pythainlp/corpus/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
from pythainlp.tools import get_full_data_path
1616

1717
_CHECK_MODE = os.getenv("PYTHAINLP_READ_MODE")
18+
_USER_AGENT = (
19+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
20+
"(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
21+
)
1822

1923

2024
class _ResponseWrapper:
@@ -43,7 +47,7 @@ def get_corpus_db(url: str):
4347

4448
corpus_db = None
4549
try:
46-
req = Request(url, headers={"User-Agent": "PyThaiNLP"})
50+
req = Request(url, headers={"User-Agent": _USER_AGENT})
4751
with urlopen(req, timeout=10) as response:
4852
corpus_db = _ResponseWrapper(response)
4953
except HTTPError as http_err:
@@ -307,7 +311,7 @@ def _download(url: str, dst: str) -> int:
307311

308312
from urllib.request import Request, urlopen
309313

310-
req = Request(url, headers={"User-Agent": "PyThaiNLP"})
314+
req = Request(url, headers={"User-Agent": _USER_AGENT})
311315
with urlopen(req, timeout=10) as response:
312316
file_size = int(response.info().get("Content-Length", -1))
313317
with open(get_full_data_path(dst), "wb") as f:

0 commit comments

Comments
 (0)