From 3bb55c61087acba06ef800c07555dad465ac5524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Antunes?= Date: Wed, 5 Aug 2020 16:03:37 -0300 Subject: [PATCH] Fix base path when query string has slashes --- m3u8/mixins.py | 6 +++++- tests/playlists.py | 15 +++++++++++++++ tests/test_model.py | 7 +++++++ tests/test_parser.py | 2 -- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/m3u8/mixins.py b/m3u8/mixins.py index 33f228c4..acdacee1 100644 --- a/m3u8/mixins.py +++ b/m3u8/mixins.py @@ -32,7 +32,11 @@ def absolute_uri(self): def base_path(self): if self.uri is None: return None - return os.path.dirname(self.uri) + return os.path.dirname(self.get_path_from_uri()) + + def get_path_from_uri(self): + """Some URIs have a slash in the query string.""" + return self.uri.split("?")[0] @base_path.setter def base_path(self, newbase_path): diff --git a/tests/playlists.py b/tests/playlists.py index ee9590bd..61bb33ae 100755 --- a/tests/playlists.py +++ b/tests/playlists.py @@ -1045,4 +1045,19 @@ #EXT-X-PART:DURATION=1,URI="filePart271.c.ts" ''' +PLAYLIST_WITH_SLASH_IN_QUERY_STRING = ''' +#EXTM3U +#EXT-X-VERSION:3 +#EXT-X-TARGETDURATION:5 +#EXT-X-MEDIA-SEQUENCE:10599 +#EXT-X-PROGRAM-DATE-TIME:2020-08-05T13:51:49.000+00:00 +#EXTINF:5.0000, +testvideo-1596635509-4769390994-a0e3087c.ts?hdntl=exp=1596678764~acl=/*~data=hdntl~hmac=12345& +#EXTINF:5.0000, +testvideo-1596635514-4769840994-a0e00878.ts?hdntl=exp=1596678764~acl=/*~data=hdntl~hmac=12345& +#EXTINF:5.0000, +testvideo-1596635519-4770290994-a0e5087d.ts?hdntl=exp=1596678764~acl=/*~data=hdntl~hmac=12345& +#EXTINF:5.0000, +''' + del abspath, dirname, join diff --git a/tests/test_model.py b/tests/test_model.py index c4aab902..5e516076 100755 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -35,6 +35,13 @@ def dst(self, dt): utc = UTC() +def test_base_path_playlist_with_slash_in_query_string(): + playlist = m3u8.M3U8( + playlists.PLAYLIST_WITH_SLASH_IN_QUERY_STRING, + base_path='http://testvideo.com/foo' + ) + assert playlist.segments[0].uri == 'http://testvideo.com/foo/testvideo-1596635509-4769390994-a0e3087c.ts?hdntl=exp=1596678764~acl=/*~data=hdntl~hmac=12345&' + def test_target_duration_attribute(): obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST) mock_parser_data(obj, {'targetduration': '1234567'}) diff --git a/tests/test_parser.py b/tests/test_parser.py index 837324b4..8837048a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -485,8 +485,6 @@ def test_gap(): def test_gap_in_parts(): data = m3u8.parse(playlists.GAP_IN_PARTS_PLAYLIST) - print(data['segments'][0]['parts']) - assert data['segments'][0]['parts'][0]['gap_tag'] is None assert data['segments'][0]['parts'][0].get('gap', None) is None assert data['segments'][0]['parts'][1]['gap_tag'] is None