コード例 #1
0
    def __init__(self):

        media_cleaner = MediaCleaner()

        # Utility for downloading files
        self.downloader = ChunkedDownloader(media_cleaner)

        self.playlist_id = None
        self.playlist_update_time = None
コード例 #2
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
コード例 #3
0
class PlaylistManager(object):
    LOG = logging.getLogger(__name__)

    def __init__(self):

        media_cleaner = MediaCleaner()

        # Utility for downloading files
        self.downloader = ChunkedDownloader(media_cleaner)

        self.playlist_id = None
        self.playlist_update_time = None

    def fetch_local_playlist(self):
        try:
            local_playlist = playlist_utils.get_stored_playlist()
            # We do not download files here because it would defeat the purpose.
            # We trust the files have either been downloaded
            # or we start to download them the next time we download a playlist
            # this way if the playlist has changed we don't unnecessarily download old files
            return local_playlist
        except:
            self.LOG.info('No locally stored playlist')
            return []

    def fetch_remote_playlist_data(self):
        headers = {'Authorization': AUTHORIZATION_HEADER}
        self.LOG.debug('Fetching remote playlist from %s' % PLAYLIST_URL)
        response = requests.get(
                url=PLAYLIST_URL,
                timeout=PLAYLIST_TIMEOUTS,
                stream=False,
                headers=headers,
                verify=SERVER_VERIFY)

        if response.status_code == 200:
            self.LOG.debug('Fetched data: %s' % response.content)
            return response.content
        raise Exception('Wrong status from server while fetching playlist: %s' % response.status_code)

    def fetch_playlist(self):
        pl_data = self.fetch_remote_playlist_data()
        if pl_data is None:
            raise Exception("No playlist data received from server.")
        self.LOG.debug('Parsing playlist')
        media_url, playlist, playlist_id, playlist_update_time = playlist_utils.parse_playlist(pl_data)
        self.LOG.debug('Playlist with id %s and update time %s parsed', playlist_id, playlist_update_time)

        # check if the playlist is already in use by the device
        # Note that this is always false when we have just started the app
        # This is by design to make sure we have all the files
        if self.playlist_id == playlist_id and self.playlist_update_time == playlist_update_time:
            raise PlaylistNotChanged("Playlist data has not changed since last downloaded")

        self.download_playlist_files(playlist, media_url)

        playlist_utils.save_playlist_to_file(playlist)

        # save new playlist id and update time after we have finished all the tasks
        # that need to be performed when we download a playlist
        self.playlist_id = playlist_id
        self.playlist_update_time = playlist_update_time

        return playlist, playlist_id, playlist_update_time

    # NOTE: Also sets content_uri to local path to media file
    def download_playlist_files(self, playlist, own_server_media_url):
        self.downloader.set_hisra_net_loc(own_server_media_url)
        for content in playlist:
            if content.content_type == Media.WEB_PAGE:
                continue  # Web pages are not downloaded
            try:
                content.content_uri = self.downloader.download(content)
            except Exception, e:
                self.LOG.debug('Failed to download content, %s %s', content.content_uri, str(e))
                raise