Skip to content

Commit

Permalink
Industrial_developed_hangman changed to api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
DiodDan committed Nov 3, 2023
1 parent 1c04b3a commit c5997d4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions Industrial_developed_hangman/src/hangman/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import random
import time
from enum import Enum
from pathlib import Path
from typing import Callable, List

import requests
from bs4 import BeautifulSoup
from colorama import Fore, Style

DEBUG = False
Expand Down Expand Up @@ -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.
Expand All @@ -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')


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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='<div id="random_word">some text</div>')
mock.get('https://random-word-api.herokuapp.com/word', text='["some text"]')
assert parse_word_from_site() == 'some text'


Expand Down

0 comments on commit c5997d4

Please sign in to comment.