Skip to content

Commit

Permalink
Tweaked TVDB image ordering, Added role ordering by name, Started add…
Browse files Browse the repository at this point in the history
…ing draft optimization code

Images retrieved from TVDB will now order in a more consistant manner,
usually favoring the highest quality by default and is now more likely
to retrieve matching sets of season poster.
  • Loading branch information
Dingmatt committed Apr 5, 2017
1 parent 56fcd6d commit 70185dd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
14 changes: 10 additions & 4 deletions Plug-ins/Amsa_Test.bundle/Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,22 @@ def scoreTitle(anime=anime, maxi=maxi, anidb=anidb, perfectScore=perfectScore):
### Parse the AniDB anime title XML ##################################################################################################################################
def update(self, metadata, media, lang, force=False):
Log.Debug("--- Update Begin -------------------------------------------------------------------------------------------")
if force:
HTTP.ClearCache()
logging.New_Milestones()
logging.Log_Milestone("WholeUpdate")
common.RefreshData()
source, id = metadata.id.split("-")
Log("Source: %s, ID: %s" % (source, id))
#filename = ""
#for media_item in media.seasons[1].episodes[1].items:
# for item_part in media_item.parts:
# filename = os.path.abspath(os.path.join(os.path.dirname(item_part.file.lower()), "..","Specials"))
#Log("FileName: %s" % (filename))
#for sea_item in media.seasons:
# Log("FileName1: %s" % (sea_item))
# for eps_item in media.seasons[sea_item].episodes:
# Log("FileName2: %s" % (eps_item))
# for media_item in media.seasons[sea_item].episodes[eps_item].items:
# for item_part in media_item.parts:
# filename = item_part.file.lower()
# Log("FileName: %s" % (filename))
#functions.downloadfile("test.webm", "https://my.mixtape.moe/sovrtq.webm")

mappingData = None
Expand Down
2 changes: 1 addition & 1 deletion Plug-ins/Amsa_Test.bundle/Contents/Code/anidb.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(self, id):
season = XML.ElementFromString(season)
bannerPath = GetElementText(data, "picture")
mainUrl, thumbUrl, mainLocalPath, thumbLocalPath = functions.ParseImage(bannerPath, constants.ANIDB_PIC_BASE_URL, os.path.join("AniDB", id, "season"))
SubElement(season, "Image", id = "1", mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath)
SubElement(season, "Image", id = "1", mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath, season = "1")
self.Season = season
self.Posters = copy.deepcopy(season)
##--------------------------------Themes-------------------------------##
Expand Down
42 changes: 39 additions & 3 deletions Plug-ins/Amsa_Test.bundle/Contents/Code/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def MapLocal(root, media):
if match:
seasonNo = int(match.group('season'))
episodeNo = int(match.group('episode'))
Log("Local2: '%s', '%s'" %( seasonNo,episodeNo))
#Log("Local2: '%s', '%s'" %( seasonNo,episodeNo))
season = GenerateSeason(root, seasonNo)
episode = GenerateEpisode(root, season, seasonNo, episodeNo)
mapped = SubElement(episode, "Mapped")
Expand Down Expand Up @@ -350,6 +350,42 @@ def Provider_Task(root=root, providers=providers, provider=provider):
SubElement(map.getparent().getparent().find("""./%s""" % (attrib)), provider).text = (u'%s' % (elementItem))
logging.Log_Milestone("MapMeta")

def SearchMap(root, media, anidbId, tvdbId):
logging.Log_Milestone("SearchMap")
streamTag = []
seasonMap = root.xpath("""./Season[@AnidbId="%s" and @num!="0"]""" % (anidbId))[0]
if not seasonMap:
seasonMap = root.xpath("""./Season[@TvdbId="%s" and @num!="0"]""" % (tvdbId))[0]
seasonNo = int(seasonMap.get("num"))
if not seasonNo:
seasonNo = 0

season = str(int(map.getparent().get('num')) - seasonNo + 1)

for seasonNo in media.seasons:
for episodeNo in media.seasons[seasonNo].episodes:
map = root.xpath("""./Season[@num>=%s]/Episode""" % (seasonNo))

logging.Log_Milestone("SearchMap")

