-
Notifications
You must be signed in to change notification settings - Fork 36
/
all_common.py
191 lines (141 loc) · 6.25 KB
/
all_common.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
import codecs
import json
import logging
import sys
import time
from logging.handlers import RotatingFileHandler
from typing import Callable, List, Union
from pathlib import Path
import requests
from all_config import API_ID, TO
# Import https://github.com/gil9red/SimplePyScripts/blob/8fa9b9c23d10b5ee7ff0161da997b463f7a861bf/wait/wait.py
# Absolute path. Analog '../wait'
sys.path.append(str(Path(__file__).resolve().parent.parent / 'wait'))
from wait import wait
def make_backslashreplace_console():
# При выводе юникодных символов в консоль винды
# Возможно, не только для винды, но и для любой платформы стоит использовать
# эту настройку -- мало какие проблемы могут встретиться
if sys.platform == 'win32':
try:
sys.stdout = codecs.getwriter(sys.stdout.encoding)(sys.stdout.detach(), 'backslashreplace')
sys.stderr = codecs.getwriter(sys.stderr.encoding)(sys.stderr.detach(), 'backslashreplace')
except AttributeError:
# ignore "AttributeError: '_io.BufferedWriter' object has no attribute 'encoding'"
pass
def get_logger(name, file='log.txt', encoding='utf-8', log_stdout=True, log_file=True) -> 'logging.Logger':
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] %(filename)s:%(lineno)d %(levelname)-8s %(message)s')
if log_file:
fh = RotatingFileHandler(file, maxBytes=10000000, backupCount=5, encoding=encoding)
fh.setFormatter(formatter)
log.addHandler(fh)
if log_stdout:
sh = logging.StreamHandler(stream=sys.stdout)
sh.setFormatter(formatter)
log.addHandler(sh)
return log
def send_sms(api_id: str, to: str, text: str, log):
api_id = api_id.strip()
to = to.strip()
if not api_id or not to:
log.warning('Параметры api_id или to не указаны, отправка СМС невозможна!')
return
log.info(f'Отправка sms: {text!r}')
if len(text) > 70:
text = text[:70-3] + '...'
log.info(f'Текст sms будет сокращено, т.к. слишком длинное (больше 70 символов): {text!r}')
# Отправляю смс на номер
url = 'https://sms.ru/sms/send?api_id={api_id}&to={to}&text={text}'.format(
api_id=api_id,
to=to,
text=text
)
log.debug(repr(url))
while True:
try:
rs = requests.get(url)
log.debug(repr(rs.text))
break
except:
log.exception("При отправке sms произошла ошибка:")
log.debug('Через 5 минут попробую снова...')
# Wait 5 minutes before next attempt
time.sleep(5 * 60)
def simple_send_sms(text: str, log=None):
# Если логгер не определен, тогда создаем свой, который логирует в консоль
if not log:
log = get_logger('all_common', log_file=False)
return send_sms(API_ID, TO, text, log)
def run_notification_job(
log__or__log_name: Union['logging.Logger', str],
file_name_items: str,
get_new_items: Callable[[], List[str]],
notified_by_sms=True,
timeout={'weeks': 1},
timeout_exception_seconds=5 * 60,
format_first_start_detected='Обнаружен первый запуск',
format_current_items='Текущий список (%s): %s',
format_get_items='Запрос списка',
format_items='Список (%s): %s',
format_new_item='Появился новый элемент "%s"',
format_no_new_items='Новых элементов нет',
format_on_exception='Ошибка:',
format_on_exception_next_attempt='Через 5 минут попробую снова...',
):
log = log__or__log_name
if isinstance(log, str):
log = get_logger(log)
def save_items(items: List[str]):
with open(file_name_items, mode='w', encoding='utf-8') as f:
json.dump(items, f, ensure_ascii=False, indent=4)
def read_items() -> List[str]:
try:
with open(file_name_items, encoding='utf-8') as f:
obj = json.load(f)
# Должен быть список, но если в файле будет что-то другое -- это будет неправильно
if not isinstance(obj, list):
return []
return obj
except:
return []
FILE_NAME_SKIP = Path('skip')
# Загрузка текущего списка из файла
current_items = read_items()
log.debug(format_current_items, len(current_items), current_items)
while True:
if FILE_NAME_SKIP.exists():
log.info('Обнаружен файл "%s", пропускаю проверку.', FILE_NAME_SKIP.name)
wait(**timeout)
continue
try:
log.debug(format_get_items)
items = get_new_items()
log.debug(format_items, len(items), items)
# Если текущих список пустой
if not current_items:
log.debug(format_first_start_detected)
current_items = items
save_items(current_items)
else:
new_items = set(items) - set(current_items)
if new_items:
current_items = items
save_items(current_items)
for item in new_items:
text = format_new_item % item
log.debug(text)
if notified_by_sms:
simple_send_sms(text, log)
else:
log.debug(format_no_new_items)
wait(**timeout)
except:
log.exception(format_on_exception)
log.debug(format_on_exception_next_attempt)
# Wait <timeout_exception_seconds> before next attempt
time.sleep(timeout_exception_seconds)