EmbedMetadata/standards/xmp.py

127 lines
3.6 KiB
Python

"XMP Manipulator for gallery-dl"
import logging
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
allowed_exts=[
"html", "htm", "xhtml", "xht", # HTML
"tif", "tiff", # TIFF
"jpeg", "jpg", "pjpeg", "pjpg", "jpe", # JPEG
"jp2", "jpf", "jpx", "jpm", "mj2", # JPEG 2000
"png", "apng", # PNG
"gif", # GIF
"mp3", # MPEG-1 / MPEG-2 Layer III
"mp4", "m4a", "m4p", "m4b", "m4r", "m4v", # MPEG-4
"mov", "qt", # Apple QuickTime .mov
"pdf", # PDF
"webp" # WebP
]
# def backupMetadata(file: str, outputFileExt: str = "xmp_backup.json.xz") -> bool:
# """Exports XMP metadata inside of a file.
# Parameters
# ----------
# file : str
# Location of the file relative to the working directory.
# outputFileExt : str, optional
# The suffix used for the backup file. By default, the output file will be formatted as such:
# <source filename>.xmp_backup.json.xz
# Returns
# -------
# bool
# True, if operation was successful.
# False, if operation failed.
# """
# from libxmp.utils import file_to_dict
# import json
# with open(f"{file}.xmp_backup.json.xz",'w+b') as bfile
# logging.debug(f"writing to {bfile}")
# import lzma
# bfile.write(lzma.compress(
# bytes(json.dumps(file_to_dict(file),sort_keys=True)),
# format=FORMAT_XZ,
# check=CHECK_SHA256,
# preset=9
# ))
# logging.debug("wrote backup data")
# logging.info("backup done!")
def backupMetadata(file):
import pyexiv2
data = pyexiv2.ImageMetadata(file)
data.read()
full = []
for key in data.xmp_keys:
full.append([
key,
data[key].raw_value
])
with open(f"{file}.xmp_backup.json.xz",'w+b') as bfile
import lzma, json
bfile.write(lzma.compress(
bytes(json.dumps(full)),
format=FORMAT_XZ,
check=CHECK_SHA256,
preset=9
))
# def writeTags(file: str, tags: dict) -> bool:
# """Writes XMP tags to file taking the info.json file as a dict.
# """
# from libxmp import XMPFiles, consts
# xmpfile = XMPFiles(file_path=file,)
# if artist in tags:
# if type(tags.artist) == str:
# xmp.set_property(consts.XMP_NS_DC, u'creator', tags.artist.decode('unicode-escape'))
def writeTags(file, tags):
import pyexiv2
xmptags = pyexiv2.ImageMetadata(file)
xmptags.read()
if artist in tags:
if type(tag.artist) == str:
xmptags['Xmp.dc.creator'] = tags.artist
elif tags.category == "deviantart":
xmptags['Xmp.dc.creator'] = tags.artist.username
if title in tags:
xmptags['Xmp.dc.title'] = tags.title
if description in tags:
xmptags['Xmp.dc.description'] = tags.description
if date in tags:
xmptags['Xmp.dc.date'] = tags.date
from ..misc.getpub import getPub
xmptags['Xmp.dc.publisher'] = getPub(tags)
del getPub
keywords=[]
keywords.append(tags.category)
if tags in tags:
for item in tags.tags:
if item not in keywords: keywords.append(item)
if tags.category == 'deviantart':
from ..category.deviantart import makeKeyWordList
for item in makeKeyWordList(tags):
if item not in keywords: keywords.append(item)
elif tags.category == 'furaffinity':
from ..category.furaffinity import makeKeyWordList
for item in makeKeyWordList(tags):
if item not in keywords: keywords.append(item)