summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2021-04-20 01:15:59 +0300
committerEvgeny Zinoviev <me@ch1p.io>2021-04-20 01:15:59 +0300
commitc9517a0ba91ce3a55ca14b2432d26f3b2c9dd933 (patch)
tree731db81a283cfe00d7bc00f2a9151e614036ec8a
parenta5eb909949d7feb8dea501698bfbf139d321a1e9 (diff)
catch errors
-rw-r--r--telegram_notify.py82
1 files changed, 47 insertions, 35 deletions
diff --git a/telegram_notify.py b/telegram_notify.py
index bba2964..bc5c834 100644
--- a/telegram_notify.py
+++ b/telegram_notify.py
@@ -1,8 +1,18 @@
-import requests
+import requests, traceback
from mgs import MGSPiracy
from argparse import ArgumentParser
from jstate import JState
+
+def send(text: str, token: str, chat_id: int):
+ r = requests.post('https://api.telegram.org/bot%s/sendMessage' % token, data={
+ 'chat_id': chat_id,
+ 'text': text
+ })
+ if r.status_code != 200:
+ print('error: telegram returned code %d' % r.status_code)
+
+
if __name__ == '__main__':
# parse arguments
parser = ArgumentParser()
@@ -14,37 +24,39 @@ if __name__ == '__main__':
parser.add_argument('--domains', nargs='+', required=True)
args = parser.parse_args()
- # get recent cases
- mgs = MGSPiracy(from_page=args._from, to_page=args.to)
- cases = mgs.get_cases()
-
- # read state
- jst = JState(args.state_file, default=dict(cases=[]))
- data = jst.read()
-
- # loop through cases
- results = []
- for case in cases:
- if case['statement_number'] in data['cases']:
- continue
-
- matched = False
- for mydomain in args.domains:
- if mydomain in case['decision_text']:
- matched = True
- results.append('%s found in %s' % (mydomain, case['statement_number']))
- data['cases'].append(case['statement_number'])
-
- if matched:
- break
-
- # remember found cases
- jst.write(data)
-
- # if found anything, send to telegram
- if results:
- text = '\n'.join(results)
- r = requests.post('https://api.telegram.org/bot%s/sendMessage' % args.token, data={
- 'chat_id': args.chat_id,
- 'text': 'new mos-gorsud findings:\n' + text
- }) \ No newline at end of file
+ try:
+ # get recent cases
+ mgs = MGSPiracy(from_page=args._from, to_page=args.to)
+ cases = mgs.get_cases()
+
+ # read state
+ jst = JState(args.state_file, default=dict(cases=[]))
+ data = jst.read()
+
+ # loop through cases
+ results = []
+ for case in cases:
+ if case['statement_number'] in data['cases']:
+ continue
+
+ matched = False
+ for mydomain in args.domains:
+ if mydomain in case['decision_text']:
+ matched = True
+ results.append('%s found in %s' % (mydomain, case['statement_number']))
+ data['cases'].append(case['statement_number'])
+
+ if matched:
+ break
+
+ # remember found cases
+ jst.write(data)
+
+ # if found anything, send to telegram
+ if results:
+ text = '\n'.join(results)
+ text = 'new mos-gorsud findings:\n' + text
+
+ send(text=text, token=args.token, chat_id=args.chat_id)
+ except Exception as e:
+ send(text='error: '+traceback.format_exc(), token=args.token, chat_id=args.chat_id) \ No newline at end of file