0

我想要一种在 YAMJ 的 xml 文件中更改已观看电影状态的方法。我可以提供 baseFilenameBase、fileLocation 和 fileURL。我需要将这两个被监视的元素更改为 true。

<library count="1">
   <category count="1" current="true" name="Other">
      <index </index>
   </category>
   <movies cols="18" count="18" indexCount="673" totalCount="737">
      <movie isExtra="false" isSet="false" isTV="false">
         <baseFilenameBase>FILE NAME</baseFilenameBase>
         <watched>false</watched>
         <files>
            <file firstPart="1" lastPart="1" season="-1" size="0" subtitlesExchange="NO"
                  title="UNKNOWN"
                  watched="false"
                  zcd="2">
               <fileLocation>PATH TO FILE</fileLocation>
               <fileURL>PATH TO FILE</fileURL>
            </file>
         </files>
      </movie>
      <movie

      And repeats .....
    </movies>
</library>    

我已经设法替换了在其他 xml 文件中观看的内容

                doc = parse(file)
                node = doc.getElementsByTagName('watched')
                node[0].firstChild.nodeValue = 'true'    

与试图与兄弟姐妹/邻居相处相比,这非常简单

4

4 回答 4

0
                msg = 'Starting xml update in '.format(updatexmlwatched)
            Debug(msg)
            pchtrakt.logger.info(msg)
            if pchtrakt.isMovie:
                xmlpath = updatexmlwatched + "Other*.xml"
                for name in glob.glob(xmlpath):
                    if myMedia.oStatus.fileName[:-4] in open(name).read():#gets xml file name as name
                        tree = ElementTree.parse(name)
                        for movie in tree.findall('movies/movie'):
                            if movie.find('baseFilenameBase').text == myMedia.oStatus.fileName[:-4]:#for  content in penContents:
                                movie.find('watched').text = 'true'
                                for mfile in movie.findall('files/file'):
                                    mfile.set('watched', 'true')
                                    bak_name = name[:-4]+'.bak'
                                    tree.write(bak_name)
                                    os.rename(bak_name, name)
                                    txt = name.replace(updatexmlwatched, '') + ' has been modified as watched for ' + myMedia.oStatus.fileName
                                    Debug(txt)
                                    pchtrakt.logger.info(txt)
                                    break
            elif pchtrakt.isTvShow:
                a = re.split("([-|.]*[Ss]\\d\\d[Ee]\\d\\d.)", myMedia.oStatus.fileName)
                ep_name = a[2][:-4].replace(".", " ").replace("- ", "")
                season_xml = a[0][:-3].replace(".", " ").replace(" - ", "")
                f_size = str(os.path.getsize(myMedia.oStatus.fullPath))
                ep_no = '01'
                fileinfo = updatexmlwatched + "Set_" + season_xml + "*.xml"
                for name in glob.glob(fileinfo):
                    if myMedia.oStatus.fileName in open(name).read():
                        tree = ElementTree.parse(name)
                        for movie in tree.findall('*/movie/files/file'):
                            if movie.get('firstPart') == str(myMedia.parsedInfo.episode_numbers[myMedia.idxEpisode]) and movie.get('season') == str(myMedia.parsedInfo.season_number):
                                movie.set('watched', 'true')
                                bak_name = name[:-4]+'.bak'
                                tree.write(bak_name)
                                os.rename(bak_name, name)
                                txt = name.replace(updatexmlwatched, '') + ' has been modified as watched for ' + myMedia.oStatus.fileName
                                Debug(txt)
                                pchtrakt.logger.info(txt)
                                break
                fileinfo = updatexmlwatched + "Other*.xml"
                for name in glob.glob(fileinfo):
                    if myMedia.oStatus.fileName in open(name).read():
                        tree = ElementTree.parse(name)
                        for movie in tree.findall('*/movie/files/file'):
                            if movie.get('size') == f_size and movie.get('firstPart') == str(myMedia.parsedInfo.episode_numbers[myMedia.idxEpisode]) and movie.get('season') == str(myMedia.parsedInfo.season_number):
                                movie.set('watched', 'true')
                                bak_name = name[:-4]+'.bak'
                                tree.write(bak_name)
                                os.rename(bak_name, name)
                                txt = name.replace(updatexmlwatched, '') + ' has been modified as watched for ' + myMedia.oStatus.fileName
                                Debug(txt)
                                pchtrakt.logger.info(txt)
                                break

这就是我最终的结果。它确实有效,但如果 xml 在网络共享上,有时会导致正在播放的媒体卡顿。我相信它正在搜索包含变量的 xml 时发生。在低功耗媒体播放器上运行时,是否有一种不那么耗费系统的方式来执行此操作。

谢谢

于 2012-09-12T20:56:34.137 回答
0

以下将在所有电影和文件中设置为 true,您应该对其进行调整以仅更改您想要的内容,并将 movies.xml 更改为您的文件名。

from xml.etree import ElementTree

tree = ElementTree.parse('movies.xml')

for movie in tree.findall('movies/movie'):
    movie.find('watched').text = 'true'
    for mfile in movie.findall('files/file'):
        mfile.set('watched', 'true')

tree.write('movies.xml')
于 2012-08-23T21:49:40.877 回答
0

如果你在一个应该有sed它的小型媒体服务器上运行,也许 python 不是答案。

sed -i 's/<watched>false/<watched>true/g' movies.xml

只需检查sed您拥有的版本,sed -h以便了解可用的选项。

于 2015-11-23T05:22:54.747 回答
0

假设您的 baseFilenameBase 是唯一的,一个简单的方法如下:我不能评论,所以我不能将它添加到 sp 的答案中。

神奇的是 xpath 表达式 "//movie[baseFilenameBase/text() = 'PATHHERE']" 这是一个 xpath 表达式,它表示:

  • //movie -- 查找所有电影节点
  • [CONDITION] -- 条件为真
  • baseFilenameBase/text() = 'PATHHERE' -- 表示它必须有一个 baseFilenameBase 节点的条件,包含文本 'PATHHERE'

然后我使用格式将路径变量的内容放入其中。XPath 是引用 xml 节点的绝佳语法,请查看此处以获取更多信息

ps 我必须承认我没有运行这个。但我认为没有任何语法错误,它应该可以工作。

from xml.etree import ElementTree

tree = ElementTree.parse('movies.xml')
movie = tree.find("//movie[baseFilenameBase/text() = '{}']".format(path))
movie.find('watched').text = 'true'
for file in movie.find('files').findall('file')
    file.set("watched", 'true')

tree.write('movies.xml')
于 2016-03-17T11:03:28.640 回答