Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.05 KB | None | 0 0
  1. import os
  2. import sys
  3. import locale
  4.  
  5. try:
  6. import configparser
  7. except ImportError:
  8. import ConfigParser as configparser
  9. import logging
  10. from extensions import *
  11. from babelfish import Language
  12.  
  13.  
  14. class ReadSettings:
  15.  
  16. def __init__(self, directory, filename, logger=None):
  17.  
  18. # Setup logging
  19. if logger:
  20. log = logger
  21. else:
  22. log = logging.getLogger(__name__)
  23.  
  24. # Setup encoding to avoid UTF-8 errors
  25. if sys.version[0] == '2':
  26. SYS_ENCODING = None
  27. try:
  28. locale.setlocale(locale.LC_ALL, "")
  29. SYS_ENCODING = locale.getpreferredencoding()
  30. except (locale.Error, IOError):
  31. pass
  32.  
  33. # For OSes that are poorly configured just force UTF-8
  34. if not SYS_ENCODING or SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
  35. SYS_ENCODING = 'UTF-8'
  36.  
  37. if not hasattr(sys, "setdefaultencoding"):
  38. reload(sys)
  39.  
  40. try:
  41. # pylint: disable=E1101
  42. # On non-unicode builds this will raise an AttributeError, if encoding type is not valid it throws a LookupError
  43. sys.setdefaultencoding(SYS_ENCODING)
  44. except:
  45. log.exception("Sorry, your environment is not setup correctly for utf-8 support. Please fix your setup and try again")
  46. sys.exit("Sorry, your environment is not setup correctly for utf-8 support. Please fix your setup and try again")
  47.  
  48. log.info(sys.executable)
  49.  
  50. # Default settings for SickBeard
  51. sb_defaults = {'host': 'localhost',
  52. 'port': '8081',
  53. 'ssl': "False",
  54. 'api_key': '',
  55. 'web_root': '',
  56. 'username': '',
  57. 'password': ''}
  58. # Default MP4 conversion settings
  59. mp4_defaults = {'ffmpeg': 'ffmpeg.exe',
  60. 'ffprobe': 'ffprobe.exe',
  61. 'threads': 'auto',
  62. 'output_directory': '',
  63. 'copy_to': '',
  64. 'move_to': '',
  65. 'output_extension': 'mp4',
  66. 'output_format': 'mp4',
  67. 'delete_original': 'True',
  68. 'relocate_moov': 'True',
  69. 'ios-audio': 'True',
  70. 'ios-first-track-only': 'False',
  71. 'ios-audio-filter': '',
  72. 'max-audio-channels': '',
  73. 'audio-language': '',
  74. 'audio-default-language': '',
  75. 'audio-codec': 'ac3',
  76. 'audio-filter': '',
  77. 'audio-channel-bitrate': '256',
  78. 'video-codec': 'h264, x264',
  79. 'video-bitrate': '',
  80. 'video-crf': '',
  81. 'video-max-width': '',
  82. 'h264-max-level': '',
  83. 'aac_adtstoasc': 'False',
  84. 'use-qsv-decoder-with-encoder': 'True',
  85. 'subtitle-codec': 'mov_text',
  86. 'subtitle-language': '',
  87. 'subtitle-default-language': '',
  88. 'subtitle-encoding': '',
  89. 'convert-mp4': 'False',
  90. 'fullpathguess': 'True',
  91. 'confirmtagdata': 'True',
  92. 'tagfile': 'True',
  93. 'tag-language': 'en',
  94. 'download-artwork': 'poster',
  95. 'download-subs': 'False',
  96. 'embed-subs': 'True',
  97. 'sub-providers': 'addic7ed, podnapisi, thesubdb, opensubtitles',
  98. 'permissions': '777',
  99. 'post-process': 'False',
  100. 'pix-fmt': '',
  101. 'preopts': '',
  102. 'postopts': ''}
  103. # Default settings for CouchPotato
  104. cp_defaults = {'host': 'localhost',
  105. 'port': '5050',
  106. 'username': '',
  107. 'password': '',
  108. 'apikey': '',
  109. 'delay': '65',
  110. 'method': 'renamer',
  111. 'delete_failed': 'False',
  112. 'ssl': 'False',
  113. 'web_root': ''}
  114. # Default settings for Sonarr
  115. sonarr_defaults = {'host': 'localhost',
  116. 'port': '8989',
  117. 'apikey': '',
  118. 'ssl': 'False',
  119. 'web_root': ''}
  120. # Default settings for Radarr
  121. radarr_defaults = {'host': 'localhost',
  122. 'port': '7878',
  123. 'apikey': '',
  124. 'ssl': 'False',
  125. 'web_root': ''}
  126. # Default uTorrent settings
  127. utorrent_defaults = {'couchpotato-label': 'couchpotato',
  128. 'sickbeard-label': 'sickbeard',
  129. 'sickrage-label': 'sickrage',
  130. 'sonarr-label': 'sonarr',
  131. 'radarr-label': 'radarr',
  132. 'bypass-label': 'bypass',
  133. 'convert': 'True',
  134. 'webui': 'False',
  135. 'action_before': 'stop',
  136. 'action_after': 'removedata',
  137. 'host': 'http://localhost:8080/',
  138. 'username': '',
  139. 'password': '',
  140. 'output_directory': ''}
  141. # Default SAB settings
  142. sab_defaults = {'convert': 'True',
  143. 'Sickbeard-category': 'sickbeard',
  144. 'Sickrage-category': 'sickrage',
  145. 'Couchpotato-category': 'couchpotato',
  146. 'Sonarr-category': 'sonarr',
  147. 'Radarr-category': 'radarr',
  148. 'Bypass-category': 'bypass',
  149. 'output_directory': ''}
  150. # Default Sickrage Settings
  151. sr_defaults = {'host': 'localhost',
  152. 'port': '8081',
  153. 'ssl': "False",
  154. 'api_key': '',
  155. 'web_root': '',
  156. 'username': '',
  157. 'password': ''}
  158.  
  159. # Default deluge settings
  160. deluge_defaults = {'couchpotato-label': 'couchpotato',
  161. 'sickbeard-label': 'sickbeard',
  162. 'sickrage-label': 'sickrage',
  163. 'sonarr-label': 'sonarr',
  164. 'radarr-label': 'radarr',
  165. 'bypass-label': 'bypass',
  166. 'convert': 'True',
  167. 'host': 'localhost',
  168. 'port': '58846',
  169. 'username': '',
  170. 'password': '',
  171. 'output_directory': '',
  172. 'remove': 'false'}
  173.  
  174. # Default Plex Settings
  175. plex_defaults = {'host': 'localhost',
  176. 'port': '32400',
  177. 'refresh': 'true',
  178. 'token': ''}
  179.  
  180. defaults = {'SickBeard': sb_defaults, 'CouchPotato': cp_defaults, 'Sonarr': sonarr_defaults, 'Radarr': radarr_defaults, 'MP4': mp4_defaults, 'uTorrent': utorrent_defaults, 'SABNZBD': sab_defaults, 'Sickrage': sr_defaults, 'Deluge': deluge_defaults, 'Plex': plex_defaults}
  181. write = False # Will be changed to true if a value is missing from the config file and needs to be written
  182.  
  183. config = configparser.SafeConfigParser()
  184. configFile = os.path.join(directory, filename)
  185. if os.path.isfile(configFile):
  186. config.read(configFile)
  187. else:
  188. log.error("Config file not found, creating %s." % configFile)
  189. # config.filename = filename
  190. write = True
  191.  
  192. # Make sure all sections and all keys for each section are present
  193. for s in defaults:
  194. if not config.has_section(s):
  195. config.add_section(s)
  196. write = True
  197. for k in defaults[s]:
  198. if not config.has_option(s, k):
  199. config.set(s, k, defaults[s][k])
  200. write = True
  201.  
  202. # If any keys are missing from the config file, write them
  203. if write:
  204. self.writeConfig(config, configFile)
  205.  
  206. # Read relevant MP4 section information
  207. section = "MP4"
  208. self.ffmpeg = os.path.normpath(self.raw(config.get(section, "ffmpeg"))) # Location of FFMPEG.exe
  209. self.ffprobe = os.path.normpath(self.raw(config.get(section, "ffprobe"))) # Location of FFPROBE.exe
  210. self.threads = config.get(section, "threads") # Number of FFMPEG threads
  211. try:
  212. if int(self.threads) < 1:
  213. self.threads = "auto"
  214. except:
  215. self.threads = "auto"
  216.  
  217. self.output_dir = config.get(section, "output_directory")
  218. if self.output_dir == '':
  219. self.output_dir = None
  220. else:
  221. self.output_dir = os.path.normpath(self.raw(self.output_dir)) # Output directory
  222. self.copyto = config.get(section, "copy_to") # Directories to make copies of the final product
  223. if self.copyto == '':
  224. self.copyto = None
  225. else:
  226. self.copyto = self.copyto.split('|')
  227. for i in range(len(self.copyto)):
  228. self.copyto[i] = os.path.normpath(self.copyto[i])
  229. if not os.path.isdir(self.copyto[i]):
  230. try:
  231. os.makedirs(self.copyto[i])
  232. except:
  233. log.exception("Error making directory %s." % (self.copyto[i]))
  234. self.moveto = config.get(section, "move_to") # Directory to move final product to
  235. if self.moveto == '':
  236. self.moveto = None
  237. else:
  238. self.moveto = os.path.normpath(self.moveto)
  239. if not os.path.isdir(self.moveto):
  240. try:
  241. os.makedirs(self.moveto)
  242. except:
  243. log.exception("Error making directory %s." % (self.moveto))
  244. self.moveto = None
  245.  
  246. self.output_extension = config.get(section, "output_extension") # Output extension
  247. self.output_format = config.get(section, "output_format") # Output format
  248. if self.output_format not in valid_formats:
  249. self.output_format = 'mov'
  250. self.delete = config.getboolean(section, "delete_original") # Delete original file
  251. self.relocate_moov = config.getboolean(section, "relocate_moov") # Relocate MOOV atom to start of file
  252. if self.relocate_moov:
  253. try:
  254. import qtfaststart
  255. except:
  256. log.error("Please install QTFastStart via PIP, relocate_moov will be disabled without this module.")
  257. self.relocate_moov = False
  258. self.acodec = config.get(section, "audio-codec").lower() # Gets the desired audio codec, if no valid codec selected, default to AC3
  259. if self.acodec == '':
  260. self.acodec == ['ac3']
  261. else:
  262. self.acodec = self.acodec.lower().replace(' ', '').split(',')
  263.  
  264. self.abitrate = config.get(section, "audio-channel-bitrate")
  265. try:
  266. self.abitrate = int(self.abitrate)
  267. except:
  268. self.abitrate = 256
  269. log.warning("Audio bitrate was invalid, defaulting to 256 per channel.")
  270. if self.abitrate > 256:
  271. log.warning("Audio bitrate >256 may create errors with common codecs.")
  272.  
  273. self.afilter = config.get(section, "audio-filter").lower().strip() # Audio filter
  274. if self.afilter == '':
  275. self.afilter = None
  276.  
  277. self.iOS = config.get(section, "ios-audio") # Creates a second audio channel if the standard output methods are different from this for iOS compatability
  278. if self.iOS == "" or self.iOS.lower() in ['false', 'no', 'f', '0']:
  279. self.iOS = False
  280. else:
  281. if self.iOS.lower() in ['true', 'yes', 't', '1']:
  282. self.iOS = ['aac']
  283. else:
  284. self.iOS = self.iOS.lower().replace(' ', '').split(',')
  285.  
  286. self.iOSFirst = config.getboolean(section, "ios-first-track-only") # Enables the iOS audio option only for the first track
  287.  
  288. self.iOSfilter = config.get(section, "ios-audio-filter").lower().strip() # iOS audio filter
  289. if self.iOSfilter == '':
  290. self.iOSfilter = None
  291.  
  292. self.downloadsubs = config.getboolean(section, "download-subs") # Enables downloading of subtitles from the internet sources using subliminal
  293. if self.downloadsubs:
  294. try:
  295. import subliminal
  296. except Exception as e:
  297. self.downloadsubs = False
  298. log.exception("Subliminal is not installed, automatically downloading of subs has been disabled.")
  299. self.subproviders = config.get(section, 'sub-providers').lower()
  300. if self.subproviders == '':
  301. self.downloadsubs = False
  302. log.warning("You must specifiy at least one subtitle provider to downlaod subs automatically, subtitle downloading disabled.")
  303. else:
  304. self.subproviders = self.subproviders.lower().replace(' ', '').split(',')
  305.  
  306. self.embedsubs = config.getboolean(section, 'embed-subs')
  307.  
  308. self.permissions = config.get(section, 'permissions')
  309. try:
  310. self.permissions = int(self.permissions, 8)
  311. except:
  312. log.exception("Invalid permissions, defaulting to 777.")
  313. self.permissions = int("0777", 8)
  314.  
  315. try:
  316. self.postprocess = config.getboolean(section, 'post-process')
  317. except:
  318. self.postprocess = False
  319.  
  320. self.aac_adtstoasc = config.getboolean(section, 'aac_adtstoasc')
  321.  
  322. # Setup variable for maximum audio channels
  323. self.maxchannels = config.get(section, 'max-audio-channels')
  324. if self.maxchannels == "":
  325. self.maxchannels = None
  326. else:
  327. try:
  328. self.maxchannels = int(self.maxchannels)
  329. except:
  330. log.exception("Invalid number of audio channels specified.")
  331. self.maxchannels = None
  332. if self.maxchannels is not None and self.maxchannels < 1:
  333. log.warning("Must have at least 1 audio channel.")
  334. self.maxchannels = None
  335.  
  336. self.vcodec = config.get(section, "video-codec")
  337. if self.vcodec == '':
  338. self.vcodec == ['h264', 'x264']
  339. else:
  340. self.vcodec = self.vcodec.lower().replace(' ', '').split(',')
  341.  
  342. self.vbitrate = config.get(section, "video-bitrate")
  343. if self.vbitrate == '':
  344. self.vbitrate = None
  345. else:
  346. try:
  347. self.vbitrate = int(self.vbitrate)
  348. if not (self.vbitrate > 0):
  349. self.vbitrate = None
  350. log.warning("Video bitrate must be greater than 0, defaulting to no video bitrate cap.")
  351. except:
  352. log.exception("Invalid video bitrate, defaulting to no video bitrate cap.")
  353. self.vbitrate = None
  354.  
  355. self.vcrf = config.get(section, "video-crf")
  356. if self.vcrf == '':
  357. self.vcrf = None
  358. else:
  359. try:
  360. self.vcrf = int(self.vcrf)
  361. except:
  362. log.exception("Invalid CRF setting, defaulting to none.")
  363. self.vcrf = None
  364.  
  365. self.vwidth = config.get(section, "video-max-width")
  366. if self.vwidth == '':
  367. self.vwidth = None
  368. else:
  369. try:
  370. self.vwidth = int(self.vwidth)
  371. except:
  372. log.exception("Invalid video width, defaulting to none.")
  373. self.vwidth = None
  374.  
  375. self.h264_level = config.get(section, "h264-max-level")
  376. if self.h264_level == '':
  377. self.h264_level = None
  378. else:
  379. try:
  380. self.h264_level = float(self.h264_level)
  381. except:
  382. log.exception("Invalid h264 level, defaulting to none.")
  383. self.h264_level = None
  384.  
  385. self.qsv_decoder = config.getboolean(section, "use-qsv-decoder-with-encoder") # Use Intel QuickSync Decoder when using QuickSync Encoder
  386. self.pix_fmt = config.get(section, "pix-fmt").strip().lower()
  387. if self.pix_fmt == '':
  388. self.pix_fmt = None
  389. else:
  390. self.pix_fmt = self.pix_fmt.replace(' ', '').split(',')
  391.  
  392. self.awl = config.get(section, 'audio-language').strip().lower() # List of acceptable languages for audio streams to be carried over from the original file, separated by a comma. Blank for all
  393. if self.awl == '':
  394. self.awl = None
  395. else:
  396. self.awl = self.awl.replace(' ', '').split(',')
  397.  
  398. self.scodec = config.get(section, 'subtitle-codec').strip().lower()
  399. if not self.scodec or self.scodec == "":
  400. if self.embedsubs:
  401. self.scodec = ['mov_text']
  402. else:
  403. self.scodec = ['srt']
  404. log.warning("Invalid subtitle codec, defaulting to '%s'." % self.scodec)
  405. else:
  406. self.scodec = self.scodec.replace(' ', '').split(',')
  407.  
  408. if self.embedsubs:
  409. if len(self.scodec) > 1:
  410. log.warning("Can only embed one subtitle type, defaulting to 'mov_text'.")
  411. self.scodec = ['mov_text']
  412. if self.scodec[0] not in valid_internal_subcodecs:
  413. log.warning("Invalid interal subtitle codec %s, defaulting to 'mov_text'." % self.scodec[0])
  414. self.scodec = ['mov_text']
  415. else:
  416. for codec in self.scodec:
  417. if codec not in valid_external_subcodecs:
  418. log.warning("Invalid external subtitle codec %s, ignoring." % codec)
  419. self.scodec.remove(codec)
  420.  
  421. if len(self.scodec) == 0:
  422. log.warning("No valid subtitle formats found, defaulting to 'srt'.")
  423. self.scodec = ['srt']
  424.  
  425. self.swl = config.get(section, 'subtitle-language').strip().lower() # List of acceptable languages for subtitle streams to be carried over from the original file, separated by a comma. Blank for all
  426. if self.swl == '':
  427. self.swl = None
  428. else:
  429. self.swl = self.swl.replace(' ', '').split(',')
  430.  
  431. self.subencoding = config.get(section, 'subtitle-encoding').strip().lower()
  432. if self.subencoding == '':
  433. self.subencoding = None
  434.  
  435. self.adl = config.get(section, 'audio-default-language').strip().lower() # What language to default an undefinied audio language tag to. If blank, it will remain undefined. This is useful for single language releases which tend to leave things tagged as und
  436. if self.adl == "" or len(self.adl) > 3:
  437. self.adl = None
  438.  
  439. self.sdl = config.get(section, 'subtitle-default-language').strip().lower() # What language to default an undefinied subtitle language tag to. If blank, it will remain undefined. This is useful for single language releases which tend to leave things tagged as und
  440. if self.sdl == ""or len(self.sdl) > 3:
  441. self.sdl = None
  442. # Prevent incompatible combination of settings
  443. if self.output_dir == "" and self.delete is False:
  444. log.error("You must specify an alternate output directory if you aren't going to delete the original file.")
  445. sys.exit()
  446. # Create output directory if it does not exist
  447. if self.output_dir is not None:
  448. if not os.path.isdir(self.output_dir):
  449. os.makedirs(self.output_dir)
  450. self.processMP4 = config.getboolean(section, "convert-mp4") # Determine whether or not to reprocess mp4 files or just tag them
  451. self.fullpathguess = config.getboolean(section, "fullpathguess") # Guess using the full path or not
  452. self.confirmtagdata = config.getboolean(section, "confirmtagdata") # Ask to confirm matched data
  453. self.tagfile = config.getboolean(section, "tagfile") # Tag files with metadata
  454. self.taglanguage = config.get(section, "tag-language").strip().lower() # Language to tag files
  455. if len(self.taglanguage) > 2:
  456. try:
  457. babel = Language(self.taglanguage)
  458. self.taglanguage = babel.alpha2
  459. except:
  460. log.exception("Unable to set tag language, defaulting to English.")
  461. self.taglanguage = 'en'
  462. elif len(self.taglanguage) < 2:
  463. log.exception("Unable to set tag language, defaulting to English.")
  464. self.taglanguage = 'en'
  465. self.artwork = config.get(section, "download-artwork").lower() # Download and embed artwork
  466. if self.artwork == "poster":
  467. self.artwork = True
  468. self.thumbnail = False
  469. elif self.artwork == "thumb" or self.artwork == "thumbnail":
  470. self.artwork = True
  471. self.thumbnail = True
  472. else:
  473. self.thumbnail = False
  474. try:
  475. self.artwork = config.getboolean(section, "download-artwork")
  476. except:
  477. self.artwork = True
  478. log.error("Invalid download-artwork value, defaulting to 'poster'.")
  479.  
  480. self.preopts = config.get(section, "preopts").lower()
  481. if self.preopts == '':
  482. self.preopts = None
  483. else:
  484. self.preopts = self.preopts.split(',')
  485.  
  486. self.postopts = config.get(section, "postopts").lower()
  487. if self.postopts == '':
  488. self.postopts = None
  489. else:
  490. self.postopts = self.postopts.split(',')
  491.  
  492. # Read relevant CouchPotato section information
  493. section = "CouchPotato"
  494. self.CP = {}
  495. self.CP['host'] = config.get(section, "host")
  496. self.CP['port'] = config.get(section, "port")
  497. self.CP['username'] = config.get(section, "username")
  498. self.CP['password'] = config.get(section, "password")
  499. self.CP['apikey'] = config.get(section, "apikey")
  500. self.CP['delay'] = config.get(section, "delay")
  501. self.CP['method'] = config.get(section, "method")
  502. self.CP['web_root'] = config.get(section, "web_root")
  503.  
  504. try:
  505. self.CP['delay'] = float(self.CP['delay'])
  506. except ValueError:
  507. self.CP['delay'] = 60
  508. try:
  509. self.CP['delete_failed'] = config.getboolean(section, "delete_failed")
  510. except (configparser.NoOptionError, ValueError):
  511. self.CP['delete_failed'] = False
  512. try:
  513. if config.getboolean(section, 'ssl'):
  514. self.CP['protocol'] = "https://"
  515. else:
  516. self.CP['protocol'] = "http://"
  517. except (configparser.NoOptionError, ValueError):
  518. self.CP['protocol'] = "http://"
  519.  
  520. # Read relevant uTorrent section information
  521. section = "uTorrent"
  522. self.uTorrent = {}
  523. self.uTorrent['cp'] = config.get(section, "couchpotato-label").lower()
  524. self.uTorrent['sb'] = config.get(section, "sickbeard-label").lower()
  525. self.uTorrent['sr'] = config.get(section, "sickrage-label").lower()
  526. self.uTorrent['sonarr'] = config.get(section, "sonarr-label").lower()
  527. self.uTorrent['radarr'] = config.get(section, "radarr-label").lower()
  528. self.uTorrent['bypass'] = config.get(section, "bypass-label").lower()
  529. try:
  530. self.uTorrent['convert'] = config.getboolean(section, "convert")
  531. except:
  532. self.uTorrent['convert'] = False
  533. self.uTorrent['output_dir'] = config.get(section, "output_directory")
  534. if self.uTorrent['output_dir'] == '':
  535. self.uTorrent['output_dir'] = None
  536. else:
  537. self.uTorrent['output_dir'] = os.path.normpath(self.raw(self.uTorrent['output_dir'])) # Output directory
  538. self.uTorrentWebUI = config.getboolean(section, "webui")
  539. self.uTorrentActionBefore = config.get(section, "action_before").lower()
  540. self.uTorrentActionAfter = config.get(section, "action_after").lower()
  541. self.uTorrentHost = config.get(section, "host").lower()
  542. self.uTorrentUsername = config.get(section, "username")
  543. self.uTorrentPassword = config.get(section, "password")
  544.  
  545. # Read relevant Deluge section information
  546. section = "Deluge"
  547. self.deluge = {}
  548. self.deluge['cp'] = config.get(section, "couchpotato-label").lower()
  549. self.deluge['sb'] = config.get(section, "sickbeard-label").lower()
  550. self.deluge['sr'] = config.get(section, "sickrage-label").lower()
  551. self.deluge['sonarr'] = config.get(section, "sonarr-label").lower()
  552. self.deluge['radarr'] = config.get(section, "radarr-label").lower()
  553. self.deluge['bypass'] = config.get(section, "bypass-label").lower()
  554. try:
  555. self.deluge['convert'] = config.getboolean(section, "convert")
  556. except:
  557. self.deluge['convert'] = False
  558. self.deluge['host'] = config.get(section, "host").lower()
  559. self.deluge['port'] = config.get(section, "port")
  560. self.deluge['user'] = config.get(section, "username")
  561. self.deluge['pass'] = config.get(section, "password")
  562. self.deluge['output_dir'] = config.get(section, "output_directory")
  563. self.deluge['remove'] = config.getboolean(section, "remove")
  564. if self.deluge['output_dir'] == '':
  565. self.deluge['output_dir'] = None
  566. else:
  567. self.deluge['output_dir'] = os.path.normpath(self.raw(self.deluge['output_dir'])) # Output directory
  568.  
  569. # Read relevant Sonarr section information
  570. section = "Sonarr"
  571. self.Sonarr = {}
  572. self.Sonarr['host'] = config.get(section, "host")
  573. self.Sonarr['port'] = config.get(section, "port")
  574. self.Sonarr['apikey'] = config.get(section, "apikey")
  575. self.Sonarr['ssl'] = config.get(section, "ssl")
  576. self.Sonarr['web_root'] = config.get(section, "web_root")
  577. if not self.Sonarr['web_root'].startswith("/"):
  578. self.Sonarr['web_root'] = "/" + self.Sonarr['web_root']
  579. if self.Sonarr['web_root'].endswith("/"):
  580. self.Sonarr['web_root'] = self.Sonarr['web_root'][:-1]
  581.  
  582. # Read relevant Radarr section information
  583. section = "Radarr"
  584. self.Radarr = {}
  585. self.Radarr['host'] = config.get(section, "host")
  586. self.Radarr['port'] = config.get(section, "port")
  587. self.Radarr['apikey'] = config.get(section, "apikey")
  588. self.Radarr['ssl'] = config.get(section, "ssl")
  589. self.Radarr['web_root'] = config.get(section, "web_root")
  590. if not self.Radarr['web_root'].startswith("/"):
  591. self.Radarr['web_root'] = "/" + self.Radarr['web_root']
  592. if self.Radarr['web_root'].endswith("/"):
  593. self.Radarr['web_root'] = self.Radarr['web_root'][:-1]
  594.  
  595. # Read Sickbeard section information
  596. section = "SickBeard"
  597. self.Sickbeard = {}
  598. self.Sickbeard['host'] = config.get(section, "host") # Server Address
  599. self.Sickbeard['port'] = config.get(section, "port") # Server Port
  600. self.Sickbeard['api_key'] = config.get(section, "api_key") # Sickbeard API key
  601. self.Sickbeard['web_root'] = config.get(section, "web_root") # Sickbeard webroot
  602. self.Sickbeard['ssl'] = config.getboolean(section, "ssl") # SSL
  603. self.Sickbeard['user'] = config.get(section, "username")
  604. self.Sickbeard['pass'] = config.get(section, "password")
  605.  
  606. # Read Sickrage section information
  607. section = "Sickrage"
  608. self.Sickrage = {}
  609. self.Sickrage['host'] = config.get(section, "host") # Server Address
  610. self.Sickrage['port'] = config.get(section, "port") # Server Port
  611. self.Sickrage['api_key'] = config.get(section, "api_key") # Sickbeard API key
  612. self.Sickrage['web_root'] = config.get(section, "web_root") # Sickbeard webroot
  613. self.Sickrage['ssl'] = config.getboolean(section, "ssl") # SSL
  614. self.Sickrage['user'] = config.get(section, "username")
  615. self.Sickrage['pass'] = config.get(section, "password")
  616.  
  617. # Read SAB section information
  618. section = "SABNZBD"
  619. self.SAB = {}
  620. try:
  621. self.SAB['convert'] = config.getboolean(section, "convert") # Convert
  622. except:
  623. self.SAB['convert'] = False
  624. self.SAB['cp'] = config.get(section, "Couchpotato-category").lower()
  625. self.SAB['sb'] = config.get(section, "Sickbeard-category").lower()
  626. self.SAB['sr'] = config.get(section, "Sickrage-category").lower()
  627. self.SAB['sonarr'] = config.get(section, "Sonarr-category").lower()
  628. self.SAB['radarr'] = config.get(section, "Radarr-category").lower()
  629. self.SAB['bypass'] = config.get(section, "Bypass-category").lower()
  630. self.SAB['output_dir'] = config.get(section, "output_directory")
  631. if self.SAB['output_dir'] == '':
  632. self.SAB['output_dir'] = None
  633. else:
  634. self.SAB['output_dir'] = os.path.normpath(self.raw(self.SAB['output_dir'])) # Output directory
  635.  
  636. # Read Plex section information
  637. section = "Plex"
  638. self.Plex = {}
  639. self.Plex['host'] = config.get(section, "host")
  640. self.Plex['port'] = config.get(section, "port")
  641. try:
  642. self.Plex['refresh'] = config.getboolean(section, "refresh")
  643. except:
  644. self.Plex['refresh'] = False
  645. self.Plex['token'] = config.get(section, "token")
  646. if self.Plex['token'] == '':
  647. self.Plex['token'] = None
  648.  
  649. # Pass the values on
  650. self.config = config
  651. self.configFile = configFile
  652.  
  653. def getRefreshURL(self, tvdb_id):
  654. config = self.config
  655. section = "SickBeard"
  656.  
  657. protocol = "http://" # SSL
  658. try:
  659. if config.getboolean(section, "ssl"):
  660. protocol = "https://"
  661. except (configparser.NoOptionError, ValueError):
  662. pass
  663. host = config.get(section, "host") # Server Address
  664. port = config.get(section, "port") # Server Port
  665. api_key = config.get(section, "api_key") # Sickbeard API key
  666. web_root = config.get(section, "web_root") # Sickbeard webroot
  667.  
  668. sickbeard_url = protocol + host + ":" + port + web_root + "/api/" + api_key + "/?cmd=show.refresh&tvdbid=" + str(tvdb_id)
  669. return sickbeard_url
  670.  
  671. def writeConfig(self, config, cfgfile):
  672. fp = open(cfgfile, "w")
  673. try:
  674. config.write(fp)
  675. except IOError:
  676. pass
  677. fp.close()
  678.  
  679. def raw(self, text):
  680. escape_dict = {'\a': r'\a',
  681. '\b': r'\b',
  682. '\c': r'\c',
  683. '\f': r'\f',
  684. '\n': r'\n',
  685. '\r': r'\r',
  686. '\t': r'\t',
  687. '\v': r'\v',
  688. '\'': r'\'',
  689. '\"': r'\"',
  690. '\0': r'\0',
  691. '\1': r'\1',
  692. '\2': r'\2',
  693. '\3': r'\3',
  694. '\4': r'\4',
  695. '\5': r'\5',
  696. '\6': r'\6',
  697. '\7': r'\7',
  698. '\8': r'\8',
  699. '\9': r'\9'}
  700.  
  701. output = ''
  702. for char in text:
  703. try:
  704. output += escape_dict[char]
  705. except KeyError:
  706. output += char
  707. return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement