Esempio n. 1
0
    def query(self, languages, title, season=None, episode=None, year=None, video=None):
        items_per_page = 10
        current_page = 1

        # convert list of languages into search string
        langs = '|'.join(
            map(str, [l.titlovi if l != Language.fromietf('sr') else 'cirilica' for l in languages]))
        # set query params
        params = {'prijevod': title, 'jezik': langs}
        is_episode = False
        if season and episode:
            is_episode = True
            params['s'] = season
            params['e'] = episode
        if year:
            params['g'] = year

        # loop through paginated results
        logger.info('Searching subtitles %r', params)
        subtitles = []

        while True:
            # query the server
            try:
                r = self.session.get(self.search_url, params=params, timeout=10)
                r.raise_for_status()

                soup = BeautifulSoup(r.content, 'lxml')

                # number of results
                result_count = int(soup.select_one('.results_count b').string)
            except:
                result_count = None

            # exit if no results
            if not result_count:
                if not subtitles:
                    logger.debug('No subtitles found')
                else:
                    logger.debug("No more subtitles found")
                break

            # number of pages with results
            pages = int(math.ceil(result_count / float(items_per_page)))

            # get current page
            if 'pg' in params:
                current_page = int(params['pg'])

            try:
                sublist = soup.select('section.titlovi > ul.titlovi > li')
                for sub in sublist:
                    # subtitle id
                    sid = sub.find(attrs={'data-id': True}).attrs['data-id']
                    # get download link
                    download_link = self.download_url + sid
                    # title and alternate title
                    match = title_re.search(sub.a.string)
                    if match:
                        _title = match.group('title')
                        alt_title = match.group('altitle')
                    else:
                        continue

                    # page link
                    page_link = self.server_url + sub.a.attrs['href']
                    # subtitle language
                    match = lang_re.search(sub.select_one('.lang').attrs['src'])
                    if match:
                        try:
                            lang = Language.fromtitlovi(match.group('lang'))
                            script = match.group('script')
                            if script:
                                lang.script = Script(script)
                        except ValueError:
                            continue

                    # relase year or series start year
                    match = year_re.search(sub.find(attrs={'data-id': True}).parent.i.string)
                    if match:
                        r_year = int(match.group('year'))
                    # fps
                    match = fps_re.search(sub.select_one('.fps').string)
                    if match:
                        fps = match.group('fps')
                    # releases
                    releases = str(sub.select_one('.fps').parent.contents[0].string)

                    # handle movies and series separately
                    if is_episode:
                        # season and episode info
                        sxe = sub.select_one('.s0xe0y').string
                        r_season = None
                        r_episode = None
                        if sxe:
                            match = season_re.search(sxe)
                            if match:
                                r_season = int(match.group('season'))
                            match = episode_re.search(sxe)
                            if match:
                                r_episode = int(match.group('episode'))

                        subtitle = self.subtitle_class(lang, page_link, download_link, sid, releases, _title,
                                                       alt_title=alt_title, season=r_season, episode=r_episode,
                                                       year=r_year, fps=fps,
                                                       asked_for_release_group=video.release_group,
                                                       asked_for_episode=episode)
                    else:
                        subtitle = self.subtitle_class(lang, page_link, download_link, sid, releases, _title,
                                                       alt_title=alt_title, year=r_year, fps=fps,
                                                       asked_for_release_group=video.release_group)
                    logger.debug('Found subtitle %r', subtitle)

                    # prime our matches so we can use the values later
                    subtitle.get_matches(video)

                    # add found subtitles
                    subtitles.append(subtitle)

            finally:
                soup.decompose()

            # stop on last page
            if current_page >= pages:
                break

            # increment current page
            params['pg'] = current_page + 1
            logger.debug('Getting page %d', params['pg'])

        return subtitles
Esempio n. 2
0
 def test_ne_with_script(self):
     self.assertNotEqual(Language('srp', script='Latn'), Language('srp', script=Script('Cyrl')))
Esempio n. 3
0
 def test_ne_with_country_and_script(self):
     self.assertNotEqual(Language('srp', 'SR', 'Latn'), Language('srp', Country('SR'), Script('Cyrl')))
Esempio n. 4
0
 def test_script(self):
     self.assertEqual(Language('srp', script='Latn').script, Script('Latn'))
     self.assertEqual(Language('srp', script=Script('Cyrl')).script, Script('Cyrl'))
Esempio n. 5
0
 def test_eq_with_script(self):
     self.assertEqual(Language('srp', script='Latn'), Language('srp', script=Script('Latn')))
Esempio n. 6
0
 def test_fromietf_country_script(self):
     language = Language.fromietf('fra-FR-Latn')
     self.assertEqual(language.alpha3, 'fra')
     self.assertEqual(language.country, Country('FR'))
     self.assertEqual(language.script, Script('Latn'))
Esempio n. 7
0
 def test_fromietf_alpha2_language(self):
     language = Language.fromietf('fr-Latn')
     self.assertEqual(language.alpha3, 'fra')
     self.assertIsNone(language.country)
     self.assertEqual(language.script, Script('Latn'))
Esempio n. 8
0
 def test_pickle(self):
     self.assertEqual(pickle.loads(pickle.dumps(Script('Latn'))), Script('Latn'))
Esempio n. 9
0
 def test_hash(self):
     self.assertEqual(hash(Script('Hira')), hash('Hira'))
Esempio n. 10
0
 def test_ne(self):
     self.assertNotEqual(Script('Cyrl'), Script('Latn'))
Esempio n. 11
0
 def test_eq(self):
     self.assertEqual(Script('Latn'), Script('Latn'))
Esempio n. 12
0
 def test_wrong_script(self):
     self.assertRaises(ValueError, lambda: Script('Azer'))