Ejemplo n.º 1
0
def test_subtitle_is_valid_valid(monkeypatch):
    subtitle = Subtitle(Language('fra'))
    text = ('1\n'
            '00:00:20,000 --> 00:00:24,400\n'
            'En réponse à l\'augmentation de la criminalité\n'
            'dans certains quartiers,\n')
    monkeypatch.setattr(Subtitle, 'text', text)
    assert subtitle.is_valid() is True
Ejemplo n.º 2
0
def test_subtitle_is_valid_invalid(monkeypatch):
    subtitle = Subtitle(Language('fra'))
    text = ('1\n'
            '00:00:20,000 --> 00:00:24,400\n'
            'En réponse à l\'augmentation de la criminalité\n'
            'dans certains quartiers,\n\n')
    text += 'This line shouldn\'t be here'
    monkeypatch.setattr(Subtitle, 'text', text)
    assert subtitle.is_valid() is False
Ejemplo n.º 3
0
def test_save_subtitles_single_directory_encoding(movies, tmpdir):
    subtitle = Subtitle(Language('jpn'))
    subtitle.content = u'ハローワールド'.encode('shift-jis')
    subtitle_pt_br = Subtitle(Language('por', 'BR'))
    subtitle_pt_br.content = b'Some brazilian content'
    subtitles = [subtitle, subtitle_pt_br]

    save_subtitles(movies['man_of_steel'], subtitles, single=True, directory=str(tmpdir), encoding='utf-8')

    # first subtitle only and correctly encoded
    path = os.path.join(str(tmpdir), os.path.splitext(os.path.split(movies['man_of_steel'].name)[1])[0] + '.srt')
    assert os.path.exists(path)
    assert io.open(path, encoding='utf-8').read() == u'ハローワールド'
Ejemplo n.º 4
0
def test_save_subtitles(movies, tmpdir, monkeypatch):
    monkeypatch.chdir(str(tmpdir))
    tmpdir.ensure(movies['man_of_steel'].name)
    subtitle_no_content = Subtitle(Language('eng'))
    subtitle = Subtitle(Language('fra'))
    subtitle.content = b'Some content'
    subtitle_other = Subtitle(Language('fra'))
    subtitle_other.content = b'Some other content'
    subtitle_pt_br = Subtitle(Language('por', 'BR'))
    subtitle_pt_br.content = b'Some brazilian content'
    subtitles = [subtitle_no_content, subtitle, subtitle_other, subtitle_pt_br]

    save_subtitles(movies['man_of_steel'], subtitles)

    # subtitle without content is skipped
    path = os.path.join(str(tmpdir), os.path.splitext(movies['man_of_steel'].name)[0] + '.en.srt')
    assert not os.path.exists(path)

    # first subtitle with language is saved
    path = os.path.join(str(tmpdir), os.path.splitext(movies['man_of_steel'].name)[0] + '.fr.srt')
    assert os.path.exists(path)
    assert io.open(path, 'rb').read() == b'Some content'

    # ietf language in path
    path = os.path.join(str(tmpdir), os.path.splitext(movies['man_of_steel'].name)[0] + '.pt-BR.srt')
    assert os.path.exists(path)
    assert io.open(path, 'rb').read() == b'Some brazilian content'
Ejemplo n.º 5
0
    def query(self, name, season, episode, keywords, filepath, languages):
        sublinks = []
        # get the show id
        show_name = name.lower()
        if show_name in self.exceptions:  # get it from exceptions
            show_id = self.exceptions[show_name]
        elif show_name in self.showids:  # get it from cache
            show_id = self.showids[show_name]
        else:  # retrieve it
            show_name_encoded = show_name
            if isinstance(show_name_encoded, unicode):
                show_name_encoded = show_name_encoded.encode('utf-8')
            show_id_url = '%sGetShowByName/%s' % (self.server_url, urllib2.quote(show_name_encoded))
            self.logger.debug(u'Retrieving show id from web at %s' % show_id_url)
            page = urllib2.urlopen(show_id_url)
            dom = minidom.parse(page)
            if not dom or len(dom.getElementsByTagName('showid')) == 0:  # no proper result
                page.close()
                return []
            show_id = dom.getElementsByTagName('showid')[0].firstChild.data
            self.showids[show_name] = show_id
            with self.lock:
                f = open(self.showid_cache, 'w')
                self.logger.debug(u'Writing showid %s to cache file' % show_id)
                pickle.dump(self.showids, f)
                f.close()
            page.close()

        # get the subs for the show id we have
        for language in languages:
            subs_url = '%sGetAllSubsFor/%s/%s/%s/%s' % (self.server_url, show_id, season, episode, language)
            self.logger.debug(u'Getting subtitles at %s' % subs_url)
            page = urllib2.urlopen(subs_url)
            dom = minidom.parse(page)
            page.close()
            for sub in dom.getElementsByTagName('result'):
                sub_filename = sub.getElementsByTagName('filename')[0].firstChild.data
                if not sub_filename.endswith(tuple(EXTENSIONS)):
                    sub_filename = sub_filename + EXTENSIONS[0]
                subtitle = Subtitle.factory(sub_filename)
                subtitle.link = sub.getElementsByTagName('downloadlink')[0].firstChild.data
                subtitle.path = self.getSubtitlePath(filepath, language)
                subtitle.plugin = self.__class__.__name__
                subtitle.language = language
                sublinks.append(subtitle)
        sublinks.sort(self._cmpReleaseGroup)
        return sublinks
Ejemplo n.º 6
0
def test_subtitle_is_valid_no_content():
    subtitle = Subtitle(Language('fra'))
    assert subtitle.is_valid() is False
Ejemplo n.º 7
0
def test_subtitle_text():
    subtitle = Subtitle(Language('eng'))
    subtitle.content = b'Some ascii text'
    assert subtitle.text == 'Some ascii text'