Advertisement
mattrix

Untitled

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