Guest User

Untitled

a guest
Jun 7th, 2019
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import os
  3. import time
  4. import traceback
  5. import logging
  6. import logging.config
  7. import yaml
  8. import traceback
  9. from subprocess import Popen, PIPE
  10.  
  11. #######################################################
  12. # Environment Variable # Details #
  13. #######################################################
  14. # sonarr_eventtype # Rename #
  15. # sonarr_series_id # Internal ID of the series #
  16. # sonarr_series_title # Title of the series #
  17. # sonarr_series_path # Full path to the series #
  18. # sonarr_series_tvdbid # TVDB ID for the series #
  19. #######################################################
  20.  
  21.  
  22. def setup_logging(
  23. default_path='logging.yaml',
  24. default_level=logging.INFO,
  25. env_key='LOG_CFG'
  26. ):
  27. """Setup logging configuration"""
  28. path = default_path
  29. value = os.getenv(env_key, None)
  30. if value:
  31. path = value
  32. if os.path.exists(path):
  33. with open(path, 'rt') as f:
  34. config = yaml.safe_load(f.read())
  35. logging.config.dictConfig(config)
  36. else:
  37. logging.basicConfig(level=default_level)
  38.  
  39. # logging setup
  40. setup_logging()
  41.  
  42. log = logging.getLogger('EmberMediaManager')
  43.  
  44. log.info("Sonarr Ember Media Manager post-processing script started.")
  45.  
  46. # Make sure kodipydent is already installed
  47. try:
  48. from kodipydent import Kodi
  49. try:
  50. full_update = True
  51. kodi_instance = Kodi('192.168.1.77', username='kodi', password='XXXXXXXXXX')
  52. kodi_tvshows = kodi_instance.VideoLibrary.GetTVShows(filter={"field": "title", "operator": "contains", "value": "{}".format(os.environ.get('sonarr_series_title'))})
  53. if "result" in kodi_tvshows: # Make sure we get a response
  54. # Check if tvshow is already on Kodi VideoLibrary
  55. if "tvshows" in kodi_tvshows["result"]:
  56. log.info("TV Show %s already on kodi's library. Scan folder for new episode.", os.environ.get('sonarr_series_title'))
  57. full_update = False
  58. else:
  59. log.info("TV Show %s not yet on kodi's library. Set Full Update.", os.environ.get('sonarr_series_title'))
  60. full_update = True
  61. else:
  62. log.info("Could not retrieve TV Shows from kodi's library. Set Full Update.")
  63. full_update = True
  64. except Exception as err:
  65. if "Request timed out" in err:
  66. log.error("There was a problem connecting to your Kodi instance. Make sure it is launched!")
  67. full_update = True
  68. elif "URLError" in err:
  69. log.error("There was a problem connecting to your Kodi instance. Make sure it is launched!")
  70. full_update = True
  71. elif "Unauthorized" in err:
  72. log.error("There was a problem connecting to your Kodi instance. Check if 'username' and 'password' are correct!")
  73. full_update = True
  74. else:
  75. log.error("Failed to connect to your Kodi instance: %s", traceback.format_exc())
  76. except ImportError:
  77. log.error("You don't have 'kodipydent' installed. Please run 'pip install kodipydent'")
  78. quit()
  79.  
  80. # Ember Media Manager installation path (MUST BET SET AS AN ENV VARIABLE)
  81. app_path = os.environ.get('EmberMediaManager')
  82.  
  83. if not app_path:
  84. log.info("You MUST set EmberMediaManager an a env and point to executable")
  85. else:
  86. # build command to run Ember Media Manager and update and scrape new TV Show
  87. #full_update = True # temporary flag until Dan implements episode scraping
  88. command = []
  89. command.append(app_path)
  90. command.append(u'-profile')
  91. command.append(u'redglory')
  92. if full_update:
  93. command.append(u'-updatetvshows')
  94. command.append(u'-scrapetvshows')
  95. command.append(u'newauto')
  96. command.append(u'all')
  97. else:
  98. # scan processed TV Show path for new episode
  99. command.append(u'-scanfolder')
  100. command.append(u'"{}"'.format(os.environ.get('sonarr_series_path')))
  101. command.append(u'-nowindow')
  102.  
  103. startTime = time.time()
  104.  
  105. log.info("Starting Ember Media Manager with application arguments: %s", str(command))
  106. log.info("TV Show Title: %s", os.environ.get('sonarr_series_title'))
  107. log.info("TV Show Full Path: %s", os.environ.get('sonarr_series_path'))
  108. log.info("TV Show TVDB ID: %s", os.environ.get('sonarr_series_tvdbid'))
  109. log.info("TV Show Internal ID: %s", os.environ.get('sonarr_series_id'))
  110.  
  111. try:
  112. p = Popen(command, stdout=PIPE, stderr=PIPE)
  113. log.info("Ember Media Manager: running on PID: (" + str(p.pid) + ")")
  114. output, error = p.communicate()
  115. if p.returncode == 0:
  116. endTime = time.time()
  117. log.info("Ember Media Manager ran sucessfully for: %s seconds", str(int(endTime - startTime)))
  118. else:
  119. log.error("Ember Media Manager failed with: %d %s %s" % (p.returncode, output, error))
  120. except:
  121. log.error("Oh No! Something went wrong!: %s", traceback.format_exc())
RAW Paste Data