Skip to content

Commit

Permalink
add support for fetching anidb posters
Browse files Browse the repository at this point in the history
  • Loading branch information
Winterbird committed Aug 31, 2023
1 parent dcc0071 commit f15ed3a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ file = File(path="/media/Anime/Series/Kemono no Souja Erin/[winterbird] Kemono n
# note that most of the time this will work even if we use a file that is not in the anidb database
# will print "'<path>' contains episode 5 of 'Kemono no Souja Erin'. Mylist state is 'on hdd'"
print("'{}' contains episode {} of '{}'. Mylist state is '{}'".format(file.path, file.episode.episode_number, file.anime.title, file.mylist_state))

# adbb supports fetching posters. download_image() supports Anime and Group objects
# (afaik, there are no other images to get from anidb)
# For other images, check the fanart section below.
with open('poster.jpg', 'wb') as f:
adbb.download_image(f, anime)
```

## Reference
Expand Down
13 changes: 13 additions & 0 deletions adbb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def get_session():
def close_session(session):
session.close()

def download_image(filehandle, obj):
if type(obj) not in (Anime, Group):
raise adbb.errors.AniDBMissingImage(f'Object type {type(obj)} does not support images')
if not obj.picname:
raise adbb.errors.AniDBMissingImage(f'{obj} does not have a picture defined')
url_base = 'https://cdn.anidb.net/images/main'
url=f'{url_base}/{obj.picname}'
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as f:
filehandle.write(f.read())



def download_fanart(filehandle, url, preview=False):
if not fanart_key:
raise adbb.errors.FanartError('No fanart key available')
Expand Down
3 changes: 3 additions & 0 deletions adbb/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ class IllegalAnimeObject(AniDBError):

class FanartError(AniDBError):
pass

class AniDBMissingImage(AniDBError):
pass
26 changes: 23 additions & 3 deletions adbb/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ def write_nfo(obj, nfo_path, fetch_fanart=True, dry_run=False):
os.remove(tmpart)
continue
os.rename(tmpart, os.path.join(art_dir, fname))
if not all_arts['poster']:
fname = 'poster.jpg'
tmpfile = os.path.join(art_dir, f'.{fname}.tmp')
try:
with open(tmpfile, 'wb') as f:
adbb.download_image(f, anime)
os.rename(tmpfile, os.path.join(art_dir, fname))
except urllib.error.HTTPError as e:
adbb.log.error(f'Failed to download anidb image for {anime}: {e}')
os.remove(tmpfile)

os.rename(tmpfile, nfo_path)

Expand Down Expand Up @@ -282,8 +292,8 @@ def create_anime_collection(
os.makedirs(collection_dir, exist_ok=True)
# Fanart
all_arts = { k: [] for k in JELLYFIN_ART_TYPES }
for anime in relations:
for arts in anime.fanart:
for series in relations:
for arts in series.fanart:
for key, value in arts.items():
if key in FANART_MAP:
art_type = FANART_MAP[key]
Expand All @@ -306,10 +316,20 @@ def create_anime_collection(
with open(tmpfile, 'wb') as f:
adbb.download_fanart(f, url)
except urllib.error.HTTPError as e:
adbb.log.error('Failed to download fanart at {url}: {e}')
adbb.log.error(f'Failed to download fanart at {url}: {e}')
os.remove(tmpfile)
continue
os.rename(tmpfile, os.path.join(collection_dir, fname))
if not all_arts['poster']:
fname = 'poster.jpg'
tmpfile = os.path.join(collection_dir, f'.{fname}.tmp')
try:
with open(tmpfile, 'wb') as f:
adbb.download_image(f, anime)
os.rename(tmpfile, os.path.join(collection_dir, fname))
except urllib.error.HTTPError as e:
adbb.log.error(f'Failed to download anidb image for {anime}: {e}')
os.remove(tmpfile)


# XML
Expand Down

0 comments on commit f15ed3a

Please sign in to comment.