示例#1
0
    def __init__(self, config):
        # Device ID
        device_id_file = open(config.get('Device', 'device_id_file'), 'r')
        self.DEVICE_ID = device_id_file.read().strip()
        device_id_file.close()

        # Playlist URL
        server_url = config.get('Server', 'server_url')
        playlist_server_path = config.get('Server', 'playlist_server_path')
        self.PLAYLIST_URL = urljoin(server_url, playlist_server_path)
        self.LOG.debug('PLAYLIST URL SET: %s', self.PLAYLIST_URL)

        # Playlist file
        playlist_file = config.get('Storage', 'playlist_file')
        playlist_folder = os.path.dirname(playlist_file)
        if not os.path.exists(playlist_folder):
            os.makedirs(playlist_folder)
        self.PLAYLIST_FILEPATH = playlist_file

        # Media folder
        self.MEDIA_FOLDER = config.get('Storage', 'media_folder')
        if not os.path.exists(self.MEDIA_FOLDER):
            os.makedirs(self.MEDIA_FOLDER)

        # Utility for parsing playlist JSON
        self.PLAYLIST_PARSER = PlaylistJsonParser(self.PLAYLIST_FILEPATH)
        playlist_bytes_timeout = int(
            config.get('Client', 'playlist_bytes_timeout'))
        if playlist_bytes_timeout == 0:
            playlist_bytes_timeout = None
        playlist_connection_timeout = int(
            config.get('Client', 'playlist_connection_timeout'))
        if playlist_connection_timeout == 0:
            playlist_connection_timeout = None
        self.PLAYLIST_TIMEOUTS = (playlist_bytes_timeout,
                                  playlist_connection_timeout)

        media_cleaner = MediaCleaner(config, self.PLAYLIST_PARSER)

        # Utility for downloading files
        self.downloader = ChunkedDownloader(server_url, self.DEVICE_ID,
                                            self.MEDIA_FOLDER,
                                            self.PLAYLIST_TIMEOUTS,
                                            media_cleaner)

        self.playlist_id = None
        self.playlist_update_time = None
 def __init__(self, media_cleaner):
     self.HISRA_NET_LOC = urlparse(SERVER_URL).netloc
     self.MEDIA_CLEANER = MediaCleaner()
class ChunkedDownloader(object):
    """
    Handles making requests, reading responses and feeds data to ResumableFileDownload object
    """

    LOG = logging.getLogger(__name__)

    def __init__(self, media_cleaner):
        self.HISRA_NET_LOC = urlparse(SERVER_URL).netloc
        self.MEDIA_CLEANER = MediaCleaner()

    def set_hisra_net_loc(self, server_media_url):
        netloc = urlparse(server_media_url).netloc
        if netloc:
            self.HISRA_NET_LOC = netloc

    def download(self, content):
        url = content.content_uri
        headers = {'Content-Type':Media.VALID_CONTENT_TYPES[content.content_type]}
        if urlparse(url).netloc == self.HISRA_NET_LOC:
            headers['Authorization'] = AUTHORIZATION_HEADER

        response = requests.get(
            url=url,
            timeout=MEDIA_DOWNLOAD_TIMEOUTS,
            stream=True,
            headers=headers,
            verify=SERVER_VERIFY
        )

        if response.status_code != 200:
            raise Exception("Expected 200 response got: %s", response.status_code)

        filename = self.get_filename(response, url)

        md5 = response.headers.get('Content-MD5')
        
        content_length = int(response.headers['Content-Length'])
        if content_length is None:
            raise Exception("Response from {0} had no content-length. Download aborted.".format(url))

        self.MEDIA_CLEANER.clean_media(content_length)

        resumable_download = ResumableFileDownload(url, MEDIA_FOLDER, filename,
                                                   md5, content_length)

        if resumable_download.is_complete():
            response.close()
            return resumable_download.complete_filepath

        bytes_downloaded = resumable_download.bytes_downloaded()

        if bytes_downloaded < content_length:
            if bytes_downloaded > 0:
                response.close()
                self.LOG.debug('Resuming a download with range: %s-%s', bytes_downloaded, resumable_download.expected_size)

                headers['Range'] = 'bytes={0}-{1}'.format(bytes_downloaded, resumable_download.expected_size)
                response = requests.get(url=url,
                                        timeout=MEDIA_DOWNLOAD_TIMEOUTS,
                                        stream=True,
                                        headers=headers,
                                        verify=SERVER_VERIFY)
                if response.status_code != 206:
                    raise Exception("Requested a range(206) but got: %s", response.status_code)

            resumable_download.stream_to_file(response.iter_content)

        response.close()
        resumable_download.download_complete()
        return resumable_download.complete_filepath

    @staticmethod
    def get_filename(response, url):
        filename = response.headers.get('Content-Disposition')
        if filename:
            filename = re.findall("filename=(.+)", response.headers['Content-Disposition'])
            if filename:
                return filename[0].strip()
        else:
            return ChunkedDownloader.slugify(url)

    @staticmethod
    def slugify(value):
        """
        Convert to ASCII. Convert spaces to hyphens.
        Remove characters that aren't alphanumerics, underscores, or hyphens.
        Convert to lowercase. Also strip leading and trailing whitespace.

        original slugify by:
        Copyright (c) Django Software Foundation and individual contributors.
        All rights reserved.

        Redistribution and use in source and binary forms, with or without modification,
        are permitted provided that the following conditions are met:

        1. Redistributions of source code must retain the above copyright notice,
           this list of conditions and the following disclaimer.

        2. Redistributions in binary form must reproduce the above copyright
           notice, this list of conditions and the following disclaimer in the
           documentation and/or other materials provided with the distribution.

        3. Neither the name of Django nor the names of its contributors may be used
           to endorse or promote products derived from this software without
           specific prior written permission.

        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
        ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        """
        value = unicode(value)
        value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
        value = re.sub('[^\w\s-]', '', value).strip().lower()
        return re.sub('[-\s]+', '-', value)