DaRealFreak /
TitleSearch
| 1 | #!/usr/local/bin/python |
||
|
0 ignored issues
–
show
|
|||
| 2 | # coding: utf-8 |
||
| 3 | |||
| 4 | from titlesearch.bakaupdates import BakaUpdates |
||
| 5 | from titlesearch.mal import MyAnimeList |
||
| 6 | from titlesearch.vndb import VisualNovelDatabase |
||
| 7 | |||
| 8 | |||
| 9 | def get_similar_titles(title: str) -> list: |
||
| 10 | """search the 3 different modules for a similar title and return a list sorted by similarity |
||
| 11 | |||
| 12 | :type title: str |
||
| 13 | :return: |
||
| 14 | """ |
||
| 15 | light_novel_results = BakaUpdates.get_similar_titles(title) |
||
| 16 | visual_novel_results = VisualNovelDatabase.get_similar_titles(title) |
||
| 17 | anime_results = MyAnimeList.get_similar_titles(title) |
||
| 18 | |||
| 19 | results = [] |
||
| 20 | passed_titles = [] |
||
| 21 | |||
| 22 | for result_list in (light_novel_results, visual_novel_results, anime_results): |
||
| 23 | for result in result_list: |
||
| 24 | if result['title'] in passed_titles: |
||
| 25 | results[passed_titles.index(result['title'])]['links'].append(result['link']) |
||
| 26 | else: |
||
| 27 | results.append({ |
||
| 28 | 'title': result['title'], |
||
| 29 | 'links': [result['link']], |
||
| 30 | 'similarity': result['similarity'] |
||
| 31 | }) |
||
| 32 | passed_titles.append(result['title']) |
||
| 33 | |||
| 34 | results.sort(key=lambda item: item['similarity'], reverse=True) |
||
| 35 | return results |
||
| 36 | |||
| 37 | |||
| 38 | def get_alternative_titles(title: str = '') -> dict: |
||
| 39 | """Search the 3 different modules for an alternative title of the given title and return a |
||
| 40 | dictionary split into the different languages |
||
| 41 | |||
| 42 | :type title: str |
||
| 43 | :return: |
||
| 44 | """ |
||
| 45 | light_novel_results = BakaUpdates.get_alternative_titles(title=title) |
||
| 46 | visual_novel_results = VisualNovelDatabase.get_alternative_titles(title=title) |
||
| 47 | anime_results = MyAnimeList.get_alternative_titles(title=title) |
||
| 48 | |||
| 49 | alternative_titles = {} |
||
| 50 | |||
| 51 | for result_list in (light_novel_results, visual_novel_results, anime_results): |
||
| 52 | for language in result_list: |
||
| 53 | if not result_list[language]: |
||
| 54 | continue |
||
| 55 | |||
| 56 | for title in result_list[language]: |
||
| 57 | if language not in alternative_titles: |
||
| 58 | alternative_titles[language] = [title] |
||
| 59 | continue |
||
| 60 | |||
| 61 | if title not in alternative_titles[language]: |
||
| 62 | alternative_titles[language].append(title) |
||
| 63 | |||
| 64 | return alternative_titles |
||
| 65 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.