# data = []
# if re.search(r".*\b(?P<season>S\d+)(?P<episode>E\d+)\b.*", filename, re.IGNORECASE) or re.search(r".*\b(?P<type>ncop|op|nced|ed)(?P<episode>\d+)\b.*", filename, re.IGNORECASE):
# mappedEpisode = root.xpath("""./Mapping/Series/Episode[@tvdb="S%sE%s"]""" % (str(season).zfill(2), str(episode).zfill(2)))
# if mappedEpisode:
# data.append(["Anidb", mappedEpisode[0].getparent().get("anidbid"), mappedEpisode[0].get("anidb")])
# data.append(["Tvdb", mappedEpisode[0].getparent().get("tvdbid"), mappedEpisode[0].get("tvdb")])
# else:
# match = re.search(r".*\B\[(?P<provider>\D+)(?P<id>\d+)\]\B.*", filename, re.IGNORECASE)
# if match:
# provider = match.group('provider').lower()
# data.append(["Anidb", match.group('id').lower(), anidb.ParseNoFromSeason(int(season), int(episode))])

# elif re.search(r".*\B\-\s(?:E|A|Abs)?(?P<episode>\d+)\s\-\B.*", filename, re.IGNORECASE):
# mappedEpisode = root.xpath("""./Mapping/Series[@anidbid="%s"]/Episode[@anidb="%s"]""" % (anidbid, anidb.ParseNoFromSeason(int(season), int(episode))))
# if mappedEpisode:
# data.append(["Anidb", mappedEpisode[0].getparent().get("anidbid"), mappedEpisode[0].get("anidb")])
# data.append(["Tvdb", mappedEpisode[0].getparent().get("tvdbid"), mappedEpisode[0].get("tvdb")])
# return data

def MapMedia(root, metadata, anidbId, tvdbId):
logging.Log_Milestone("MapMedia")
Expand Down Expand Up @@ -389,9 +425,9 @@ def Episode_Par():
@task
def Episode_Task(map=map, metadata=metadata, anidbId=anidbId, tvdbId=tvdbId, seasonNo=seasonNo):
season = str(int(map.getparent().get('num')) - seasonNo + 1)
Log("SeasonNumber: %s, %s, %s" % (seasonNo, map.getparent().get('num'), str(int(map.getparent().get('num')) - seasonNo + 1)))
#Log("SeasonNumber: %s, %s, %s" % (seasonNo, map.getparent().get('num'), str(int(map.getparent().get('num')) - seasonNo + 1)))
episode = map.get('num')
Log("Episode: '%s', '%s', %s, %s" % (season, episode, anidbId, map.getparent().get('AnidbId')))
#Log("Episode: '%s', '%s', %s, %s" % (season, episode, anidbId, map.getparent().get('AnidbId')))

logging.Log_Milestone("MapMedia_Episode_S" + season + "E" + episode)
metadata.seasons[season].episodes[episode].title = functions.PopulateMetadata(map.xpath("""./Title/*[node()]"""), str, constants.EPISODE_TITLE_PRIORITY)
Expand Down
23 changes: 15 additions & 8 deletions Plug-ins/Amsa_Test.bundle/Contents/Code/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def CleanTitle(title, filter = False):
title = unidecode(u"%s" % (title))
return str(unicodedata.normalize('NFKD', safe_unicode(title)).strip())

def GetElementText(el, xp):
return el.xpath(xp)[0].text if el is not None and el.xpath(xp) and el.xpath(xp)[0].text else ""
def GetElementText(el, xp, default=None):
return el.xpath(xp)[0].text if el is not None and el.xpath(xp) and el.xpath(xp)[0].text else ("" if default == None else default)

def GetByPriority(metaList, priorityList, metaType):
try:
Expand All @@ -207,8 +207,15 @@ def GetByPriority(metaList, priorityList, metaType):
if metaType is Framework.modelling.attributes.SetObject:
return sorted(filter(lambda i: len(i.getchildren()) > 0, metaList), key=lambda x: priorityList.index(x.tag.lower()), reverse=False)[0]
elif metaType is Framework.modelling.attributes.ProxyContainerObject:
dataList = sorted(metaList, key=lambda x: priorityList.index(x.getparent().tag.lower()), reverse=False)
dataList[0].set('id', '0')

dataList = sorted(metaList, key=lambda x: priorityList.index(x.getparent().tag.lower()) and x.get("id"), reverse=False)
seasonCount = []
for image in dataList:
if image.getparent().getparent().tag == "Season":
seasonCount.append(image.get("season"))
image.set('id', str(seasonCount.count(image.get("season"))))
#Log("Order: %s, %s, %s" % (image.getparent().tag.lower(), image.get("id"), image.get("season")))

