diff --git a/Industrial_developed_hangman/src/hangman/main.py b/Industrial_developed_hangman/src/hangman/main.py index bad2a4470d0..b2a7e780ac3 100644 --- a/Industrial_developed_hangman/src/hangman/main.py +++ b/Industrial_developed_hangman/src/hangman/main.py @@ -1,3 +1,4 @@ +import json import random import time from enum import Enum @@ -5,7 +6,6 @@ from typing import Callable, List import requests -from bs4 import BeautifulSoup from colorama import Fore, Style DEBUG = False @@ -59,7 +59,7 @@ def parse_word_from_local(choice_function: Callable[[List[str]], str] = random.c raise FileNotFoundError('File local_words.txt was not found') -def parse_word_from_site(url: str = 'https://randomword.com') -> str: +def parse_word_from_site(url: str = 'https://random-word-api.herokuapp.com/word') -> str: # noqa: DAR201 """ Parse word from website. @@ -70,12 +70,11 @@ def parse_word_from_site(url: str = 'https://randomword.com') -> str: :raises RuntimeError: something go wrong with getting the word from site. """ try: - page: requests.Response = requests.get(url, timeout=request_timeout) + response: requests.Response = requests.get(url, timeout=request_timeout) except ConnectionError: raise ConnectionError('There is no connection to the internet') - if page.status_code == success_code: - soup = BeautifulSoup(page.text, 'html.parser') - return soup.find('div', id='random_word').text + if response.status_code == success_code: + return json.loads(response.content.decode())[0] raise RuntimeError('Something go wrong with getting the word from site') diff --git a/Industrial_developed_hangman/tests/test_hangman/test_main.py b/Industrial_developed_hangman/tests/test_hangman/test_main.py index 052c1960aab..46d0b1d6f0e 100644 --- a/Industrial_developed_hangman/tests/test_hangman/test_main.py +++ b/Industrial_developed_hangman/tests/test_hangman/test_main.py @@ -56,7 +56,7 @@ def test_parse_word_from_site() -> None: def test_parse_word_from_site_no_internet() -> None: with requests_mock.Mocker() as mock: - mock.get('https://randomword.com', text='
some text
') + mock.get('https://random-word-api.herokuapp.com/word', text='["some text"]') assert parse_word_from_site() == 'some text'