SHARE
TWEET

Untitled

mattrix Aug 23rd, 2017 189 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os, platform, requests, shutil, traceback
  2. import xbmc, xbmcgui, xbmcaddon
  3. import wvconfig as config
  4.  
  5. def check_inputstream(reinstall=False):
  6.     system_ = platform.system()
  7.     arch = platform.machine()
  8.  
  9.     __addon__ = xbmcaddon.Addon('script.module.wvhelper')
  10.     __data_dir__ = xbmc.translatePath(__addon__.getAddonInfo('profile'))
  11.     data_file = os.path.join(__data_dir__, 'installed.txt')
  12.  
  13.     if not reinstall:
  14.         try:
  15.             addon = xbmcaddon.Addon('inputstream.adaptive')
  16.             cdm_path = xbmc.translatePath(addon.getSetting('DECRYPTERPATH'))
  17.  
  18.             with open(data_file) as f:
  19.                 installed = f.read()
  20.  
  21.             if (addon.getAddonInfo('version') == installed and
  22.                 os.path.exists(os.path.join(cdm_path, config.WIDEVINECDM_DICT[system_])) and
  23.                     os.path.exists(os.path.join(cdm_path, config.SSD_WV_DICT[system_]))):
  24.  
  25.                 return True
  26.         except:
  27.             pass
  28.    
  29.     if system_ == 'Windows':
  30.         arch = platform.architecture()[0]
  31.     if arch[:3] == 'arm':
  32.         arch = arch[:5]
  33.     if arch == 'i686':
  34.         arch = 'i386'
  35.  
  36.     try:
  37.         kodi_major_version = int(xbmc.getInfoLabel("System.BuildVersion").split('.')[0])
  38.     except Exception:
  39.         kodi_major_version = 0
  40.  
  41.     if system_ == 'Linux' and xbmc.getCondVisibility('system.platform.android'):
  42.         system_ = 'Android'
  43.         supported = kodi_major_version >= 18
  44.     elif system_+arch in config.SUPPORTED_PLATFORMS:
  45.         supported = kodi_major_version >= 17
  46.     else:
  47.         supported = False
  48.  
  49.     if not supported:
  50.         xbmc.log('DRM Not Supported: {0} KODI v{1}'.format(system_+arch, kodi_major_version), xbmc.LOGNOTICE)
  51.  
  52.         msg = 'This system (%s KODI v%s) is not currently supported for DRM content playback.' % (system_+arch, kodi_major_version)
  53.         if kodi_major_version < 17:
  54.             msg += '\nDRM playback requires at least KODI 17.'
  55.         if system_ == 'Android':
  56.             msg += '\nDRM support for Android is coming in KODI 18.'
  57.  
  58.         xbmcgui.Dialog().ok('Not Supported', msg)
  59.         return False
  60.  
  61.     def get_addon():
  62.         try:
  63.             xbmc.executeJSONRPC('{"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"inputstream.adaptive","enabled":false}}')
  64.             xbmc.executeJSONRPC('{"jsonrpc":"2.0","id":1,"method":"Addons.SetAddonEnabled","params":{"addonid":"inputstream.adaptive","enabled":true}}')
  65.             return xbmcaddon.Addon('inputstream.adaptive')
  66.         except:
  67.             return False
  68.  
  69.     addon = get_addon()
  70.     if not addon:
  71.         xbmc.executebuiltin('InstallAddon(inputstream.adaptive)', True)
  72.  
  73.         addon = get_addon()
  74.         if not addon:
  75.             xbmcgui.Dialog().ok('Missing inputstream.adaptive add-on',
  76.                                 'inputstream.adaptive VideoPlayer InputStream add-on \
  77.                                not found or not enabled. This add-on is required to \
  78.                                view DRM protected content.')
  79.             return False
  80.  
  81.     cdm_path = xbmc.translatePath(addon.getSetting('DECRYPTERPATH'))
  82.     if not os.path.isdir(cdm_path):
  83.         os.makedirs(cdm_path)
  84.  
  85.     if system_ != 'Android':
  86.         try:
  87.             get_widevinecdm(cdm_path, system_, arch, addon)
  88.             get_ssd_wv(cdm_path, system_, arch, addon)
  89.         except:
  90.             traceback.print_exc()
  91.             xbmcgui.Dialog().ok('ERROR', 'There was an error installing DRM. Please restart KODI and try again.')
  92.             return False
  93.  
  94.     if not os.path.exists(__data_dir__):
  95.         os.mkdir(__data_dir__)
  96.  
  97.     with open(data_file, 'w') as f:
  98.         f.write(addon.getAddonInfo('version'))
  99.  
  100.     xbmcgui.Dialog().ok('DRM installed OK.', 'If videos still won\'t play - please try restarting KODI.')
  101.  
  102.     return True
  103.  
  104. def remove_path(path):
  105.     if os.path.islink(path):
  106.         os.unlink(path)
  107.     elif os.path.exists(path):
  108.         os.remove(path)
  109.  
  110. def get_widevinecdm(cdm_path, system_, arch, addon):
  111.     filename = config.WIDEVINECDM_DICT[system_]
  112.  
  113.     download_path = os.path.join(cdm_path, filename)
  114.     remove_path(download_path)
  115.  
  116.     version = addon.getAddonInfo('version')[0] + '.0'
  117.     url = config.GIT_URL.format(version, system_, arch, filename)
  118.  
  119.     if not progress_download(url, download_path, filename):
  120.         raise Exception("Failed to download file")
  121.  
  122.     os.chmod(download_path, 0755)
  123.  
  124. def get_ssd_wv(cdm_path, system_, arch, addon):
  125.     filename = config.SSD_WV_DICT[system_]
  126.  
  127.     download_path = os.path.join(cdm_path, filename)
  128.     remove_path(download_path)
  129.  
  130.     addon_path = xbmc.translatePath(addon.getAddonInfo('path')).decode("utf-8")
  131.     paths = [os.path.join(addon_path, filename), os.path.join(addon_path, 'lib', filename), os.path.join(os.sep, 'usr', 'lib', filename)]
  132.     for path in paths:
  133.         if os.path.exists(path):
  134.             try:
  135.                 shutil.copy(path, download_path)
  136.                 os.chmod(download_path, 0755)
  137.                 return
  138.             except:
  139.                 continue
  140.  
  141.     version = addon.getAddonInfo('version')[0] + '.0'
  142.     url = config.GIT_URL.format(version, system_, arch, filename)
  143.  
  144.     if not progress_download(url, download_path, filename):
  145.         raise Exception("Failed to download file")
  146.  
  147.     os.chmod(download_path, 0755)
  148.    
  149. def progress_download(url, download_path, filename):
  150.     xbmc.log('Downloading {0}'.format(url),xbmc.LOGNOTICE)
  151.  
  152.     try:
  153.         res = requests.get(url, stream=True, verify=False)
  154.         res.raise_for_status()
  155.     except requests.exceptions.HTTPError:
  156.         xbmcgui.Dialog().ok('Download failed', 'HTTP '+str(res.status_code)+' error')
  157.         xbmc.log('Error retrieving {0}'.format(url), level=xbmc.LOGNOTICE)
  158.         return False
  159.  
  160.     total_length = float(res.headers.get('content-length'))
  161.     dp = xbmcgui.DialogProgress()
  162.     dp.create("Installing DRM", "Downloading", url)
  163.  
  164.     with open(download_path, 'wb') as f:
  165.         chunk_size = 1024
  166.         downloaded = 0
  167.         for chunk in res.iter_content(chunk_size=chunk_size):
  168.             f.write(chunk)
  169.             downloaded += len(chunk)
  170.             percent = int(downloaded*100/total_length)
  171.             if dp.iscanceled():
  172.                 dp.close()
  173.                 res.close()
  174.             dp.update(percent)
  175.  
  176.     xbmc.log('Download {0} bytes complete, saved in {1}'.format(
  177.                 int(total_length), download_path),xbmc.LOGNOTICE)
  178.  
  179.     dp.close()
  180.     return True
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top