用额外的值更新字典

Update dict with extra values

我有以下将数据(显示)从一种格式转换为另一种格式的方法,这曾经有效,直到 trakt.tv 更新了他们的 api。

我现在需要的是像这样的一个季号内每一季的所有剧集号

"seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]

转换后的数据最终应该是这样的

"shows": [{"title": "The Walking Dead", "year": 2010, "ids": {"tvdb": 153021, "imdb": "tt1520211", "tmdb": 1402, "tvrage": 25056}, "seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]

原始数据(显示)看起来像

{'episodes': [{'episode': 0, 'season': 1}, {'episode': 1, 'season': 1}, {'episode': 2, 'season': 1}], 'imdbnumber': '260449', 'title': 'Vikings'}

标题、年份和 ID 部分没问题,如果我 运行 现在的代码,我最终会得到多个第 1 季(每集 1 个)

def convert_YAMJ_show_to_trakt(show):
#"shows": [{"title": "The Walking Dead", "year": 2010, "ids": {"tvdb": 153021, "imdb": "tt1520211", "tmdb": 1402, "tvrage": 25056}, "seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]
trakt_show = {'ids':{}}

if 'imdbnumber' in show:
    if show['imdbnumber'].startswith('tt'):
        trakt_show['ids']['imdb'] = show['imdbnumber']
    else:
        trakt_show['ids']['tvdb'] = show['imdbnumber']

if 'tvdb' in show:
    trakt_show['ids']['tvdb'] = show['tvdb']

if 'imdb' in show:
    trakt_show['ids']['imdb'] = show['imdb']

if 'title' in show:
    trakt_show['title'] = show['title']

if 'episodes' in show and show['episodes']:
    for episode in show['episodes']:
        if 'date' in episode:
            #ep = {'episode': episode['episode'], 'season': episode['season'], 'watched_at': episode['date']}
            ep = {"seasons": [{"number": episode['season'], "episodes": [{"number": episode['episode']}]}]}
        else:
            #ep = {'episode': episode['episode'], 'season': episode['season']}
            ep = {"seasons": [{"number": episode['season'], "episodes": [{"number": episode['episode']}]}]}

        #if 'playcount' in episode:
        #     ep['plays'] = episode['playcount']

        #trakt_show['seasons'].append(ep)
        trakt_show.append(ep)
return trakt_show

你能试试这个吗:-

  1. 我改变了循环逻辑。已创建 dictionary,其中 keyseason numbervalue 为第 ` 集编号的 list
  2. 最终从字典创建输出结构。

代码:

show = {
        'episodes': [
                    {'episode': 0, 'season': 1}, {'episode': 1, 'season': 1}, {'episode': 2, 'season': 1},
                    {'episode': 0, 'season': 2}, {'episode': 1, 'season': 2}, {'episode': 2, 'season': 2},
                ], 
            'imdbnumber': '260449', 'title': 'Vikings'
        }


trakt_show = {'ids':{}}

if 'imdbnumber' in show:
    if show['imdbnumber'].startswith('tt'):
        trakt_show['ids']['imdb'] = show['imdbnumber']
    else:
        trakt_show['ids']['tvdb'] = show['imdbnumber']
try:
    trakt_show['ids']['tvdb'] = show['tvdb']
except:
    pass
try:
    trakt_show['ids']['imdb'] = show['imdb']
except:
    pass
try:
    trakt_show['title'] = show['title']
except:
    pass

if 'episodes' in show and show['episodes']:
    ep = {}
    for episode in show['episodes']:
        try:
            ep[episode["season"]].append(episode["episode"])
        except:
            ep[episode["season"]] = [episode["episode"]]

    ep1 = {"seasons":[]}
    for i in ep:
        tmp = dict()
        tmp["number"] = i
        tmp["episodes"] = []
        for j in ep[i]:
            tmp["episodes"].append({"number":j})
        ep1["seasons"].append(tmp)
    trakt_show.update(ep1)
print trakt_show

输出:

vivek@vivek:~/Desktop$ python 2.py 
{'seasons': [{'episodes': [{'number': 0}, {'number': 1}, {'number': 2}], 'number': 1}, {'episodes': [{'number': 0}, {'number': 1}, {'number': 2}], 'number': 2}], 'ids': {'tvdb': '260449'}, 'title': 'Vikings'}