Skip to content

Commit

Permalink
loader: load from url and file, fixes #7, #8
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsobreira committed May 2, 2012
1 parent 5ad6f75 commit c4f1efd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
*.egg-info
tests/server.stdout
10 changes: 9 additions & 1 deletion m3u8/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from urllib2 import urlopen

from m3u8.model import M3U8
from m3u8.parser import parse

Expand All @@ -15,4 +17,10 @@ def load(uri):
Retrieves the content from a given URI and returns a M3U8 object.
Raises ValueError if invalid content or IOError if request fails.
'''
return M3U8()
if uri.startswith('http://') or uri.startswith('https://'):
content = urlopen(uri).read().strip()
else:
with open(uri) as fileobj:
content = fileobj.read().strip()

return M3U8(content)
16 changes: 15 additions & 1 deletion runtests
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#!/bin/bash

test_server_stdout=tests/server.stdout

function install_deps {
pip install pytest
pip install pytest bottle
}

function start_server {
rm -f ${test_server_stdout}
python tests/m3u8server.py >${test_server_stdout} 2>&1 &
}

function stop_server {
ps ax | grep m3u8server.py | grep -v grep | cut -d ' ' -f 1 | xargs kill
echo "Test server stdout on ${test_server_stdout}"
}

function run {
Expand All @@ -10,7 +22,9 @@ function run {

function main {
install_deps
start_server
run
stop_server
}

if [ -z "$1" ]; then
Expand Down
21 changes: 21 additions & 0 deletions tests/m3u8server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Test server to deliver stubed M3U8s
'''
from os.path import dirname, abspath, join

from bottle import route, run, response
import bottle

playlists = abspath(join(dirname(__file__), 'playlists'))

@route('/simple.m3u8')
def simple():
response.set_header('Content-Type', 'application/vnd.apple.mpegurl')
return m3u8_file('simple-playlist.m3u8')

def m3u8_file(filename):
with open(join(playlists, filename)) as fileobj:
return fileobj.read().strip()

bottle.debug = True
run(host='localhost', port=8112)
10 changes: 10 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from os.path import dirname, abspath, join

TEST_HOST = 'http://localhost:8112'

SIMPLE_PLAYLIST = '''
#EXTM3U
#EXT-X-TARGETDURATION:5220
Expand All @@ -6,6 +10,10 @@
#EXT-X-ENDLIST
'''

SIMPLE_PLAYLIST_FILENAME = abspath(join(dirname(__file__), 'playlists/simple-playlist.m3u8'))

SIMPLE_PLAYLIST_URI = TEST_HOST + '/simple.m3u8'

SLIDING_WINDOW_PLAYLIST = '''
#EXTM3U
#EXT-X-TARGETDURATION:8
Expand Down Expand Up @@ -74,3 +82,5 @@
http://media.example.com/entire.ts
#EXT-X-ENDLIST
'''

del abspath, dirname, join
5 changes: 5 additions & 0 deletions tests/playlists/simple-playlist.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#EXTM3U
#EXT-X-TARGETDURATION:5220
#EXTINF:5220,
http://media.example.com/entire.ts
#EXT-X-ENDLIST
20 changes: 17 additions & 3 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import m3u8
from playlists import *

def test_loads_should_create_m3u8_object_from_string():
m3u8_obj = m3u8.loads(SIMPLE_PLAYLIST)
assert 5220 == m3u8_obj.target_duration
def test_loads_should_create_object_from_string():
obj = m3u8.loads(SIMPLE_PLAYLIST)
assert isinstance(obj, m3u8.M3U8)
assert 5220 == obj.target_duration
assert 'http://media.example.com/entire.ts' == obj.segments[0].uri

def test_load_should_create_object_from_file():
obj = m3u8.load(SIMPLE_PLAYLIST_FILENAME)
assert isinstance(obj, m3u8.M3U8)
assert 5220 == obj.target_duration
assert 'http://media.example.com/entire.ts' == obj.segments[0].uri

def test_load_should_create_object_from_uri():
obj = m3u8.load(SIMPLE_PLAYLIST_URI)
assert isinstance(obj, m3u8.M3U8)
assert 5220 == obj.target_duration
assert 'http://media.example.com/entire.ts' == obj.segments[0].uri

0 comments on commit c4f1efd

Please sign in to comment.