return dataList
else:
return sorted(filter(lambda i: i.text != None and i.text != "None", metaList), key=lambda x: priorityList.index(x.tag.lower()), reverse=False)[0].text
Expand All @@ -229,7 +236,7 @@ def PopulateMetadata(map, metaType, priorityList, metaList=None, secondType=None
return metaList
if metaType is Framework.modelling.attributes.SetObject:
metaList.clear()
for person in data:
for person in sorted(data, key=lambda x: x.get('seiyuu_name', ''), reverse=False):
if isinstance(person, basestring):
if not len(person):
continue
Expand All @@ -247,16 +254,16 @@ def PopulateMetadata(map, metaType, priorityList, metaList=None, secondType=None
if secondType == "Images":
@parallelize
def Image_Par():
for image in sorted(data, key=lambda x: x.get("id"), reverse=False):
for image in sorted(data, key=lambda x: int(x.get("id")), reverse=False):
@task
def Image_Task(image=image, metaList=metaList):
#Log("Poster: %s, %s" % (image.get("id"), image.get("local")))
#Log("Poster: %s, %s, %s" % (image.get("id"), image.get("mainLocalPath"), image.getparent().tag.lower()))
if len(image.get("thumbUrl")) > 0:
FileFromURL(image.get("thumbUrl"), os.path.basename(image.get("thumbLocalPath")), os.path.dirname(image.get("thumbLocalPath")), CACHE_1HOUR * 24)
else:
FileFromURL(image.get("mainUrl"), os.path.basename(image.get("mainLocalPath")), os.path.dirname(image.get("mainLocalPath")), CACHE_1HOUR * 24)
if image.getparent().getparent().tag == "Season":
metaList[image.get("id")].posters[image.get("mainUrl")] = Proxy.Preview(Data.Load(image.get("thumbLocalPath")), sort_order=image.get("id")) if len(image.get("thumbLocalPath")) > 0 else Proxy.Media(Data.Load(image.get("mainLocalPath")), sort_order=image.get("id"))
metaList[image.get("season")].posters[image.get("mainUrl")] = Proxy.Preview(Data.Load(image.get("thumbLocalPath")), sort_order=image.get("id")) if len(image.get("thumbLocalPath")) > 0 else Proxy.Media(Data.Load(image.get("mainLocalPath")), sort_order=image.get("id"))
else:
metaList[image.get("mainUrl")] = Proxy.Preview(Data.Load(image.get("thumbLocalPath")), sort_order=image.get("id")) if len(image.get("thumbLocalPath")) > 0 else Proxy.Media(Data.Load(image.get("mainLocalPath")), sort_order=image.get("id"))
elif secondType == "Themes":
Expand Down
10 changes: 6 additions & 4 deletions Plug-ins/Amsa_Test.bundle/Contents/Code/tvdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ def __init__(self, id):
bannersCount = 2
season = etree.tostring(E.Images(), pretty_print=True, xml_declaration=True, encoding="UTF-8")
season = XML.ElementFromString(season)
seasonCount = []

for banner in bannersXml.xpath("./Banner"):
for banner in sorted(bannersXml.xpath("./Banner"), key=lambda x: float(GetElementText(x, "Rating", 0)) , reverse=True):

bannerType = GetElementText(banner, "BannerType")
bannerType2 = GetElementText(banner, "BannerType2")
bannerPath = GetElementText(banner, "BannerPath")
Expand All @@ -90,7 +92,7 @@ def __init__(self, id):
"banners" if bannerType == "series" or bannerType2=="seasonwide" else \
"season" if bannerType == "season" and bannerType2=="season" else None)

mainUrl, thumbUrl, mainLocalPath, thumbLocalPath = functions.ParseImage(bannerPath, constants.TVDB_IMAGES_URL, os.path.join("TvDB", id, metatype), bannerThumb)
mainUrl, thumbUrl, mainLocalPath, thumbLocalPath = functions.ParseImage(bannerPath, constants.TVDB_IMAGES_URL, os.path.join("TvDB", id, metatype), bannerThumb)
if metatype == "art":
SubElement(art, "Image", id = str(1 if bannerPath == GetElementText(data, "Series/fanart") else artCount), mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath)
artCount = artCount + 1
Expand All @@ -101,8 +103,8 @@ def __init__(self, id):
SubElement(banners, "Image", id = str(1 if bannerPath == GetElementText(data, "Series/banner") else bannersCount), mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath)
bannersCount = bannersCount + 1
if metatype == "season":
SubElement(season, "Image", id = str(GetElementText(banner, "Season")), mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath)

seasonCount.append(GetElementText(banner, "Season"))
SubElement(season, "Image", id = str(seasonCount.count(GetElementText(banner, "Season"))), mainUrl = mainUrl, thumbUrl = thumbUrl, mainLocalPath = mainLocalPath, thumbLocalPath = thumbLocalPath, season = str(GetElementText(banner, "Season")))

self.Art = art
self.Posters = posters
Expand Down

0 comments on commit 70185dd

Please sign in to comment.