Пример #1
0
    def download_subtitle(self, language, format='xml'):
        """
        Downloads the subtitle for ``language`` where ``language`` is
        a language code of 2 chars (e.g., *en*) and returns a temporary
        file the subtitles were downloaded to.

        The ``format`` parameter specifies the file format the subtitles
        shall be returned in. Currently supported are

        * 'xml': (default) returns the original data received from YouTube.
        * 'mpl2': returns the subtitles in MPL2 format.
        """
        if language in self._subtitles:
            # All work done, just returned the cached subtitles.
            return self._subtitles[language]

        tempfile = NamedTempfile(self.video_id+'-subtitle-'+language+'.xml')
        if tempfile.isempty():
            subtitle_url = SUBTITLE_GET_URL.format(video_id=self.video_id, language_code=language)
            with tempfile:
                tempfile.file.write(urllib2.urlopen(subtitle_url).read())
            if tempfile.isempty():
                raise YouTubeError("Subtitle for video '{0}' not available in '{1}'"\
                                   .format(self.video_id, language))

        if format == 'xml':
            return tempfile.name
        #elif format == 'json':
        #    return self._subtitle_file_to_json(tempfile.name, language)
        elif format == 'mpl2':
            return self._subtitle_file_as_mpl2(tempfile.name, language)
        else:
            raise TypeError("Unknown subtitle format '%s'" % format)
Пример #2
0
    def request_video_info(self):
        """
        Sends a HTTP request to the YouTube servers asking for additional
        information about the video.

        Note that this method has to be called before accessing ``video_info`` and some
        other attributes that depend on it (like ``stream_urls`` or ``thumbnail_url``)!
        """
        if hasattr(self, '_video_info'):
            # All work already done, do nothing.
            return
        tempfile = NamedTempfile(self.video_id+'-info')
        if tempfile.isempty():
            raw_data = urllib2.urlopen(VIDEO_INFO_URL.format(video_id=self.video_id)).read()
            with tempfile:
                tempfile.file.write(raw_data)
        else:
            with tempfile:
                raw_data = tempfile.file.read()

        info = urlparse.parse_qs(raw_data)
        if info['status'][0] != 'ok':
            try:
                video_title = "('" + self.title + "')"
            except AttributeError:
                video_title = ''
            exc = YouTubeError(
                "Could not get video information about video '%s' %s: %s" % (
                    self.video_id, video_title, info['reason'][0]
            ))
            exc.reason = info['reason'][0]
            raise exc

        else:
            self._video_info = info
Пример #3
0
    def _thumbnail_path(self):
        if self.thumbnail_url is None:
            return None

        tempfile = NamedTempfile('thumbnail-'+self.video_id)
        if tempfile.isempty():
            # download the thumbnail if not already done so.
            with tempfile:
                tempfile.file.write(urllib2.urlopen(self.thumbnail_url).read())
        return tempfile.name
Пример #4
0
 def _subtitle_file_as_mpl2(self, xmlfile, language):
     # see http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html
     tempfile = NamedTempfile(self.video_id+'-subtitle-'+language+'.mpl2')
     if not tempfile.isempty():
         return tempfile.name
     with tempfile:
         xmltree = parse_xml(xmlfile).getroot()
         for element in xmltree:
            start = int(float(element.attrib['start'])*1000)
            end = start + int(float(element.attrib['dur'])*1000)
            text = element.text.replace('\n', '|')
            tempfile.file.write('[{0}][{1}]{2}\n'.format(start, end, text))
     return tempfile.name