Advertisement
Guest User

utils.py for CBC

a guest
May 20th, 2023
78
0
12 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | Source Code | 0 0
  1. """Utilities module."""
  2. import os
  3. import pickle
  4. from requests.utils import dict_from_cookiejar
  5. from requests.cookies import cookiejar_from_dict
  6.  
  7.  
  8. def get_cookie_file():
  9.     """Get the cookies file."""
  10.     try:
  11.         from xbmc import translatePath
  12.         base = translatePath('special://userdata/addon_data/plugin.video.cbc')
  13.     except ModuleNotFoundError:
  14.         base = os.getcwd()
  15.  
  16.     return os.path.join(base, 'cookies')
  17.  
  18.  
  19. def get_iptv_channels_file():
  20.     """Get the filename for the IPTV channels filter."""
  21.     try:
  22.         from xbmc import translatePath
  23.         base = translatePath('special://userdata/addon_data/plugin.video.cbc')
  24.     except ModuleNotFoundError:
  25.         base = os.getcwd()
  26.     return os.path.join(base, 'iptvchannels')
  27.  
  28.  
  29. def save_cookies(session_cookies):
  30.     """
  31.    Write cookies to the cookie file
  32.    @param session_cookies the session.cookies object to save
  33.    """
  34.     with open(get_cookie_file(), 'wb') as f:
  35.         cookies = dict_from_cookiejar(session_cookies)
  36.         pickle.dump(cookies, f)
  37.  
  38.  
  39. def loadCookies():
  40.     """
  41.    Load cookies from the cookie file into a session.cookies object
  42.    @return a session.cookies object
  43.    """
  44.     try:
  45.         with open(get_cookie_file(), 'rb') as f:
  46.             cookies = pickle.load(f)
  47.             return cookiejar_from_dict(cookies)
  48.     except IOError as err:
  49.         log('Unable to load cookies: {}'.format(err), True)
  50.         return None
  51.  
  52.     return None
  53.  
  54.  
  55. def getAuthorizationFile():
  56.     """
  57.    Get the authorization file
  58.    """
  59.     try:
  60.         from xbmc import translatePath
  61.         base = translatePath('special://userdata/addon_data/plugin.video.cbc')
  62.     except:
  63.         base = os.getcwd()
  64.  
  65.     return os.path.join(base, 'authorization')
  66.  
  67.  
  68. def saveAuthorization(authorization):
  69.     with open(getAuthorizationFile(), 'wb') as f:
  70.         pickle.dump(authorization, f)
  71.  
  72.  
  73. def loadAuthorization():
  74.     """
  75.    Load authorization from the authorization file into an object
  76.    @return an object
  77.    """
  78.     try:
  79.         with open(getAuthorizationFile(), 'rb') as f:
  80.             authorization = pickle.load(f)
  81.             return authorization
  82.     except IOError as err:
  83.         log('Unable to load authorization: {}'.format(err), True)
  84.         return None
  85.  
  86.     return None
  87.  
  88.  
  89. def log(msg, error = False):
  90.     """
  91.    Log an error
  92.    @param msg The error to log
  93.    @param error error severity indicator
  94.    """
  95.     try:
  96.         import xbmc
  97.         full_msg = "plugin.video.cbc: {}".format(msg)
  98.         xbmc.log(full_msg, level=xbmc.LOGERROR if error else xbmc.LOGINFO)
  99.     except:
  100.         print(msg)
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement