Advertisement
Guest User

livechannels.py for CBC

a guest
May 20th, 2023
105
0
13 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | Source Code | 0 0
  1. """Module for live channels."""
  2. import json
  3. from urllib import urlencode
  4.  
  5. import requests
  6.  
  7. from resources.lib.utils import save_cookies, loadCookies, log, get_iptv_channels_file
  8. from resources.lib.cbc import CBC
  9.  
  10. LIST_URL = 'https://tpfeed.cbc.ca/f/ExhSPC/t_t3UKJR6MAT?pretty=true&sort=pubDate%7Cdesc'
  11. LIST_ELEMENT = 'entries'
  12.  
  13.  
  14.  
  15. class LiveChannels:
  16.     """Class for live channels."""
  17.  
  18.     def __init__(self):
  19.         """Initialize the live channels class."""
  20.         # Create requests session object
  21.         self.session = requests.Session()
  22.         session_cookies = loadCookies()
  23.         if session_cookies is not None:
  24.             self.session.cookies = session_cookies
  25.  
  26.     def get_live_channels(self):
  27.         """Get the list of live channels."""
  28.         resp = self.session.get(LIST_URL)
  29.  
  30.         if not resp.status_code == 200:
  31.             log('ERROR: {} returns status of {}'.format(LIST_URL, resp.status_code), True)
  32.             return None
  33.         save_cookies(self.session.cookies)
  34.  
  35.         items = json.loads(resp.content)[LIST_ELEMENT]
  36.         return items
  37.  
  38.     def get_iptv_channels(self):
  39.         """Get the channels in a IPTV Manager compatible list."""
  40.         cbc = CBC()
  41.         channels = self.get_live_channels()
  42.         blocked = self.get_blocked_iptv_channels()
  43.         result = []
  44.         for channel in channels:
  45.             callsign = CBC.get_callsign(channel)
  46.  
  47.             # if the user has omitted this from the list of their channels, don't populate it
  48.             if callsign in blocked:
  49.                 continue
  50.  
  51.             labels = CBC.get_labels(channel)
  52.             image = cbc.getImage(channel)
  53.             values = {
  54.                 'url': channel['content'][0]['url'],
  55.                 'image': image,
  56.                 'labels': urlencode(labels)
  57.             }
  58.             channel_dict = {
  59.                 'name': channel['title'],
  60.                 'stream': 'plugin://plugin.video.cbc/smil?' + urlencode(values),
  61.                 'id': callsign,
  62.                 'logo': image
  63.             }
  64.  
  65.             # Use "CBC Toronto" instead of "Toronto"
  66.             if len(channel_dict['name']) < 4 or channel_dict['name'][0:4] != 'CBC ':
  67.                 channel_dict['name'] = 'CBC {}'.format(channel_dict['name'])
  68.             result.append(channel_dict)
  69.  
  70.         return result
  71.  
  72.     @staticmethod
  73.     def get_blocked_iptv_channels():
  74.         """Get the list of blocked channels."""
  75.         chan_file = get_iptv_channels_file()
  76.         try:
  77.             with open(get_iptv_channels_file(), 'r') as chan_file:
  78.                 return json.load(chan_file)
  79.         except FileNotFoundError:
  80.             return []
  81.  
  82.     @staticmethod
  83.     def remove_iptv_channel(channel):
  84.         """Add all live channels for IPTV."""
  85.         blocked = LiveChannels.get_blocked_iptv_channels()
  86.  
  87.         if channel not in blocked:
  88.             blocked.append(channel)
  89.  
  90.         with open(get_iptv_channels_file(), 'w') as chan_file:
  91.             json.dump(blocked, chan_file)
  92.  
  93.     @staticmethod
  94.     def add_iptv_channel(channel):
  95.         """Add all live channels for IPTV."""
  96.         blocked = LiveChannels.get_blocked_iptv_channels()
  97.         if len(blocked) == 0:
  98.             return
  99.  
  100.         if channel in blocked:
  101.             blocked.remove(channel)
  102.  
  103.         with open(get_iptv_channels_file(), 'w') as chan_file:
  104.             json.dump(blocked, chan_file)
  105.  
  106.     @staticmethod
  107.     def add_only_iptv_channel(channel):
  108.         """
  109.        Add only a single specified channel to the list of IPTV channels.
  110.  
  111.        This method gets the list of all channels, and removes the only one the user wants, leaving the rest as an
  112.        extensive filter.
  113.        """
  114.         blocked = [CBC.get_callsign(chan) for chan in LiveChannels().get_live_channels()]
  115.  
  116.         if channel in blocked:
  117.             blocked.remove(channel)
  118.  
  119.         with open(get_iptv_channels_file(), 'w') as chan_file:
  120.             json.dump(blocked, chan_file)
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement