Repository navigation
Expand file tree
/
Copy pathsonarr_rss_4k.py
More file actions
165 lines (132 loc) · 4.67 KB
/
Copy pathsonarr_rss_4k.py
File metadata and controls
165 lines (132 loc) · 4.67 KB
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
#!/usr/bin/env python
import requests
import logging
import sys
import json
from bs4 import BeautifulSoup
################################
# About
################################
#
# This script reads RSS feeds from most newznab based indexers (tested with nzbgeek and dog).
# It then checks if you have any of the shows in the RSS feed added to sonarr, if not, adds them.
#
# The intention of this is to auto add 4K shows to a seperate 4k Sonarr instance as 4k versions become available.
#
# Run this script on a cron.
#
################################
# Config
################################
# Add rocket chat webhook here if you want to send notifcation on adding shows
# Leave blank if you do not want to use
uri = ''
sonarr_url = 'https://sonarr4k.domain.com:443'
sonarr_api = ''
sonarr_path = '/mnt/unionfs/Media/4K-TV'
rss_url = {'https://api.nzbgeek.info/rss?t=5045&dl=1&num=50&r=xxxxxxxxxxxxx',
'https://dognzb.cr/rss.cfm?r=xxxxxxxxxxxxx&t=5045&num=100'}
quality_profile_id = 5
################################
# Logging
################################
# Logging format
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# root logger
logger = logging.getLogger()
# Set initial level to INFO
logger.setLevel(logging.INFO)
# Console handler, log to stdout
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
# Other modules logging levels
logging.getLogger('requests').setLevel(logging.INFO)
################################
# Init
################################
headers = {'X-Api-Key': sonarr_api}
options = {'ignoreEpisodesWithFiles': False, 'ignoreEpisodesWithoutFiles': False,
'searchForMissingEpisodes': True}
################################
# Main
################################
def get_rss_tvdbid():
tvdb_id_list = []
for url in rss_url:
req = requests.get(url)
handler = req.text.encode('utf-8')
soup = BeautifulSoup(handler, 'xml')
for item in soup.findAll('item'):
newznabs = item.findAll('newznab:attr')
newz_dict = {}
for attribute in newznabs:
newz_dict[attribute['name'].split('.')[0]] = attribute['value'].split(".")[0]
try:
if newz_dict['tvdbid'] not in tvdb_id_list:
tvdb_id_list.append(newz_dict['tvdbid'])
except KeyError:
pass
return tvdb_id_list
def get_library():
library = []
r = requests.get(sonarr_url + '/api/series', headers=headers, timeout=60)
try:
if r.status_code == 401:
logger.warning("Error when connecting to sonarr, unauthorised. check api/url")
sys.exit(1)
tv_lib_raw = r.json()
for n in tv_lib_raw:
if n['tvdbId'] not in library:
library.append(n['tvdbId'])
except requests.ConnectionError:
logger.warning('Can not connect to sonarr check if sonarr is up, or URL is right')
sys.exit(1)
return library
def send_to_sonarr(tvid):
logger.info('Attempting to send to sonarr')
payload = {'tvdbId': tvid, 'title': '', 'qualityProfileId': quality_profile_id, 'images': [],
'seasons': [], 'seasonFolder': True, 'monitored': True, 'rootFolderPath': sonarr_path,
'addOptions': options, }
r = requests.post(sonarr_url + '/api/series', headers=headers, data=json.dumps(payload), timeout=30)
if r.status_code == 201:
logger.info('sent to sonarr successfully')
return True
else:
logger.info('failed to send to sonarr, code return: {}'.format(r.status_code))
logger.debug(r.content)
def library_compare(source, new):
library = []
for tvid in new:
if int(tvid) not in source:
library.append(tvid)
else:
logger.debug('{} is already in library'.format(tvid))
return library
def send_notif(info):
data = {
"username": "Sonarr RSS Bot",
"icon_emoji": ":robot:",
"attachments": [
{
"title": "Sonarr RSS Feed",
"text": "{} Show(s) where added to sonarr".format(info)
}
]
}
r = requests.post(uri, json.dumps(data)).content
return r
if __name__ == '__main__':
library = get_library()
rss_library = get_rss_tvdbid()
content = library_compare(library, rss_library)
logger.info(content)
logger.info(library)
if len(content) == 0:
logger.info('no shows to add')
else:
for show in content:
send_to_sonarr(show)
if uri:
send_notif(len(content))
logger.info('Finished')