Last active
April 18, 2022 12:34
-
-
Save dawtmaytrikx/8c0314dd8c7c19b321c493fffd743a85 to your computer and use it in GitHub Desktop.
Gets the most recent English TV/MOViE releases from xREL and adds the groups to sonarr/radarr whitelists (release profiles or restrictions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sqlite3 | |
from collections import namedtuple | |
import sys | |
import requests | |
import time | |
import datetime | |
sonarr = namedtuple('sonarr', 'url, apikey, required, ignored') | |
sonarr.url = 'http://sonarr/api/v3/releaseprofile/1' | |
sonarr.apikey = '' | |
sonarr4k = namedtuple('sonarr4k', 'url, apikey, required, ignored') | |
sonarr4k.url = 'http://sonarr4k/api/v3/releaseprofile/1' | |
sonarr4k.apikey = '' | |
radarr = namedtuple('radarr', 'url, apikey, required, ignored') | |
radarr.url = 'http://radarr/api/v3/restriction/1' | |
radarr.apikey = '' | |
radarr4k = namedtuple('radarr4k', 'url, apikey, required, ignored') | |
radarr4k.url = 'http://radarr4k/api/v3/restriction/1' | |
radarr4k.apikey = '' | |
if not os.path.exists('scenegroups.db'): | |
with open('scenegroups.db', 'w'): pass | |
con = sqlite3.connect('scenegroups.db') | |
cur = con.cursor() | |
sql_command = ''' | |
CREATE TABLE IF NOT EXISTS scenegroups ( | |
id INTEGER PRIMARY KEY, | |
groupname TEXT, | |
release TEXT, | |
pvr TEXT, | |
releasedate INTEGER, | |
date TEXT | |
);''' | |
cur.execute(sql_command) | |
con.commit() | |
sql_command = ''' | |
CREATE TABLE IF NOT EXISTS latest ( | |
id INTEGER PRIMARY KEY, | |
release TEXT, | |
releasedate INTEGER, | |
date TEXT | |
);''' | |
cur.execute(sql_command) | |
con.commit() | |
cur.execute('SELECT releasedate FROM latest WHERE id = 1') | |
result = cur.fetchone() | |
if result is not None: | |
lastprocessed = result[0] | |
else: | |
lastprocessed = 1 | |
for page in range(1, 51): | |
xrel = requests.get( | |
'https://api.xrel.to/v2/release/browse_category.json?category_name=ENGLISH&per_page=100&page={}'.format(str(page))) | |
if xrel.status_code != 200: | |
print('ERROR: xREL API failed.') | |
exit | |
xrel = xrel.json()['list'] | |
for pvr in (sonarr, sonarr4k, radarr, radarr4k): | |
notify_headers = {'content-type': 'application/json', | |
'accept': 'application/json', 'X-Api-Key': pvr.apikey} | |
response = requests.get(pvr.url, headers=notify_headers) | |
if response.status_code != 200: | |
print('ERROR: Something\s wrong with {}'.format(pvr.__name__)) | |
continue | |
response = response.json() | |
pvr.required = response['required'] | |
pvr.ignored = response['ignored'] | |
checked = [] | |
for release in xrel: | |
is_ignored = False | |
already_processed = False | |
if page == 1 and release == xrel[0]: | |
latestrelease = release | |
# don't process releases ignored by $pvr | |
if isinstance(pvr.ignored, str): | |
ignorelist = pvr.ignored.split(',') | |
elif isinstance(pvr.ignored, list): | |
ignorelist = pvr.ignored | |
else: | |
print('ERROR: Something\'s wrong with the ignored in {}'.format( | |
pvr.__name__)) | |
exit | |
for ignorepattern in ignorelist: | |
if ignorepattern in release['dirname']: | |
is_ignored = True | |
break | |
if is_ignored == True: | |
print('IGNORED: {} in {}.'.format( | |
release['dirname'], pvr.__name__)) | |
continue | |
else: | |
newrestriction = '-' + release['group_name'] | |
# don't process releases previously processed | |
if release['time'] <= lastprocessed: | |
already_processed = True | |
break | |
# don't process duplicates | |
if newrestriction in checked: | |
continue | |
else: | |
checked.append(newrestriction) | |
## TO DO: convert all instances to list and back? | |
if isinstance(pvr.required, list): | |
if newrestriction not in pvr.required: | |
pvr.required.append(newrestriction) | |
else: | |
print('SKIPPED: {} is already in {}! Release: {}'.format( | |
newrestriction, pvr.__name__, release['dirname'])) | |
continue | |
elif isinstance(pvr.required, str): # convert to list and back instead? | |
if not pvr.required.endswith(',' + newrestriction) and ',{},'.format(newrestriction) not in pvr.required: | |
pvr.required += ',' + newrestriction | |
else: | |
print('SKIPPED: {} is already in {}! Release: {}'.format( | |
newrestriction, pvr.__name__, release['dirname'])) | |
continue | |
else: | |
print('ERROR: required in {} are neither LIST nor STR'.format( | |
pvr.__name__)) | |
continue | |
# update $required | |
response['required'] = pvr.required | |
notify_data = response | |
reply = requests.put( | |
pvr.url, json=notify_data, headers=notify_headers) | |
while reply.status_code != 202: | |
print('ERROR: Could not add {} to {}.\nResponse code: {}'.format( | |
newrestriction, pvr.__name__, str(reply.status_code))) | |
time.sleep(60) | |
## TO DO: Handle errors | |
if reply.status_code == 202: | |
cur.execute('''INSERT INTO scenegroups (groupname, release, pvr, releasedate, date) VALUES (?, ?, ?, ?, ?)''', | |
(release['group_name'], release['dirname'], pvr.__name__, release['time'], datetime.datetime.now())) | |
con.commit() | |
print('ADDED {} to {}! Release: {}'.format( | |
newrestriction, pvr.__name__, release['dirname'])) | |
# will only trigger after all pvrs reached the previously processed release | |
if already_processed == True: | |
break | |
if latestrelease['time'] != lastprocessed: | |
cur.execute('''INSERT INTO latest (id, release, releasedate, date) VALUES (?, ?, ?, ?) | |
ON CONFLICT (id) DO UPDATE SET release=excluded.release, releasedate=excluded.releasedate, date=excluded.date''', | |
(1, latestrelease['dirname'], latestrelease['time'], datetime.datetime.now())) | |
con.commit() | |
else: | |
print('INFO: Nothing to do.') | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment