def setUp(self): self.user = get_user_model().objects.create_superuser(username='******', password='******', email='*****@*****.**') anime = Category.objects.get(slug='anime') Work.objects.bulk_create([ Work(title='Sangatsu no Lion', category=anime), Work(title='Hibike! Euphonium', category=anime), Work(title='Kiznaiver', category=anime) ]) self.work_ids = Work.objects.values_list('pk', flat=True) WorkTitle.objects.bulk_create([ WorkTitle(work=Work.objects.get(id=self.work_ids[0]), title='3-gatsu no Lion', type='synonym'), WorkTitle(work=Work.objects.get(id=self.work_ids[1]), title='Sound! Euphonium', type='synonym') ]) self.worktitle_ids = WorkTitle.objects.values_list('pk', flat=True)
def build_work_titles(work: Work, titles: Dict[str, Tuple[str, str]]) -> List[WorkTitle]: """ Insert WorkTitle objects for a given Work when required into Mangaki's database. :param work: a work :param titles: a list of alternative titles :type work: Work :type titles: Dict[str, Tuple[str, str]] :return: a list of WorkTitle objects that were inserted in Mangaki's database :rtype: List[WorkTitle] """ if not titles: return [] language_map = { 'english': anilist_langs.english_ext_lang, 'romaji': anilist_langs.romaji_ext_lang, 'japanese': anilist_langs.japanese_ext_lang, 'unknown': anilist_langs.unknown_ext_lang, } worktitles = [] for title, (language, title_type) in titles.items(): ext_language = language_map.get(language, language_map['unknown']) worktitles.append( WorkTitle(work=work, title=title, ext_language=ext_language, language=ext_language.lang if ext_language else None, type=title_type)) existing_titles = set( WorkTitle.objects.filter(title__in=titles).values_list('title', flat=True)) missing_titles = [ worktitle for worktitle in worktitles if worktitle.title not in existing_titles ] WorkTitle.objects.bulk_create(missing_titles) return missing_titles
def _build_work_titles(self, work: Work, titles: Dict[str, Dict[str, str]], reload_lang_cache: bool = False) -> List[WorkTitle]: if reload_lang_cache: # noinspection PyPropertyAccess del self.lang_map work_titles = [] raw_titles = [] for title_info in titles: title = title_info['title'] lang = title_info['lang'] title_type = title_info['type'] ext_lang_model = self.lang_map.get(lang, self.unknown_language) raw_titles.append(title) work_titles.append( WorkTitle( work=work, title=title, ext_language=ext_lang_model, language=ext_lang_model.lang if ext_lang_model else None, type=title_type ) ) already_existing_titles = set(WorkTitle.objects .filter(title__in=raw_titles) .values_list('title', flat=True)) missing_titles = [ work_title for work_title in work_titles if work_title.title not in already_existing_titles ] WorkTitle.objects.bulk_create(missing_titles) return missing_titles
def build_work_titles(work: Work, titles: Dict[str, Tuple[str, str]]) -> List[WorkTitle]: worktitles = [] for title, (language, title_type) in titles.items(): ext_language = language_map.get(language, language_map['unknown']) worktitles.append( WorkTitle(work=work, title=title, ext_language=ext_language, language=ext_language.lang if ext_language else None, type=title_type)) existing_titles = set( WorkTitle.objects.filter(title__in=titles).values_list('title', flat=True)) missing_titles = [ worktitle for worktitle in worktitles if worktitle.title not in existing_titles ] WorkTitle.objects.bulk_create(missing_titles) return missing_titles
def insert_into_mangaki_database_from_mal( mal_entries: List[MALEntry], title: Optional[str] = None) -> Optional[Work]: """ Insert into Mangaki database from MAL data. And return the work corresponding to the title. :param mal_entries: a list of entries that will be inserted into the database if not present :type mal_entries: List[MALEntry] :param title: title of work looked for (optional) :type title: str (can be None) :return: a work if title is specified :rtype: Optional[Work] """ # Assumption: Mangaki's slugs are the same as MAL's work types. if not mal_entries: return first_matching_work = None # All entries are supposed of the same type. work_cat = Category.objects.get(slug=mal_entries[0].work_type.value) for entry in mal_entries: # Check over all known titles. titles = [entry.title] if entry.english_title: titles.append(entry.english_title) is_present = (len( lookup_works(Work.objects.filter(category=work_cat), entry.poster, titles)) > 0) if not is_present: extra_fields = {} if entry.nb_episodes: extra_fields['nb_episodes'] = entry.nb_episodes work = Work.objects.create(category=work_cat, title=entry.english_title or entry.title, source=entry.source_url, ext_poster=entry.poster, date=entry.start_date, **extra_fields) if (title and not first_matching_work and title.upper() in list(map(str.upper, titles))): first_matching_work = work work_titles = [ WorkTitle(work=work, title=synonym, ext_language=mal_langs.unk_ext_lang, language=mal_langs.unk_ext_lang.lang, type='synonym') for synonym in entry.synonyms ] if entry.english_title: work_titles += [ WorkTitle(work=work, title=entry.english_title, ext_language=mal_langs.english_ext_lang, language=mal_langs.english_ext_lang.lang, type='main') ] if work_titles: WorkTitle.objects.bulk_create(work_titles) # FIXME: add ext genre, ext type. # Tracked in https://github.com/mangaki/mangaki/issues/339 return first_matching_work