python - 解析服务器有效负载,缺少几个键

标签 python json xml metadata

我有一些相当基本的代码。基本上,它的作用是将 API 请求发送到本地托管的服务器并返回 JSON 字符串。我要拿起那根绳子,把它折断。然后我从中获取所需内容,制作一个字典,并将其导出为带有 nfo 扩展名的 XML 文件。

问题是有时源数据会丢失一些位。例如,季节缺失的情况相当频繁。它破坏了数据映射。我需要一种方法来处理这个问题。对于某些内容,我可能想排除数据,而对于其他内容,我需要一个合理的默认值。

#!/bin/env python

import os
import requests
import re
import json
import dicttoxml
import xml.dom.minidom
from xml.dom.minidom import parseString
# Grab Shoko Auth Key

apiheaders = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

apidata = '{"user": "Default", "pass": "", "device": "CLI"}'

r = requests.post('http://192.168.254.100:8111/api/auth',
                  headers=apiheaders, data=apidata)
key = json.loads(r.text)['apikey']

# Grabbing Episode Data
EpisodeHeaders = {
    'accept': 'text/plain',
    'apikey': key
}

EpisodeParams = (
    ('filename',
     "FILE HERE"),
    ('pic', '1'),

)

fileinfo = requests.get(
    'http://192.168.254.100:8111/api/ep/getbyfilename', headers=EpisodeHeaders, params=EpisodeParams)

# Mapping Data from Shoko to Jellyfin NFO
string = json.loads(fileinfo.text)
print(string)
eplot = json.loads(fileinfo.text)['summary']
etitle = json.loads(fileinfo.text)['name']
eyear = json.loads(fileinfo.text)['year']
episode = json.loads(fileinfo.text)['epnumber']
season = json.loads(fileinfo.text)['season']
aid = json.loads(fileinfo.text)['aid']
seasonnum = season.split('x')

# Create Dictionary From Mapped Data

show = {
    "plot": eplot,
    "title": etitle,
    "year": eyear,
    "episode": episode,
    "season": seasonnum[0],
}

这是代码崩溃时的一些示例输出

{'type': 'ep', 'eptype': 'Credits', 'epnumber': 1, 'aid': 10713, 'eid': 167848, 
'id': 95272, 'name': 'Opening', 'summary': 'Episode Overview not Available', 
'year': '2014', 'air': '2014-11-23', 'rating': '10.00', 'votes': '1', 
'art': {'fanart': [{'url': '/api/v2/image/support/plex_404.png'}], 
'thumb': [{'url': '/api/v2/image/support/plex_404.png'}]}}
Traceback (most recent call last):
  File "/home/fletcher/Documents/Shoko-Jellyfin-NFO/Xml3.py", line 48, in <module>
    season = json.loads(fileinfo.text)['season']
KeyError: 'season'

基于 Mahori 建议的解决方案。工作完美。

eplot = json.loads(fileinfo.text).get('summary', None)
etitle = json.loads(fileinfo.text).get('name', None)
eyear = json.loads(fileinfo.text).get('year', None)
episode = json.loads(fileinfo.text).get('epnumber', None)
season = json.loads(fileinfo.text).get('season', '1x1')
aid = json.loads(fileinfo.text).get('aid', None)

最佳答案

这是 Web 开发中相当常见的场景,您不能总是假设另一方会发送所有 key 。

解决这个问题的标准方法是使用get而不是named fetch。

season = json.loads(fileinfo.text).get('season', None)
#you can change None to any default value here

关于python - 解析服务器有效负载,缺少几个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61570962/

相关文章:

python - OpenCV 对象跟踪输入格式

python - 压缩两个长度为 n 和 2n 的数组以形成字典

javascript - 在javascript中将json字符串转换为xml字符串

django - 使用 json 文件在开发服务器中测试文档上传

xml - rails 3 渲染 xml 而不管 request.format

python - 如何从 https ://stackoverflow. com/?tab=month 中提取元素(标题名称、投票、 View 、每个标题的答案)?

python - 比较平面列表和嵌套列表

javascript - 如果数据的格式略有损坏,是否仍然可以确定其文件类型?

Java Xpath 查询编译失败

javascript - XMLHttpRequest 是否根据 XSD 检查 XML 文档(如果存在)?