45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Enable owner Telegram notifications for Chernobyl imports; run as root."""
|
|
import json
|
|
import urllib.request
|
|
|
|
def env(path):
|
|
return dict(line.strip().split('=', 1) for line in open(path)
|
|
if '=' in line and not line.startswith('#'))
|
|
|
|
key = env('/etc/media.env')['SONARR_KEY']
|
|
|
|
def api(path, data=None, method=None):
|
|
req = urllib.request.Request('http://127.0.0.1:8989/api/v3/' + path,
|
|
data=None if data is None else json.dumps(data).encode(),
|
|
headers={'X-Api-Key': key, 'Content-Type': 'application/json'},
|
|
method=method or ('GET' if data is None else 'POST'))
|
|
with urllib.request.urlopen(req, timeout=30) as response:
|
|
return json.load(response)
|
|
|
|
series = next(s for s in api('series') if s['tvdbId'] == 360893)
|
|
tags = api('tag')
|
|
tag = next((t for t in tags if t['label'] == 'notify-owner-chernobyl'), None)
|
|
if tag is None:
|
|
tag = api('tag', {'label': 'notify-owner-chernobyl'})
|
|
if tag['id'] not in series['tags']:
|
|
series['tags'].append(tag['id'])
|
|
api('series/' + str(series['id']), series, 'PUT')
|
|
name = 'Telegram: Chernobyl owner'
|
|
existing = next((n for n in api('notification') if n['name'] == name), None)
|
|
notification = existing or next(n for n in api('notification/schema') if n['implementation'] == 'Telegram')
|
|
for field in notification['fields']:
|
|
values = {'botToken': env('/etc/astra-bot.env')['TG_TOKEN'], 'chatId': '607015',
|
|
'sendSilently': False, 'includeAppNameInTitle': True}
|
|
if field['name'] in values:
|
|
field['value'] = values[field['name']]
|
|
for event in list(notification):
|
|
if event.startswith('on') and isinstance(notification[event], bool):
|
|
notification[event] = False
|
|
notification.update(name=name, tags=[tag['id']], onDownload=True)
|
|
api('notification/' + str(existing['id']) if existing else 'notification', notification,
|
|
'PUT' if existing else 'POST')
|
|
check = next(n for n in api('notification') if n['name'] == name)
|
|
assert check['onDownload'] and check['tags'] == [tag['id']]
|
|
print('Configured: Telegram notifications for Chernobyl episode imports, owner chat 607015.')
|