Skip to content

Commit

Permalink
Fix Pickling Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky4546 committed Dec 22, 2023
1 parent 0b37cc3 commit 2c7515c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import lib.common.exceptions as exceptions

VERSION = '0.9.14.00-RC12'
VERSION = '0.9.14.00-RC13'
CABERNET_URL = 'https://github.com/cabernetwork/cabernet'
CABERNET_ID = 'cabernet'
CABERNET_REPO = 'manifest.json'
Expand Down
5 changes: 0 additions & 5 deletions lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ def main(script_dir):
init_versions(plugins)

if opersystem in ['Windows']:
# Need to make sure all http_sessions are None since
# that object cannot pickle
for name, plugin in plugins.plugins.items():
if plugin.plugin_obj.http_session is not None:
plugin.plugin_obj.http_session = None
pickle_it = Pickling(config)
pickle_it.to_pickle(plugins)

Expand Down
5 changes: 0 additions & 5 deletions lib/plugins/plugin_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __init__(self, _instance_obj):
self.ch_num_enum = self.config_obj.data[self.config_section].get('channel-start_ch_num')
if self.ch_num_enum is None or self.ch_num_enum < 0:
self.ch_num_enum = 0
self.plugin_obj.initialize_http_session()

def terminate(self):
"""
Expand Down Expand Up @@ -84,7 +83,6 @@ def get_channels(self):
@handle_url_except()
@handle_json_except
def get_uri_json_data(self, _uri, _retries):
self.plugin_obj.initialize_http_session()
header = {
'Content-Type': 'application/json',
'User-agent': utils.DEFAULT_USER_AGENT}
Expand All @@ -95,7 +93,6 @@ def get_uri_json_data(self, _uri, _retries):

@handle_url_except()
def get_uri_data(self, _uri, _retries, _header=None, _data=None):
self.plugin_obj.initialize_http_session()
if _header is None:
header = {
'User-agent': utils.DEFAULT_USER_AGENT}
Expand All @@ -110,7 +107,6 @@ def get_uri_data(self, _uri, _retries, _header=None, _data=None):

@handle_url_except()
def get_m3u8_data(self, _uri, _retries, _header=None):
self.plugin_obj.initialize_http_session()
if _header is None:
return m3u8.load(_uri,
headers={'User-agent': utils.DEFAULT_USER_AGENT},
Expand Down Expand Up @@ -199,7 +195,6 @@ def get_thumbnail_size(self, _thumbnail, _retries, _ch_uid, ):

@handle_url_except
def get_best_stream(self, _url, _retries, _channel_id, _referer=None):
self.plugin_obj.initialize_http_session()
if self.config_obj.data[self.config_section]['player-stream_type'] == 'm3u8redirect':
return _url

Expand Down
31 changes: 20 additions & 11 deletions lib/plugins/plugin_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, _plugin):
self.logger = logging.getLogger(__name__)
self.plugin = _plugin
self.plugins = None
self.http_session = None
self.http_session = PluginObj.HttpSession()
self.config_obj = _plugin.config_obj
self.namespace = _plugin.namespace
self.def_trans = ''.join([
Expand Down Expand Up @@ -69,15 +69,6 @@ def terminate(self):
self.instances = None
self.scheduler_db = None

def initialize_http_session(self):
"""
httpx cannot be initialized until later due to the httpx lib not being
pickleable. So, when a function call is made, it checks to see if the
http_session has been initialized and if not, will initialize it.
"""
if self.http_session is None:
self.http_session = httpx.Client(http2=True, verify=False, follow_redirects=True)



# INTERFACE METHODS
Expand Down Expand Up @@ -241,7 +232,6 @@ def refresh_it(self, _what_to_refresh, _instance=None):
"""
_what_to_refresh is either 'EPG' or 'Channels' for now
"""
self.initialize_http_session()
try:
if not self.enabled:
self.logger.debug(
Expand Down Expand Up @@ -308,3 +298,22 @@ def check_logger_refresh(self):
@property
def name(self):
return self.namespace

class HttpSession:
"""
This class handles the management of the httpx session since
pickling of the httpx Client throws an exception.
"""
def __init__(self):
self.http_session = None

def get(self, uri, headers=None, timeout=8):
if self.http_session is None:
self.http_session = httpx.Client(http2=True, verify=False, follow_redirects=True)
return self.http_session.get(uri, headers=headers, timeout=timeout)

def post(self, uri, headers=None, data=None, timeout=8):
if self.http_session is None:
self.http_session = httpx.Client(http2=True, verify=False, follow_redirects=True)
return self.http_session.post(uri, headers=headers, data=data, timeout=timeout)

2 changes: 1 addition & 1 deletion lib/streams/m3u8_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def get_uri_data(self, _uri, _retries):
_retries is used by the decorator when a HTTP failure occurs
"""
global HTTP_TIMEOUT
resp = M3U8Queue.http_session.get(_uri, headers=M3U8Queue.http_header, timeout=HTTP_TIMEOUT, follow_redirects=True)
resp = M3U8Queue.http_session.get(_uri, headers=M3U8Queue.http_header, timeout=HTTP_TIMEOUT)
x = resp.content
resp.raise_for_status()
return x
Expand Down

0 comments on commit 2c7515c

Please sign in to comment.