Skip to content

Commit

Permalink
Split converter with combiner as core logic is different
Browse files Browse the repository at this point in the history
  • Loading branch information
nattadasu committed Aug 15, 2023
1 parent 14fe0a8 commit 2bdd4b1
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 183 deletions.
185 changes: 185 additions & 0 deletions generator/combiner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
from typing import Any

from alive_progress import alive_bar # type: ignore
from const import pprint
from prettyprint import Platform, Status

def combine_arm(
arm: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine ARM data with AOD data"""
linked = 0
with alive_bar(len(aod),
title="Combining ARM data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
myanimelist = item['myanimelist']
anilist = item['anilist']
# Skip if both myanimelist and anilist are null
if myanimelist is None and anilist is None:
item.update({
'shoboi': None,
'annict': None,
})
bar()
continue

# Check if mal_id and anilist_id exist in arm_data
for arm_item in arm:
mal_id = arm_item.get('mal_id', None)
anilist_id = arm_item.get('anilist_id', None)
syoboi = arm_item.get('syobocal_tid', None)
annict = arm_item.get('annict_id', None)
if myanimelist is not None and mal_id == myanimelist:
# Combine the data from arm_item with the item in aod_data
item.update({
'shoboi': syoboi,
'annict': annict,
'anilist': anilist if anilist is not None else anilist_id,
})

linked += 1
break
elif anilist is not None and anilist_id == anilist:
# Combine the data from arm_item with the item in aod_data
item.update({
'shoboi': syoboi,
'annict': annict,
'myanimelist': myanimelist if myanimelist is not None else mal_id,
'shikimori': myanimelist if myanimelist is not None else mal_id,
})
linked += 1
break
bar()
pprint.print(
Platform.ARM,
Status.PASS,
"ARM data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"ARM data:",
f"{len(arm)}",
)
return aod


def combine_anitrakt(
anitrakt: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine AniTrakt data with AOD data"""
linked = 0
with alive_bar(len(aod),
title="Combining AniTrakt data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
matched = False
myanimelist = item['myanimelist']
# Skip if both myanimelist and anilist are null
if myanimelist is None:
item.update({
'trakt': None,
'trakt_type': None,
'trakt_season': None,
})
bar()
continue

# Check if mal_id and anilist_id exist in anitrakt_data
for anitrakt_item in anitrakt:
mal_id = anitrakt_item.get('mal_id', None)
trakt = anitrakt_item.get('trakt_id', None)
media_type = anitrakt_item.get('type', None)
media_season = anitrakt_item.get('season', None)
if myanimelist is not None and mal_id == myanimelist:
# Combine the data from anitrakt_item with the item in aod_data
item.update({
'trakt': trakt,
'trakt_type': media_type,
'trakt_season': media_season,
})
linked += 1
matched = True
break

if not matched:
item.update({
'trakt': None,
'trakt_type': None,
'trakt_season': None,
})
bar()
pprint.print(
Platform.ANITRAKT,
Status.PASS,
"AniTrakt data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"AniTrakt data:",
f"{len(anitrakt)}",
)
return aod


def combine_fribb(
fribb: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine Fribb's Animelists data with AOD data to obtain IMDb and TMDB IDs via AniDB"""
linked = 0
with alive_bar(len(aod),
title="Combining Fribb's Animelists data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
matched = False
anidb = item['anidb']
# Skip if anidb is null
if anidb is None:
item.update({
'imdb': None,
'themoviedb': None,
})
bar()
continue

# Check if anidb_id exist in fribb_data
for fbi in fribb:
anidb_id = fbi.get('anidb_id', None)
imdb = fbi.get('imdb_id', None)
tmdb: str | int | None = fbi.get('themoviedb_id', None)
if anidb is not None and anidb_id == anidb:
# Combine the data from fribb_item with the item in aod_data
data_fbi = {}
data_fbi['imdb'] = imdb
if isinstance(tmdb, str):
tmdbl = tmdb.split(",")
tmdb = int(tmdbl[0])
data_fbi['themoviedb'] = tmdb
item.update(data_fbi)
linked += 1
matched = True
break

if not matched:
item.update({
'imdb': None,
'themoviedb': None,
})
bar()
pprint.print(
Platform.FRIBB,
Status.PASS,
"Fribb's Animelists data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"Fribb's Animelists data:",
f"{len(fribb)}",
)
return aod
181 changes: 0 additions & 181 deletions generator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,184 +440,3 @@ def link_silveryasha_to_mal(
with open("database/raw/silveryasha_unlinked.json", "w", encoding="utf-8") as file:
json.dump(unlinked, file)
return aod_list


def combine_arm(
arm: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine ARM data with AOD data"""
linked = 0
with alive_bar(len(aod),
title="Combining ARM data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
myanimelist = item['myanimelist']
anilist = item['anilist']
# Skip if both myanimelist and anilist are null
if myanimelist is None and anilist is None:
item.update({
'shoboi': None,
'annict': None,
})
bar()
continue

# Check if mal_id and anilist_id exist in arm_data
for arm_item in arm:
mal_id = arm_item.get('mal_id', None)
anilist_id = arm_item.get('anilist_id', None)
syoboi = arm_item.get('syobocal_tid', None)
annict = arm_item.get('annict_id', None)
if myanimelist is not None and mal_id == myanimelist:
# Combine the data from arm_item with the item in aod_data
item.update({
'shoboi': syoboi,
'annict': annict,
'anilist': anilist if anilist is not None else anilist_id,
})

linked += 1
break
elif anilist is not None and anilist_id == anilist:
# Combine the data from arm_item with the item in aod_data
item.update({
'shoboi': syoboi,
'annict': annict,
'myanimelist': myanimelist if myanimelist is not None else mal_id,
'shikimori': myanimelist if myanimelist is not None else mal_id,
})
linked += 1
break
bar()
pprint.print(
Platform.ARM,
Status.PASS,
"ARM data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"ARM data:",
f"{len(arm)}",
)
return aod


def combine_anitrakt(
anitrakt: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine AniTrakt data with AOD data"""
linked = 0
with alive_bar(len(aod),
title="Combining AniTrakt data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
matched = False
myanimelist = item['myanimelist']
# Skip if both myanimelist and anilist are null
if myanimelist is None:
item.update({
'trakt': None,
'trakt_type': None,
'trakt_season': None,
})
bar()
continue

# Check if mal_id and anilist_id exist in anitrakt_data
for anitrakt_item in anitrakt:
mal_id = anitrakt_item.get('mal_id', None)
trakt = anitrakt_item.get('trakt_id', None)
media_type = anitrakt_item.get('type', None)
media_season = anitrakt_item.get('season', None)
if myanimelist is not None and mal_id == myanimelist:
# Combine the data from anitrakt_item with the item in aod_data
item.update({
'trakt': trakt,
'trakt_type': media_type,
'trakt_season': media_season,
})
linked += 1
matched = True
break

if not matched:
item.update({
'trakt': None,
'trakt_type': None,
'trakt_season': None,
})
bar()
pprint.print(
Platform.ANITRAKT,
Status.PASS,
"AniTrakt data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"AniTrakt data:",
f"{len(anitrakt)}",
)
return aod


def combine_fribb(
fribb: list[dict[str, Any]],
aod: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Combine Fribb's Animelists data with AOD data to obtain IMDb and TMDB IDs via AniDB"""
linked = 0
with alive_bar(len(aod),
title="Combining Fribb's Animelists data with AOD data",
spinner=None) as bar: # type: ignore
for item in aod:
matched = False
anidb = item['anidb']
# Skip if anidb is null
if anidb is None:
item.update({
'imdb': None,
'themoviedb': None,
})
bar()
continue

# Check if anidb_id exist in fribb_data
for fbi in fribb:
anidb_id = fbi.get('anidb_id', None)
imdb = fbi.get('imdb_id', None)
tmdb: str | int | None = fbi.get('themoviedb_id', None)
if anidb is not None and anidb_id == anidb:
# Combine the data from fribb_item with the item in aod_data
data_fbi = {}
data_fbi['imdb'] = imdb
if isinstance(tmdb, str):
tmdbl = tmdb.split(",")
tmdb = int(tmdbl[0])
data_fbi['themoviedb'] = tmdb
item.update(data_fbi)
linked += 1
matched = True
break

if not matched:
item.update({
'imdb': None,
'themoviedb': None,
})
bar()
pprint.print(
Platform.FRIBB,
Status.PASS,
"Fribb's Animelists data combined with AOD data.",
"Total linked data:",
f"{linked},",
"AOD data:",
f"{len(aod)}",
"Fribb's Animelists data:",
f"{len(fribb)}",
)
return aod
4 changes: 2 additions & 2 deletions generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from alive_progress import alive_bar # type: ignore
from const import (KAIZE_EMAIL, KAIZE_PASSWORD, KAIZE_SESSION,
KAIZE_XSRF_TOKEN, attribution, pprint)
from converter import (combine_anitrakt, combine_arm, combine_fribb,
link_kaize_to_mal, link_otakotaku_to_mal,
from combiner import combine_anitrakt, combine_arm, combine_fribb
from converter import (link_kaize_to_mal, link_otakotaku_to_mal,
link_silveryasha_to_mal)
from dumper import update_attribution, update_markdown
from fetcher import (get_anime_offline_database, get_anitrakt, get_arm,
Expand Down

0 comments on commit 2bdd4b1

Please sign in to comment.