Skip to content

Commit

Permalink
Merge pull request #129 from colde/master
Browse files Browse the repository at this point in the history
Add the ability to set URI's when loading from a string
  • Loading branch information
leandromoreira authored Dec 6, 2018
2 parents db07d2a + ab1e405 commit 979ec01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions m3u8/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
'Segment', 'loads', 'load', 'parse', 'ParseError')


def loads(content):
def loads(content, uri=None):
'''
Given a string with a m3u8 content, returns a M3U8 object.
Optionally parses a uri to set a correct base_uri on the M3U8 object.
Raises ValueError if invalid content
'''
return M3U8(content)

if uri is None:
return M3U8(content)
else:
base_uri = _parsed_url(uri)
return M3U8(content, base_uri=base_uri)


def load(uri, timeout=None, headers={}):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def test_absolute_uri_should_handle_empty_base_uri_path():
assert 'http://example.com/key.bin' == key.absolute_uri


def test_presence_of_base_uri_if_provided_when_loading_from_string():
with open(playlists.RELATIVE_PLAYLIST_FILENAME) as f:
content = f.read()
obj = m3u8.loads(content, uri='http://media.example.com/path/playlist.m3u8')
assert obj.base_uri == 'http://media.example.com/path/'


def test_raise_timeout_exception_if_timeout_happens_when_loading_from_uri():
try:
obj = m3u8.load(playlists.TIMEOUT_SIMPLE_PLAYLIST_URI, timeout=1)
Expand Down

0 comments on commit 979ec01

Please sign in to comment.