|
Hi, thanks for the tip.
So I asked chatgpt to write one for me and it wrote this script:
from calibre.customize import MetadataReaderPlugin
from lxml import etree
import re
class TranslatorsPlugin(MetadataReaderPlugin):
name = 'Translators Metadata Plugin'
description = 'Extracts translator names from OPF and stores them in #translators'
supported_platforms = ['windows', 'osx', 'linux']
author = 'ChatGPT'
version = (1, 2, 0)
file_types = {'opf'}
def read_metadata(self, stream, metadata):
tree = etree.parse(stream)
translators = []
# Match all meta tags refining any #translator reference (#translator, #translator-1, etc.)
for meta in tree.xpath('//meta[@property="se:name.person.full-name" and starts-with(@refines, "#translator")]'):
if meta.text:
translators.append(meta.text.strip())
# Fallback to generic <meta name="translator">
for meta in tree.xpath('//meta[@name="translator"]'):
if meta.get('content'):
translators.append(meta.get('content').strip())
# Fallback to <dc:contributor opf:role="trl">
for node in tree.xpath('//dc:contributor[@opf:role="trl"]',
namespaces={'dc': 'http://purl.org/dc/elements/1.1/',
'opf': 'http://www.idpf.org/2007/opf'}):
if node.text:
translators.append(node.text.strip())
if translators:
metadata.set('#translators', ', '.join(sorted(set(translators))))
return metadata
In the opf file, the translators are like this:
<dc:contributor id="translator">Gilbert Murray</dc:contributor>
<meta property="file-as" refines="#translator">Murray, Gilbert</meta>
<meta property="se:name.person.full-name" refines="#translator">George Gilbert Aimé Murray</meta>
<meta property="se:url.encyclopedia.wikipedia" refines="#translator">https://en.wikipedia.org/wiki/Gilbert_Murray</meta>
<meta property="se:url.authority.nacoaf" refines="#translator">http://id.loc.gov/authorities/names/n79043229</meta>
<meta property="role" refines="#translator" scheme="marc:relators">ann</meta>
<meta property="role" refines="#translator" scheme="marc:relators">trl</meta>
<meta property="role" refines="#translator" scheme="marc:relators">wpr</meta>
But it's still not working. Can you point me in the right direction?
|