-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework.py
More file actions
143 lines (119 loc) · 4.19 KB
/
homework.py
File metadata and controls
143 lines (119 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import requests
import time
import logging
import sys
from http import HTTPStatus
from dotenv import load_dotenv
from telegram import Bot
from telegram.error import TelegramError
from exceptions import APIStatusIsNotOKError
load_dotenv()
PRACTICUM_TOKEN = os.getenv('PRACTICUM_TOKEN')
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
RETRY_TIME = 600
ENDPOINT = 'https://practicum.yandex.ru/api/user_api/homework_statuses/'
HEADERS = {'Authorization': f'OAuth {PRACTICUM_TOKEN}'}
HOMEWORK_STATUSES = {
'approved': 'Homework is checked: everithing is fine. Hooray!',
'reviewing': 'Homework submitted for the review.',
'rejected': 'Homework is checked: the reviewer has comments.'
}
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s'
)
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
def send_message(bot, message):
"""Sending message by bot."""
try:
bot.send_message(
chat_id=TELEGRAM_CHAT_ID,
text=message
)
logging.info(f'Bot sent message {message}')
except TelegramError:
logging.error('Error while sending the message')
def get_api_answer(current_timestamp):
"""Sending a request to the endpoint of Practicum.Homework API."""
timestamp = current_timestamp or int(time.time())
params = {'from_date': timestamp}
response = requests.get(ENDPOINT, headers=HEADERS, params=params)
if response.status_code != HTTPStatus.OK:
raise APIStatusIsNotOKError('Error while accessing the API')
return response.json()
def check_response(response):
"""API response check for correctness."""
if isinstance(response, dict) & (len(response) == 2):
homeworks = response.get('homeworks')
if isinstance(homeworks, list):
return homeworks
raise TypeError('Incorrect API response is received')
def parse_status(homework):
"""Getting the specific homework status."""
homework_name = homework.get('homework_name')
homework_status = homework.get('status')
for var_name, var in {
'homework_name': homework_name,
'homework_status': homework_status
}.items():
if not var:
message = f'{var_name} not sent'
logging.error(message)
raise KeyError(message)
verdict = HOMEWORK_STATUSES.get(homework_status)
if verdict:
return (
f'Check status change for homework "{homework_name}". {verdict}'
)
message = 'Unknown homework status'
logging.error(message)
raise ValueError(message)
def check_tokens():
"""Checking the availability of required environment variables."""
required_variables = {
'PRACTICUM_TOKEN': PRACTICUM_TOKEN,
'TELEGRAM_TOKEN': TELEGRAM_TOKEN,
'TELEGRAM_CHAT_ID': TELEGRAM_CHAT_ID,
}
for var_name, variable in required_variables.items():
if not variable:
logging.critical(
f'Required environment variable is not found: {var_name}. '
'The program is forcibly stopped.'
)
return False
return True
def main():
"""The main logic of the bot."""
if not check_tokens():
sys.exit(
'Required environment variable is not found. The program is '
'forcibly stopped.'
)
bot = Bot(token=TELEGRAM_TOKEN)
current_timestamp = int(time.time())
while True:
try:
response = get_api_answer(current_timestamp)
homeworks = check_response(response)
if homeworks:
message = parse_status(homeworks[0])
send_message(bot, message)
else:
logging.debug('No new statuses')
current_timestamp = response.get(
'current_date',
current_timestamp
)
time.sleep(RETRY_TIME)
except Exception as error:
message = f'Error while program operation: {error}'
logging.error(message)
send_message(bot, message)
time.sleep(RETRY_TIME)
if __name__ == '__main__':
main()