Skip to content

Commit

Permalink
Merge branch 'globocom:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtai9710 authored Oct 30, 2024
2 parents 70b96c6 + b6fd525 commit d115e72
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
- uses: astral-sh/ruff-action@v1.1.0

ruff_format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
- uses: astral-sh/ruff-action@v1.1.0
with:
args: format --check --diff
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
5 changes: 3 additions & 2 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ def __str__(self):


class RenditionReport(BasePathMixin):
def __init__(self, base_uri, uri, last_msn, last_part=None):
def __init__(self, base_uri, uri, last_msn=None, last_part=None):
self.base_uri = base_uri
self.uri = uri
self.last_msn = last_msn
Expand All @@ -1303,7 +1303,8 @@ def __init__(self, base_uri, uri, last_msn, last_part=None):
def dumps(self):
report = []
report.append("URI=" + quoted(self.uri))
report.append("LAST-MSN=" + str(self.last_msn))
if self.last_msn is not None:
report.append("LAST-MSN=" + str(self.last_msn))
if self.last_part is not None:
report.append("LAST-PART=" + str(self.last_part))

Expand Down
62 changes: 62 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,58 @@
#EXT-X-RENDITION-REPORT:URI="../4M/waitForMSN.php",LAST-MSN=273,LAST-PART=3
"""

LOW_LATENCY_OMITTED_ATTRIBUTES = """
#EXTM3U
#EXT-X-VERSION:7
#EXT-X-TARGETDURATION:2
#EXT-X-SERVER-CONTROL:CAN-BLOCK-RELOAD=YES,PART-HOLD-BACK=2.171
#EXT-X-PART-INF:PART-TARGET=1.034
#EXT-X-MAP:URI="init_data.m4s"
#EXT-X-MEDIA-SEQUENCE:6342
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:31:57.350+00:00
#EXTINF:2,
chunk_6342.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:31:59.350+00:00
#EXTINF:2,
chunk_6343.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:01.350+00:00
#EXTINF:2,
chunk_6344.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:03.350+00:00
#EXTINF:2,
chunk_6345.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:05.350+00:00
#EXTINF:2,
chunk_6346.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:07.350+00:00
#EXTINF:2,
chunk_6347.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:09.350+00:00
#EXTINF:2,
chunk_6348.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:11.350+00:00
#EXT-X-PART:DURATION=1,URI="chunk_6349.0.m4s",INDEPENDENT=YES
#EXT-X-PART:DURATION=1,URI="chunk_6349.1.m4s"
#EXTINF:2,
chunk_6349.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:13.350+00:00
#EXT-X-PART:DURATION=1,URI="chunk_6350.0.m4s",INDEPENDENT=YES
#EXT-X-PART:DURATION=1,URI="chunk_6350.1.m4s"
#EXTINF:2,
chunk_6350.m4s
#EXT-X-PROGRAM-DATE-TIME:2024-09-24T15:32:15.350+00:00
#EXT-X-PART:DURATION=1,URI="chunk_6351.0.m4s?skid=default&signature=NjZmYzFjODBfYzY3NGRlODc4Zjk1MjM1OGNmMmE3NjhiM2E2NTUyNGI1Y2JiYzMyZDU5YTFjNTQzODI2MjI5ZTllZmNhMDZmNQ==&zone=1",INDEPENDENT=YES
#EXT-X-PART:DURATION=1,URI="chunk_6351.1.m4s?skid=default&signature=NjZmYzFjODBfMDcwMjA0OTZlMTE3Y2RiN2VjOGY2YjE2MDE2NTAwZThlN2Q3NjUyZTAzM2YxZTZlZmFlZTg1ZThmZWEyZmQ4Ng==&zone=1"
#EXTINF:2,
chunk_6350.m4s
#EXT-X-PRELOAD-HINT:TYPE=PART,URI="chunk_6352.0.m4s?skid=default&signature=NjZmYzFjODBfMzkyZmNiOWNjNmY5N2EwN2QwNTU3YTA3M2Q0ZTRlMWU2YjliZDMyM2Y0MTRmYTY5OTdhODIyMmIwY2QwOWY1NQ==&zone=1"
#EXT-X-RENDITION-REPORT:URI="rendition_1.m3u8"
#EXT-X-RENDITION-REPORT:URI="rendition_2.m3u8"
#EXT-X-RENDITION-REPORT:URI="rendition_3.m3u8"
#EXT-X-RENDITION-REPORT:URI="rendition_4.m3u8"
#EXT-X-RENDITION-REPORT:URI="rendition_5.m3u8"
"""

LOW_LATENCY_WITH_PRELOAD_AND_BYTERANGES_PLAYLIST = """
#EXTM3U
#EXTINF:4.08,
Expand Down Expand Up @@ -1524,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"
8 changes: 8 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,14 @@ def test_ll_playlist():
assert obj.preload_hint.base_uri == "http://localhost/test_base_uri"


def test_ll_playlist_omitted_attributes():
# RFC 8216 4.4.5.4 states that even the required attribute LAST-MSN
# can be omitted in certain conditions.
obj = m3u8.M3U8(playlists.LOW_LATENCY_OMITTED_ATTRIBUTES)
text = obj.dumps()
assert '#EXT-X-RENDITION-REPORT:URI="rendition_1.m3u8"\n' in text


def test_add_rendition_report_to_playlist():
obj = m3u8.M3U8()

Expand Down

0 comments on commit d115e72

Please sign in to comment.