Skip to content

Commit

Permalink
Issue 387: Support absolute Windows paths (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles authored Oct 11, 2024
1 parent e9e185a commit b6fd525
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion m3u8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def load(
Retrieves the content from a given URI and returns a M3U8 object.
Raises ValueError if invalid content or IOError if request fails.
"""
if urlsplit(uri).scheme:
base_uri_parts = urlsplit(uri)
if base_uri_parts.scheme and base_uri_parts.netloc:
content, base_uri = http_client.download(uri, timeout, headers, verify_ssl)
return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)
else:
Expand Down
6 changes: 4 additions & 2 deletions m3u8/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def absolute_uri(self):
return None

ret = urljoin(self.base_uri, self.uri)
if self.base_uri and (not urlsplit(self.base_uri).scheme):
return ret
if self.base_uri:
base_uri_parts = urlsplit(self.base_uri)
if (not base_uri_parts.scheme) and (not base_uri_parts.netloc):
return ret

if not urlsplit(ret).scheme:
raise ValueError("There can not be `absolute_uri` with no `base_uri` set")
Expand Down
10 changes: 10 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,5 +1576,15 @@
content-131.jpg
"""

WINDOWS_PLAYLIST = r"""\
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:55119
#EXT-X-PROGRAM-DATE-TIME:2024-10-11T09:53:30.001Z
#EXTINF:9.600,
C:\HLS Video\test1.ts
"""

del abspath, dirname, join
16 changes: 15 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import os
import socket
import urllib.parse
import m3u8
import unittest.mock

import pytest

import m3u8
import playlists


Expand Down Expand Up @@ -146,3 +149,14 @@ def test_raise_timeout_exception_if_timeout_happens_when_loading_from_uri():
assert True
else:
assert False


def test_windows_paths():
file_path = "C:\\HLS Video\test.m3ui8"
with unittest.mock.patch("builtins.open") as mock_open:
mock_open.return_value.__enter__.return_value.read.return_value = (
playlists.WINDOWS_PLAYLIST
)
obj = m3u8.load(file_path)
assert obj.segments[0].uri == "C:\\HLS Video\\test1.ts"
assert obj.segments[0].absolute_uri == "C:\\HLS Video\\test1.ts"

0 comments on commit b6fd525

Please sign in to comment.