File size: 3,251 Bytes
27867f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# coding: utf-8
# Copyright 2014 Globo.com Player authors. All rights reserved.
# Use of this source code is governed by a MIT License
# license that can be found in the LICENSE file.

import logging
import os
import sys

from .httpclient import DefaultHTTPClient, _parsed_url
from .model import (M3U8, Segment, SegmentList, PartialSegment,
                        PartialSegmentList, Key, Playlist, IFramePlaylist,
                        Media, MediaList, PlaylistList, Start,
                        RenditionReport, RenditionReportList, ServerControl,
                        Skip, PartInformation, PreloadHint, DateRange,
                        DateRangeList)
from .parser import parse, is_url, ParseError


__all__ = ('M3U8', 'Segment', 'SegmentList', 'PartialSegment',
            'PartialSegmentList', 'Key', 'Playlist', 'IFramePlaylist',
            'Media', 'MediaList', 'PlaylistList', 'Start', 'RenditionReport',
            'RenditionReportList', 'ServerControl', 'Skip', 'PartInformation',
            'PreloadHint' 'DateRange', 'DateRangeList', 'loads', 'load',
            'parse', 'ParseError')

LOGGER = None

def loads(content, uri=None, custom_tags_parser=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
    '''
    global LOGGER
    if LOGGER is None:
        LOGGER = logging.getLogger(__name__)
    if not content.startswith('#EXTM3U'):
        LOGGER.warning('INVALID m3u format: #EXTM3U missing {}'.format(uri))
        return None
    if uri is None:
        return M3U8(content, custom_tags_parser=custom_tags_parser)
    else:
        base_uri = _parsed_url(uri)
        return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)


def load(uri, timeout=9, headers={}, custom_tags_parser=None, http_client=DefaultHTTPClient(), verify_ssl=True, http_session=None):
    '''
    Retrieves the content from a given URI and returns a M3U8 object.
    Raises ValueError if invalid content or IOError if request fails.
    '''
    global LOGGER
    if LOGGER is None:
        LOGGER = logging.getLogger(__name__)
    if is_url(uri):
        content, base_uri = http_client.download(uri, timeout, headers, verify_ssl, http_session)
        if content is None:
            LOGGER.warning('Unable to obtain m3u file {}'.format(uri))
            return None        
        if not content.startswith('#EXTM3U'):
            LOGGER.warning('INVALID m3u format: #EXTM3U missing {}'.format(uri))
            return None
        return M3U8(content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)
    else:
        return _load_from_file(uri, custom_tags_parser)


def _load_from_file(uri, custom_tags_parser=None):
    global LOGGER
    if LOGGER is None:
        LOGGER = logging.getLogger(__name__)
    with open(uri, encoding='utf8') as fileobj:
        raw_content = fileobj.read().strip()
    base_uri = os.path.dirname(uri)
    if not raw_content.startswith('#EXTM3U'):
        LOGGER.warning('INVALID m3u format: #EXTM3U missing {}'.format(uri))
        return None
    return M3U8(raw_content, base_uri=base_uri, custom_tags_parser=custom_tags_parser)