【发布时间】:2013-02-02 00:13:57
【问题描述】:
我有一个媒体播放器,我想将我正在播放的内容发送到 trakt.tv,除了标题/路径中的外国字母外,一切正常。系统运行python 2.7.3
def getStatus(self,ip,timeout=10.0):
oPchStatus = PchStatus()
try:
oResponse = urlopen("http://" + ip + ":8008/playback?arg0=get_current_vod_info",None,timeout)
oPchStatus = self.parseResponse(oResponse.readlines()[0])
return oPchStatus
这将返回类似这样的东西。
<?xml version="1.0"?>
<theDavidBox>
<request>
<arg0>get_current_vod_info</arg0>
<module>playback</module>
</request>
<response>
<currentStatus>pause</currentStatus>
<currentTime>3190</currentTime>
<downloadSpeed>0</downloadSpeed>
<fullPath>/opt/sybhttpd/localhost.drives/HARD_DISK/Storage/NAS/Videos/FILMS/A.Haunted.House.(2013)/A Haunted House.avi</fullPath>
<lastPacketTime>0</lastPacketTime>
<mediatype>OTHERS</mediatype>
<seekEnable>true</seekEnable>
<title/>
<totalTime>4860</totalTime>
</response>
<returnValue>0</returnValue>
</theDavidBox>
下一步采用上述方法并将每个项目分配给一个变量。
class PchStatus:
def __init__(self):
self.status=EnumStatus.NOPLAY
self.fullPath = u""
self.fileName = u""
self.currentTime = 0
self.totalTime = 0
self.percent = 0
self.mediaType = ""
self.currentChapter = 0 # For Blu-ray Disc only
self.totalChapter = 0 # For Blu-ray Disc only
self.error = None
class PchRequestor:
def parseResponse(self, response):
oPchStatus = PchStatus()
try:
response = unescape(response)
oXml = ElementTree.XML(response)
if oXml.tag == "theDavidBox": # theDavidBox should be the root
if oXml.find("returnValue").text == '0' and int(oXml.find("response/totalTime").text) > 90:#Added total time check to avoid scrobble while playing adverts/trailers
oPchStatus.totalTime = int(oXml.find("response/totalTime").text)
oPchStatus.status = oXml.find("response/currentStatus").text
oPchStatus.fullPath = oXml.find("response/fullPath").text
oPchStatus.currentTime = int(oXml.find("response/currentTime").text)
等等。使用上面返回的xml,
oPchStatus.totalTime 将是“4860” oPchStatus.status 将是“暂停” oPchStatus.fullPath 将是“/opt/sybhttpd/localhost.drives/HARD_DISK/Storage/NAS/Videos/FILMS/A.Haunted.House.(2013)/A Haunted House.avi” oPchStatus.currentTime 将是“3190”
就像我说的那样,这在标题中出现外国字母之前效果很好。像 Le.Fabuleux.Destin.d'Amélie.Poulain.(2001).avi 这样的标题将使 oPchStatus.fullPath 包含字符串“/opt/sybhttpd/localhost.drives/HARD_DISK/Storage/NAS/Videos/Le.Fabuleux。 Destin.d'Am\xe9lie.Poulain.(2001).avi"
而不是
“/opt/sybhttpd/localhost.drives/HARD_DISK/Storage/NAS/Videos/Le.Fabuleux.Destin.d'Amélie.Poulain.(2001).avi”
如我所愿。
在脚本中还有一些例程可以扫描 xml 文件中的文件名并创建 FILENAME.watched,因此我需要文件名与实际文件名匹配,而不是替换任何字母。
确保正确编码这些类型的文件名的最佳方法是什么? 我已尝试提供尽可能多的信息,但如果您需要更多信息,请尽管询问。
【问题讨论】:
-
d&apos;Am\xe9lie值看起来是正确的如果这是 python 向您显示元素。如果这是写入 XML 文件的内容(所以\xe9,字面意思是 4 个字符),那么还有其他问题。 -
什么是
oResponse.info()(特别是Content-Type标头)?响应中是否有 xml 声明,例如<?xml version="1.0" encoding="UTF-8" ?>?为什么使用response = unescape(response)? -
最后但同样重要的是,这是 Python 2 还是 3?
-
@MartijnPieters:如果输入是有效的xml;根本不需要使用
unescape()(因此是“为什么”问题)。"&apos;"是 predefined xml entity 和ElementTreeunderstands it -
@J.F.Sebastian:确实很有趣。看起来我们需要更多上下文。
标签: python xml utf-8 decode encode