Example #1
0
    def _sort_by_ratio(self, results, query):
        """ Sort results by ratio between result and search params. """
        ratio_table = []
        qry_imdb = query.get('imdbid')
        for result in filter(lambda res: res._result_dict, results):
            ratio = 0.0
            if qry_imdb and qry_imdb == result._result_dict.get('imdbid'):
                ratio = 1.0
            elif query.get('title'):
                ratio_a = string_similarity_ratio(
                    query.title, result._result_dict.get('original_title')
                )
                ratio_b = string_similarity_ratio(
                    query.title, result._result_dict['title']
                )
                ratio = max(ratio_a or 0.0, ratio_b or 0.0)

                # TODO: Fix first time wrong results. Maybe sort by year?
                if query.get('year') and result._result_dict['year']:
                    a, b = query.get('year'), result._result_dict['year']

                    penalty = math.sqrt(1 - (abs(a - b) / max(a, b)))
                    ratio *= penalty

            ratio_entry = {'result': result, 'ratio': ratio}
            ratio_table.append(ratio_entry)

        ratio_table.sort(key=lambda x: x['ratio'], reverse=True)
        return [res['result'] for res in ratio_table]
Example #2
0
    def _parse_search_module(self, response, search_params):
        """ Parse a search response. Find high prio result. Build urllist."""
        # create similarity matrix for title, check agains german and original
        # title, higher ratio wins
        similarity_map = []
        for response in response['eintrag']:
            # Get the title with the highest similarity ratio:
            ratio = 0.0
            if '-Serie' not in response['titel_de']:
                for title_key in ['titel_de', 'titel_orig']:
                    ratio = max(
                        ratio,
                        string_similarity_ratio(
                            response[title_key],
                            search_params.title
                        )
                    )
                similarity_map.append(
                    {'ofdbid': response['id'], 'ratio': ratio}
                )

        # sort by ratio, generate ofdbid list with requestet item count
        similarity_map.sort(
            key=lambda value: value['ratio'],
            reverse=True
        )
        item_count = min(len(similarity_map), search_params.amount)
        matches = [item['ofdbid'] for item in similarity_map[:item_count]]
        return self._common.movieids_to_urllist(matches)
Example #3
0
    def _parse_search_response(self, response, search_params):
        similarity_map = []
        for item in response['results']:
            ratio = string_similarity_ratio(
                item['name'],
                search_params.name
            )
            similarity_map.append({'tmdbid': item['id'], 'ratio': ratio})

        similarity_map.sort(key=lambda value: value['ratio'], reverse=True)
        item_count = min(len(similarity_map), search_params.amount)
        movie_ids = [item['tmdbid'] for item in similarity_map[:item_count]]
        return self._config.build_person_urllist(movie_ids, search_params)
Example #4
0
 def _parse_search_module(self, result, search_params):
     similarity_map = []
     for result in result['Search']:
         if result['Type'] == 'movie' or result['Type'] == 'N/A':
             ratio = string_similarity_ratio(
                 result['Title'],
                 search_params.title
             )
             similarity_map.append(
                 {'imdbid': result['imdbID'], 'ratio': ratio}
             )
     similarity_map.sort(key=lambda value: value['ratio'], reverse=True)
     item_count = min(len(similarity_map), search_params.amount)
     movieids = [item['imdbid'] for item in similarity_map[:item_count]]
     return self._movieids_to_urllist(movieids)
Example #5
0
    def _parse_search_module(self, result, search_params):
        similarity_map = []
        for result in result['results']:
            if result is not None:
                ratio = 0.0
                for title_key in ['original_title', 'title']:
                    ratio = max(
                        ratio,
                        string_similarity_ratio(
                            result[title_key],
                            search_params.title
                        )
                    )
                similarity_map.append({'tmdbid': result['id'], 'ratio': ratio})

        similarity_map.sort(key=lambda value: value['ratio'], reverse=True)
        item_count = min(len(similarity_map), search_params.amount)
        movieids = [item['tmdbid'] for item in similarity_map[:item_count]]
        return self._config.build_movie_urllist(movieids, search_params)
Example #6
0
    def _parse_search_module(self, result, search_params):
        similarity_map = []
        if result.body.table:
            try:
                for item in result.body.table.find_all("a"):
                    if item.get_text() and item.get("href"):
                        title = item.get_text().replace('\n', '')
                        url = item.get("href")
                        ratio = string_similarity_ratio(
                            title,
                            search_params.title
                        )
                        similarity_map.append(
                            {'title': title, 'ratio': ratio, 'url': url}
                        )
            except AttributeError as e:
                print('AttributeError in _parse_search_module, filmstarts.', e)
                return None

        similarity_map.sort(key=lambda value: value['ratio'], reverse=True)
        item_count = min(len(similarity_map), search_params.amount)

        return self._create_links(similarity_map[:item_count])
Example #7
0
    def _parse_search_module(self, result, search_params):
        similarity_map = []
        try:
            for item in result.find_all("div", {"class": "name"}):
                if item.get_text() and item.find("a").get("href"):
                    title = item.get_text()
                    url, _ = item.find("a").get("href").split('?', maxsplit=1)
                    ratio = string_similarity_ratio(
                        title,
                        search_params.title
                    )
                    similarity_map.append(
                        {'title': title, 'ratio': ratio, 'url': url}
                    )
        except AttributeError as e:
            print(e)
            return None

        similarity_map.sort(key=lambda value: value['ratio'], reverse=True)
        item_count = min(len(similarity_map), search_params.amount)
        return [[self._movie_url.format(
            item['url'])] for item in similarity_map[:item_count]
        ]
Example #8
0
 def _parse_search_module(self, response, search_params):
     """ Parse a search response. Find high prio result. Build urllist."""
     similarity_map = []
     for response in response['eintrag']:
         if response['wert'].isnumeric():
             # a 'hack' for better name matching, as name string often looks
             # like this 'Emma Roberts (10.02.1991) alias Emma Rose Roberts'
             clean_name, *_ = response['name'].split('(')
             ratio = string_similarity_ratio(
                 clean_name,
                 search_params.name
             )
             similarity_map.append(
                 {'ofdbid': response['wert'],
                  'ratio': ratio, 'name': response['name']}
             )
     # sort by ratio, generate ofdbid list with requestet item count
     similarity_map.sort(
         key=lambda value: value['ratio'],
         reverse=True
     )
     item_count = min(len(similarity_map), search_params.amount)
     personids = [item['ofdbid'] for item in similarity_map[:item_count]]
     return self._common.personids_to_urllist(personids)