62 lines
2.6 KiB
Python
62 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Send one owner notification after Radarr import and Jellyfin indexing."""
|
|
import json
|
|
from pathlib import Path
|
|
import urllib.request
|
|
|
|
TMDB = 124459
|
|
STATE = Path('/var/lib/media-ready/burt-wonderstone.sent')
|
|
|
|
def env(path):
|
|
return dict(line.strip().split('=', 1) for line in open(path)
|
|
if '=' in line and not line.startswith('#'))
|
|
|
|
def api(base, path, headers=None, data=None):
|
|
request = urllib.request.Request(base + path, headers=headers or {},
|
|
data=None if data is None else json.dumps(data).encode())
|
|
with urllib.request.urlopen(request, timeout=30) as response:
|
|
body = response.read()
|
|
return json.loads(body) if body else None
|
|
|
|
def main():
|
|
if STATE.exists():
|
|
return
|
|
keys = env('/etc/media.env')
|
|
movies = api('http://127.0.0.1:7878', '/api/v3/movie',
|
|
{'X-Api-Key': keys['RADARR_KEY']})
|
|
movie = next((m for m in movies if m['tmdbId'] == TMDB), None)
|
|
if not movie or not movie.get('hasFile'):
|
|
print('Waiting for Radarr import')
|
|
return
|
|
base = 'http://127.0.0.1:8096'
|
|
headers = {'Authorization': 'MediaBrowser Token="' + keys['JELLYFIN_KEY'] + '"'}
|
|
items = api(base, '/Items?Recursive=true&IncludeItemTypes=Movie&Fields=ProviderIds', headers)
|
|
item = next((i for i in items['Items']
|
|
if i.get('ProviderIds', {}).get('Tmdb') == str(TMDB)), None)
|
|
if not item:
|
|
api(base, '/Library/Refresh', headers, {})
|
|
print('Waiting for Jellyfin index; refresh requested')
|
|
return
|
|
server = api(base, '/System/Info/Public')['Id']
|
|
link = ('http://192.168.10.25:8096/web/#/details?id='
|
|
+ item['Id'] + '&serverId=' + server)
|
|
token = env('/etc/astra-bot.env')['TG_TOKEN']
|
|
result = api('https://api.telegram.org', '/bot' + token + '/sendMessage',
|
|
{'Content-Type': 'application/json'},
|
|
{'chat_id': 607015, 'text': '«Невероятный Бёрт Уандерстоун» скачался. '
|
|
'Русская озвучка, 1080p. Смотреть дома:\n' + link,
|
|
'disable_web_page_preview': True})
|
|
if not result.get('ok'):
|
|
raise RuntimeError('Telegram did not confirm delivery')
|
|
STATE.parent.mkdir(parents=True, exist_ok=True)
|
|
STATE.write_text(str(result['result']['message_id']) + '\n')
|
|
print('Ready notification delivered')
|
|
|
|
if __name__ == '__main__':
|
|
# urllib exceptions can contain the Telegram token in their URL.
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print('Movie notification failed: ' + type(error).__name__)
|
|
raise SystemExit(1)
|