Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 387: Support absolute Windows paths #388

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, this should be:
file_path = "C:\\HLS Video\\test.m3u8"

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"
Loading