def download_ts_file(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): authorization_token = requested_query_string_parameters.get( 'wmsAuthSign') channel_number = requested_query_string_parameters.get( 'channel_number') nimble_session_id = requested_query_string_parameters.get( 'nimblesessionid') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) requests_session = cls._get_session_parameter('requests_session') target_url = 'https://{0}.smoothstreams.tv/{1}/ch{2}q1.stream{3}'.format( Configuration.get_configuration_parameter('SMOOTHSTREAMS_SERVER'), Configuration.get_configuration_parameter('SMOOTHSTREAMS_SERVICE'), channel_number, re.sub(r'(/.*)?(/.*\.ts)', r'\2', requested_path)) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_number => {2}\n' ' client_uuid => {3}\n' 'Target path => {4}\n' ' Parameters\n' ' nimblesessionid => {5}\n' ' wmsAuthSign => {6}'.format( client_ip_address, requested_path, channel_number, client_uuid, target_url, nimble_session_id, authorization_token)) response = Utility.make_http_request( requests_session.get, target_url, params={ 'nimblesessionid': nimble_session_id, 'wmsAuthSign': authorization_token }, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_binary=True)) return response.content else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def _reduce_hls_stream_delay(cls, chunks_m3u8, client_uuid, channel_number, number_of_segments_to_keep=3): do_reduce_hls_stream_delay = False try: last_requested_channel_number = IPTVProxy.get_serviceable_client_parameter( client_uuid, 'last_requested_channel_number') if channel_number != last_requested_channel_number: do_reduce_hls_stream_delay = True else: last_requested_ts_file_path = IPTVProxy.get_serviceable_client_parameter( client_uuid, 'last_requested_ts_file_path') m3u8_object = m3u8.loads(chunks_m3u8) delete_segments_up_to_index = None for (segment_index, segment) in enumerate(m3u8_object.segments): if last_requested_ts_file_path in segment.uri: delete_segments_up_to_index = segment_index break if delete_segments_up_to_index: for _ in range(delete_segments_up_to_index + 1): m3u8_object.segments.pop(0) m3u8_object.media_sequence += 1 chunks_m3u8 = m3u8_object.dumps() except KeyError: do_reduce_hls_stream_delay = True if do_reduce_hls_stream_delay: m3u8_object = m3u8.loads(chunks_m3u8) for _ in range( len(m3u8_object.segments) - number_of_segments_to_keep): m3u8_object.segments.pop(0) m3u8_object.media_sequence += 1 chunks_m3u8 = m3u8_object.dumps() return chunks_m3u8
def download_playlist_m3u8(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): channel_number = requested_query_string_parameters.get( 'channel_number') http_token = requested_query_string_parameters.get('http_token') protocol = requested_query_string_parameters.get('protocol') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_channel_number', channel_number) cls.refresh_session() if protocol == 'hls': authorization_token = cls._get_session_parameter( 'authorization_token') requests_session = cls._get_session_parameter('requests_session') target_url = 'https://{0}.smoothstreams.tv/{1}/ch{2}q1.stream{3}'.format( Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVER'), Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVICE'), channel_number, re.sub(r'(/.*)?(/.*\.m3u8)', r'\2', requested_path)) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_number => {2}\n' ' client_uuid => {3}\n' ' protocol => {4}\n' 'Target path => {5}\n' ' Parameters\n' ' wmsAuthSign => {6}'.format( client_ip_address, requested_path, channel_number, client_uuid, protocol, target_url, authorization_token)) response = Utility.make_http_request( requests_session.get, target_url, params={'wmsAuthSign': authorization_token}, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=True, do_print_content=True)) return response.text.replace( 'chunks.m3u8?', 'chunks.m3u8?channel_number={0}&client_uuid={1}&http_token={2}&' .format( channel_number, client_uuid, urllib.parse.quote(http_token) if http_token else '')) else: logger.error( Utility.assemble_response_from_log_message(response)) response.raise_for_status() elif protocol == 'mpegts': authorization_token = cls._get_session_parameter( 'authorization_token') return '#EXTM3U\n' \ '#EXTINF:-1 ,{0}\n' \ 'https://{1}.smoothstreams.tv:443/{2}/ch{3}q1.stream/mpeg.2ts?' \ 'wmsAuthSign={4}'.format(SmoothStreamsEPG.get_channel_name(int(channel_number)), Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVER'), Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVICE'), channel_number, authorization_token) elif protocol == 'rtmp': authorization_token = cls._get_session_parameter( 'authorization_token') return '#EXTM3U\n' \ '#EXTINF:-1 ,{0}\n' \ 'rtmp://{1}.smoothstreams.tv:3635/{2}/ch{3}q1.stream?' \ 'wmsAuthSign={4}'.format(SmoothStreamsEPG.get_channel_name(int(channel_number)), Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVER'), Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_SERVICE'), channel_number, authorization_token)
def download_chunks_m3u8(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): authorization_token = requested_query_string_parameters.get( 'wmsAuthSign') channel_number = requested_query_string_parameters.get( 'channel_number') http_token = requested_query_string_parameters.get('http_token') nimble_session_id = requested_query_string_parameters.get( 'nimblesessionid') nimble_session_id = cls._map_nimble_session_id(client_ip_address, channel_number, client_uuid, nimble_session_id, authorization_token) IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_channel_number', channel_number) authorization_token = cls._get_session_parameter('authorization_token') requests_session = cls._get_session_parameter('requests_session') target_url = 'https://{0}.smoothstreams.tv/{1}/ch{2}q1.stream{3}'.format( Configuration.get_configuration_parameter('SMOOTHSTREAMS_SERVER'), Configuration.get_configuration_parameter('SMOOTHSTREAMS_SERVICE'), channel_number, re.sub(r'(/.*)?(/.*\.m3u8)', r'\2', requested_path)) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_number => {2}\n' ' client_uuid => {3}\n' 'Target path => {4}\n' ' Parameters\n' ' nimblesessionid => {5}\n' ' wmsAuthSign => {6}'.format( client_ip_address, requested_path, channel_number, client_uuid, target_url, nimble_session_id, authorization_token)) response = Utility.make_http_request( requests_session.get, target_url, params={ 'nimblesessionid': nimble_session_id, 'wmsAuthSign': authorization_token }, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=True, do_print_content=True)) return response.text.replace( '.ts?', '.ts?channel_number={0}&client_uuid={1}&http_token={2}&'. format(channel_number, client_uuid, urllib.parse.quote(http_token) if http_token else '')) else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def download_ts_file( cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters, ): authorization_token = requested_query_string_parameters.get( 'authorization_token') channel_number = requested_query_string_parameters.get( 'channel_number') hostname = requested_query_string_parameters.get('hostname') leaf_directory = requested_query_string_parameters.get( 'leaf_directory') port = requested_query_string_parameters.get('port') scheme = requested_query_string_parameters.get('scheme') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) username = Configuration.get_configuration_parameter( '{0}_USERNAME'.format(cls._provider_name.upper())) password = SecurityManager.decrypt_password( Configuration.get_configuration_parameter('{0}_PASSWORD'.format( cls._provider_name.upper()))).decode() requests_session = requests.Session() target_url = '{0}://{1}{2}/hls{3}{4}/{5}/{6}/{7}/{8}{9}'.format( scheme, hostname, port, 'r' if authorization_token else '', '/{0}'.format(authorization_token) if authorization_token else '', username, password, channel_number, leaf_directory, re.sub(r'(/.*)?(/.*\.ts)', r'\2', requested_path), ) logger.debug( 'Proxying request\n' 'Source IP => %s\n' 'Requested path => %s\n' ' Parameters\n' ' channel_number => %s\n' ' client_uuid => %s\n' ' hostname => %s\n' ' port => %s\n' ' scheme => %s\n' 'Target path => %s', client_ip_address, requested_path, channel_number, client_uuid, hostname, port, scheme, target_url, ) response = Utility.make_http_request( requests_session.get, target_url, headers=requests_session.headers, cookies=requests_session.cookies.get_dict(), ) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_binary=True)) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_ts_file_path', re.sub(r'(/.*)?(/.*\.ts)', r'\2', requested_path)[1:], ) return response.content logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def download_playlist_m3u8( cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters, ): channel_number = requested_query_string_parameters.get( 'channel_number') http_token = requested_query_string_parameters.get('http_token') protocol = requested_query_string_parameters.get('protocol') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_channel_number', channel_number) url = Configuration.get_configuration_parameter('{0}_URL'.format( cls._provider_name.upper())) username = Configuration.get_configuration_parameter( '{0}_USERNAME'.format(cls._provider_name.upper())) password = SecurityManager.decrypt_password( Configuration.get_configuration_parameter('{0}_PASSWORD'.format( cls._provider_name.upper()))).decode() if protocol == 'hls': requests_session = requests.Session() target_url = '{0}live/{1}/{2}/{3}.m3u8'.format( url, username, password, channel_number) logger.debug( 'Proxying request\n' 'Source IP => %s\n' 'Requested path => %s\n' ' Parameters\n' ' channel_number => %s\n' ' client_uuid => %s\n' ' protocol => %s\n' 'Target path => %s', client_ip_address, requested_path, channel_number, client_uuid, protocol, target_url, ) response = Utility.make_http_request( requests_session.get, target_url, headers=requests_session.headers, cookies=requests_session.cookies.get_dict(), ) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=True, do_print_content=True)) with cls._do_reduce_hls_stream_delay_lock.reader_lock: if cls._do_reduce_hls_stream_delay: chunks_m3u8 = cls._reduce_hls_stream_delay( response.text, client_uuid, channel_number, number_of_segments_to_keep=2, ) else: chunks_m3u8 = response.text parsed_url = urllib.parse.urlparse(response.request.url) scheme = parsed_url.scheme hostname = parsed_url.hostname port = (':{0}'.format(parsed_url.port) if parsed_url.port is not None else '') return re.sub( r'/hls/(.*)/(.*)/(.*)/(.*)/(.*).ts', r'\5.ts?' 'authorization_token=&' 'channel_number={0}&' 'client_uuid={1}&' 'hostname={2}&' 'http_token={3}&' r'leaf_directory=\4&' 'port={4}&' 'scheme={5}'.format( channel_number, client_uuid, urllib.parse.quote(hostname), urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), scheme, ), chunks_m3u8, ) elif response.status_code == requests.codes.FOUND: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=False, do_print_content=False)) parsed_url = urllib.parse.urlparse( response.headers['Location']) scheme = parsed_url.scheme hostname = parsed_url.hostname port = (':{0}'.format(parsed_url.port) if parsed_url.port is not None else '') return ('#EXTM3U\n' '#EXT-X-VERSION:3\n' '#EXT-X-STREAM-INF:BANDWIDTH=8388608\n' 'chunks.m3u8?authorization_token={0}&' 'channel_number={1}&' 'client_uuid={2}&' 'hostname={3}&' 'http_token={4}&' 'port={5}&' 'scheme={6}'.format( dict(urllib.parse.parse_qsl( parsed_url.query))['token'], channel_number, client_uuid, urllib.parse.quote(hostname), urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), scheme, )) logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status() elif protocol == 'mpegts': provider_map_class = ProvidersController.get_provider_map_class( cls._provider_name) return ('#EXTM3U\n' '#EXTINF:-1 ,{0}\n' '{1}live/{2}/{3}/{4}.ts' ''.format( provider_map_class.epg_class().get_channel_name( int(channel_number)), url, username, password, channel_number, ))
def download_chunks_m3u8( cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters, ): authorization_token = requested_query_string_parameters.get( 'authorization_token') channel_number = requested_query_string_parameters.get( 'channel_number') client_uuid = requested_query_string_parameters.get('client_uuid') hostname = requested_query_string_parameters.get('hostname') http_token = requested_query_string_parameters.get('http_token') port = requested_query_string_parameters.get('port') scheme = requested_query_string_parameters.get('scheme') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_channel_number', channel_number) username = Configuration.get_configuration_parameter( '{0}_USERNAME'.format(cls._provider_name.upper())) password = SecurityManager.decrypt_password( Configuration.get_configuration_parameter('{0}_PASSWORD'.format( cls._provider_name.upper()))).decode() requests_session = requests.Session() target_url = '{0}://{1}{2}/live/{3}/{4}/{5}.m3u8'.format( scheme, hostname, port, username, password, channel_number) logger.debug( 'Proxying request\n' 'Source IP => %s\n' 'Requested path => %s\n' ' Parameters\n' ' authorization_token => %s\n' ' channel_number => %s\n' ' client_uuid => %s\n' ' hostname => %s\n' ' port => %s\n' ' scheme => %s\n' 'Target path => %s\n' ' Parameters\n' ' token => %s', client_ip_address, requested_path, authorization_token, channel_number, client_uuid, hostname, port, scheme, target_url, authorization_token, ) response = Utility.make_http_request( requests_session.get, target_url, params={'token': authorization_token}, headers=requests_session.headers, cookies=requests_session.cookies.get_dict(), ) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=True, do_print_content=True)) with cls._do_reduce_hls_stream_delay_lock.reader_lock: if cls._do_reduce_hls_stream_delay: chunks_m3u8 = cls._reduce_hls_stream_delay( response.text, client_uuid, channel_number, number_of_segments_to_keep=2, ) else: chunks_m3u8 = response.text return re.sub( r'/hlsr/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).ts', r'\6.ts?' r'authorization_token=\1&' 'channel_number={0}&' 'client_uuid={1}&' 'hostname={2}&' 'http_token={3}&' r'leaf_directory=\5&' 'port={4}&' 'scheme={5}'.format( channel_number, client_uuid, urllib.parse.quote(hostname), urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), scheme, ), chunks_m3u8, ) else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def download_chunks_m3u8(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): channel_number = requested_query_string_parameters.get( 'channel_number') http_token = requested_query_string_parameters.get('http_token') protocol = requested_query_string_parameters.get('protocol') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) username = Configuration.get_configuration_parameter( '{0}_USERNAME'.format(cls._provider_name.upper())) password = SecurityManager.decrypt_password( Configuration.get_configuration_parameter('{0}_PASSWORD'.format( cls._provider_name.upper()))).decode() if protocol == 'hls': requests_session = requests.Session() target_url = '{0}live/{1}/{2}/{3}.m3u8'.format( ProvidersController.get_provider_map_class( cls._provider_name).constants_class().BASE_URL, username, password, channel_number) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_number => {2}\n' ' client_uuid => {3}\n' ' protocol => {4}\n' 'Target path => {5}'.format( client_ip_address, requested_path, channel_number, client_uuid, protocol, target_url)) response = Utility.make_http_request( requests_session.get, target_url, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace( Utility.assemble_response_from_log_message( response, is_content_text=True, do_print_content=True)) with cls._do_reduce_hls_stream_delay_lock.reader_lock: if cls._do_reduce_hls_stream_delay: chunks_m3u8 = cls._reduce_hls_stream_delay( response.text, client_uuid, channel_number, number_of_segments_to_keep=2) else: chunks_m3u8 = response.text IPTVProxy.set_serviceable_client_parameter( client_uuid, 'last_requested_channel_number', channel_number) match = re.search( r'http://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?', response.request.url) server = match.group(1) port = match.group(2) if len(match.groups()) == 2 else ':80' return re.sub( r'/hlsr/(.*)/(.*)/(.*)/(.*)/(.*)/(.*).ts', r'\6.ts?' r'authorization_token=\1&' 'channel_number={0}&' 'client_uuid={1}&' 'http_token={2}&' r'leaf_directory=\5&' 'port={3}&' 'server={4}'.format( channel_number, client_uuid, urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), urllib.parse.quote(server)), chunks_m3u8) else: logger.error( Utility.assemble_response_from_log_message(response)) response.raise_for_status() elif protocol == 'mpegts': provider_map_class = ProvidersController.get_provider_map_class( cls._provider_name) return '#EXTM3U\n' \ '#EXTINF:-1 ,{0}\n' \ '{1}{2}/{3}/{4}' \ ''.format(provider_map_class.constants_class().BASE_URL, provider_map_class.epg_class().get_channel_name(int(channel_number)), username, password, channel_number)
def download_chunks_m3u8(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): authorization_token = requested_query_string_parameters.get('authorization_token') channel_name = requested_query_string_parameters.get('channel_name') channel_number = requested_query_string_parameters.get('channel_number') http_token = requested_query_string_parameters.get('http_token') port = requested_query_string_parameters.get('port') server = requested_query_string_parameters.get('server') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter(client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) requests_session = requests.Session() target_url = 'http://{0}{1}/{2}/tracks-v1a1/mono.m3u8'.format( server if re.match(r"\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z", server) else '{0}.vaders.tv'.format(server), port if port != ':80' else '', channel_name) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_name => {2}\n' ' channel_number => {3}\n' ' client_uuid => {4}\n' ' port => {5}\n' ' server => {6}\n' 'Target path => {7}\n' ' Parameters\n' ' token => {8}'.format(client_ip_address, requested_path, channel_name, channel_number, client_uuid, port, server, target_url, authorization_token)) response = Utility.make_http_request(requests_session.get, target_url, params={ 'token': authorization_token }, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace(Utility.assemble_response_from_log_message(response, is_content_text=True, do_print_content=True)) with cls._do_reduce_hls_stream_delay_lock.reader_lock: if cls._do_reduce_hls_stream_delay: chunks_m3u8 = cls._reduce_hls_stream_delay(response.text, client_uuid, channel_number, number_of_segments_to_keep=3) else: chunks_m3u8 = response.text IPTVProxy.set_serviceable_client_parameter(client_uuid, 'last_requested_channel_number', channel_number) match = re.search(r'http://(.*)\.vaders\.tv(:\d+)?/(.*)/tracks-v1a1/.*', chunks_m3u8) if match is None: match = re.search(r'http://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?/(.*)/tracks-v1a1/.*', chunks_m3u8) if match is not None: server = match.group(1) port = match.group(2) if match.group(2) is not None and len(match.groups()) == 3 else ':80' channel_name = match.group(3) if len(match.groups()) == 3 else match.group(2) chunks_m3u8 = re.sub('.*/tracks-v1a1/', '', chunks_m3u8) return re.sub(r'.ts\?token=(.*)', r'.ts?' r'authorization_token=\1&' 'channel_name={0}&' 'channel_number={1}&' 'client_uuid={2}&' 'http_token={3}&' 'port={4}&' 'server={5}'.format(urllib.parse.quote(channel_name), channel_number, client_uuid, urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), urllib.parse.quote(server)), chunks_m3u8.replace('/', '_')) else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def download_ts_file(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): authorization_token = requested_query_string_parameters.get('authorization_token') channel_name = requested_query_string_parameters.get('channel_name') channel_number = requested_query_string_parameters.get('channel_number') port = requested_query_string_parameters.get('port') server = requested_query_string_parameters.get('server') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter(client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) requests_session = requests.Session() target_url = 'http://{0}{1}/{2}{3}'.format(server if re.match(r"\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z", server) else '{0}.vaders.tv'.format(server), port if port != ':80' else '', channel_name, re.sub(r'(/.*)?(/.*\.ts)', r'\2', requested_path).replace('_', '/')) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_name => {2}\n' ' channel_number => {3}\n' ' client_uuid => {4}\n' ' port => {5}\n' ' server => {6}\n' 'Target path => {7}\n' ' Parameters\n' ' token => {8}'.format(client_ip_address, requested_path, channel_name, channel_number, client_uuid, port, server, target_url, authorization_token)) response = Utility.make_http_request(requests_session.get, target_url, params={ 'token': authorization_token }, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace(Utility.assemble_response_from_log_message(response, is_content_binary=True)) IPTVProxy.set_serviceable_client_parameter(client_uuid, 'last_requested_ts_file_path', re.sub(r'(/.*)?(/.*\.ts)', r'\2', requested_path).replace('_', '/')[1:]) return response.content else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status()
def download_playlist_m3u8(cls, client_ip_address, client_uuid, requested_path, requested_query_string_parameters): channel_number = requested_query_string_parameters.get('channel_number') http_token = requested_query_string_parameters.get('http_token') protocol = requested_query_string_parameters.get('protocol') IPTVProxy.refresh_serviceable_clients(client_uuid, client_ip_address) IPTVProxy.set_serviceable_client_parameter(client_uuid, 'last_request_date_time_in_utc', datetime.now(pytz.utc)) authorization_token = cls._calculate_token() if protocol == 'hls': requests_session = requests.Session() target_url = 'http://vapi.vaders.tv/play/{0}.m3u8'.format(channel_number) logger.debug('Proxying request\n' 'Source IP => {0}\n' 'Requested path => {1}\n' ' Parameters\n' ' channel_number => {2}\n' ' client_uuid => {3}\n' ' protocol => {4}\n' 'Target path => {5}\n' ' Parameters\n' ' token => {6}'.format(client_ip_address, requested_path, channel_number, client_uuid, protocol, target_url, authorization_token)) response = Utility.make_http_request(requests_session.get, target_url, params={ 'token': authorization_token }, headers=requests_session.headers, cookies=requests_session.cookies.get_dict()) if response.status_code == requests.codes.OK: logger.trace(Utility.assemble_response_from_log_message(response, is_content_text=True, do_print_content=True)) match = re.search(r'http://(.*)\.vaders\.tv(:\d+)?/(.*)/tracks-v1a1/.*', response.text) if match is None: match = re.search(r'http://(.*)\.vaders\.tv(:\d+)?/(.*)/index.m3u8.*', response.request.url) if match is None: match = re.search(r'http://(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?/(.*)/index.m3u8.*', response.request.url) server = match.group(1) port = match.group(2) if match.group(2) is not None and len(match.groups()) == 3 else ':80' channel_name = match.group(3) if len(match.groups()) == 3 else match.group(2) return re.sub(r'tracks-v1a1/mono.m3u8\?token=(.*)', 'chunks.m3u8?' r'authorization_token=\1&' 'channel_name={0}&' 'channel_number={1}&' 'client_uuid={2}&' 'http_token={3}&' 'port={4}&' 'server={5}'.format(urllib.parse.quote(channel_name), channel_number, client_uuid, urllib.parse.quote(http_token) if http_token else '', urllib.parse.quote(port), urllib.parse.quote(server)), re.sub('.*/tracks-v1a1/', 'tracks-v1a1/', response.text)) else: logger.error(Utility.assemble_response_from_log_message(response)) response.raise_for_status() elif protocol == 'mpegts': return '#EXTM3U\n' \ '#EXTINF:-1 ,{0}\n' \ 'http://vapi.vaders.tv/play/{1}.ts?' \ 'token={2}'.format(VaderStreamsEPG.get_channel_name(int(channel_number)), channel_number, authorization_token)