Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os
- import time
- import traceback
- import logging
- import logging.config
- import yaml
- import traceback
- from subprocess import Popen, PIPE
- #######################################################
- # Environment Variable # Details #
- #######################################################
- # sonarr_eventtype # Rename #
- # sonarr_series_id # Internal ID of the series #
- # sonarr_series_title # Title of the series #
- # sonarr_series_path # Full path to the series #
- # sonarr_series_tvdbid # TVDB ID for the series #
- #######################################################
- def setup_logging(
- default_path='logging.yaml',
- default_level=logging.INFO,
- env_key='LOG_CFG'
- ):
- """Setup logging configuration"""
- path = default_path
- value = os.getenv(env_key, None)
- if value:
- path = value
- if os.path.exists(path):
- with open(path, 'rt') as f:
- config = yaml.safe_load(f.read())
- logging.config.dictConfig(config)
- else:
- logging.basicConfig(level=default_level)
- # logging setup
- setup_logging()
- log = logging.getLogger('EmberMediaManager')
- log.info("Sonarr Ember Media Manager post-processing script started.")
- # Make sure kodipydent is already installed
- try:
- from kodipydent import Kodi
- try:
- full_update = True
- kodi_instance = Kodi('192.168.1.77', username='kodi', password='XXXXXXXXXX')
- kodi_tvshows = kodi_instance.VideoLibrary.GetTVShows(filter={"field": "title", "operator": "contains", "value": "{}".format(os.environ.get('sonarr_series_title'))})
- if "result" in kodi_tvshows: # Make sure we get a response
- # Check if tvshow is already on Kodi VideoLibrary
- if "tvshows" in kodi_tvshows["result"]:
- log.info("TV Show %s already on kodi's library. Scan folder for new episode.", os.environ.get('sonarr_series_title'))
- full_update = False
- else:
- log.info("TV Show %s not yet on kodi's library. Set Full Update.", os.environ.get('sonarr_series_title'))
- full_update = True
- else:
- log.info("Could not retrieve TV Shows from kodi's library. Set Full Update.")
- full_update = True
- except Exception as err:
- if "Request timed out" in err:
- log.error("There was a problem connecting to your Kodi instance. Make sure it is launched!")
- full_update = True
- elif "URLError" in err:
- log.error("There was a problem connecting to your Kodi instance. Make sure it is launched!")
- full_update = True
- elif "Unauthorized" in err:
- log.error("There was a problem connecting to your Kodi instance. Check if 'username' and 'password' are correct!")
- full_update = True
- else:
- log.error("Failed to connect to your Kodi instance: %s", traceback.format_exc())
- except ImportError:
- log.error("You don't have 'kodipydent' installed. Please run 'pip install kodipydent'")
- quit()
- # Ember Media Manager installation path (MUST BET SET AS AN ENV VARIABLE)
- app_path = os.environ.get('EmberMediaManager')
- if not app_path:
- log.info("You MUST set EmberMediaManager an a env and point to executable")
- else:
- # build command to run Ember Media Manager and update and scrape new TV Show
- #full_update = True # temporary flag until Dan implements episode scraping
- command = []
- command.append(app_path)
- command.append(u'-profile')
- command.append(u'redglory')
- if full_update:
- command.append(u'-updatetvshows')
- command.append(u'-scrapetvshows')
- command.append(u'newauto')
- command.append(u'all')
- else:
- # scan processed TV Show path for new episode
- command.append(u'-scanfolder')
- command.append(u'"{}"'.format(os.environ.get('sonarr_series_path')))
- command.append(u'-nowindow')
- startTime = time.time()
- log.info("Starting Ember Media Manager with application arguments: %s", str(command))
- log.info("TV Show Title: %s", os.environ.get('sonarr_series_title'))
- log.info("TV Show Full Path: %s", os.environ.get('sonarr_series_path'))
- log.info("TV Show TVDB ID: %s", os.environ.get('sonarr_series_tvdbid'))
- log.info("TV Show Internal ID: %s", os.environ.get('sonarr_series_id'))
- try:
- p = Popen(command, stdout=PIPE, stderr=PIPE)
- log.info("Ember Media Manager: running on PID: (" + str(p.pid) + ")")
- output, error = p.communicate()
- if p.returncode == 0:
- endTime = time.time()
- log.info("Ember Media Manager ran sucessfully for: %s seconds", str(int(endTime - startTime)))
- else:
- log.error("Ember Media Manager failed with: %d %s %s" % (p.returncode, output, error))
- except:
- log.error("Oh No! Something went wrong!: %s", traceback.format_exc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement