mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-06-29 03:26:16 +00:00
remove unnecessary sass
clean up metadata parser (kinda)
This commit is contained in:
parent
3c09fb494b
commit
4c7bf9706f
9 changed files with 209 additions and 286 deletions
|
@ -0,0 +1 @@
|
|||
# :3
|
|
@ -12,90 +12,74 @@ from .helpers import *
|
|||
from .mapping import *
|
||||
|
||||
|
||||
class Metadata:
|
||||
def yoink(file_path):
|
||||
"""
|
||||
Metadata parser
|
||||
Initialize the metadata parser
|
||||
"""
|
||||
if not os.path.isfile(file_path):
|
||||
return None
|
||||
|
||||
img_exif = {}
|
||||
file = Image.open(file_path)
|
||||
|
||||
img_exif["FileName"] = os.path.basename(file_path)
|
||||
img_exif["FileSize"] = os.path.getsize(file_path)
|
||||
img_exif["FileFormat"] = img_exif["FileName"].split(".")[-1]
|
||||
img_exif["FileWidth"], img_exif["FileHeight"] = file.size
|
||||
|
||||
def __init__(self, file_path):
|
||||
"""
|
||||
Initialize the metadata parser
|
||||
"""
|
||||
self.file_path = file_path
|
||||
img_exif = {}
|
||||
try:
|
||||
tags = file._getexif()
|
||||
for tag, value in TAGS.items():
|
||||
if tag in tags:
|
||||
img_exif[value] = tags[tag]
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
file.close()
|
||||
|
||||
return _format_data(img_exif)
|
||||
|
||||
try:
|
||||
file = Image.open(file_path)
|
||||
tags = file._getexif()
|
||||
img_exif = {}
|
||||
|
||||
for tag, value in TAGS.items():
|
||||
if tag in tags:
|
||||
img_exif[value] = tags[tag]
|
||||
def _format_data(encoded):
|
||||
"""
|
||||
Formats the data into a dictionary
|
||||
"""
|
||||
exif = {
|
||||
"Photographer": {},
|
||||
"Camera": {},
|
||||
"Software": {},
|
||||
"File": {},
|
||||
}
|
||||
|
||||
img_exif["FileName"] = os.path.basename(file_path)
|
||||
img_exif["FileSize"] = os.path.getsize(file_path)
|
||||
img_exif["FileFormat"] = img_exif["FileName"].split(".")[-1]
|
||||
img_exif["FileWidth"], img_exif["FileHeight"] = file.size
|
||||
# Thanks chatGPT xP
|
||||
# the helper function works, so not sure why it triggers pylint
|
||||
for key, value in encoded.items():
|
||||
for mapping_name, mapping_val in EXIF_MAPPING:
|
||||
if key in mapping_val:
|
||||
if len(mapping_val[key]) == 2:
|
||||
exif[mapping_name][mapping_val[key][0]] = {
|
||||
"raw": value,
|
||||
"formatted": (
|
||||
getattr(
|
||||
helpers, # pylint: disable=E0602
|
||||
mapping_val[key][1],
|
||||
)(value)
|
||||
),
|
||||
}
|
||||
else:
|
||||
exif[mapping_name][mapping_val[key][0]] = {
|
||||
"raw": value,
|
||||
}
|
||||
continue
|
||||
|
||||
file.close()
|
||||
except TypeError:
|
||||
img_exif["FileName"] = os.path.basename(file_path)
|
||||
img_exif["FileSize"] = os.path.getsize(file_path)
|
||||
img_exif["FileFormat"] = img_exif["FileName"].split(".")[-1]
|
||||
img_exif["FileWidth"], img_exif["FileHeight"] = file.size
|
||||
# Remove empty keys
|
||||
if not exif["Photographer"]:
|
||||
del exif["Photographer"]
|
||||
if not exif["Camera"]:
|
||||
del exif["Camera"]
|
||||
if not exif["Software"]:
|
||||
del exif["Software"]
|
||||
if not exif["File"]:
|
||||
del exif["File"]
|
||||
|
||||
self.encoded = img_exif
|
||||
|
||||
def yoink(self):
|
||||
"""
|
||||
Yoinks the metadata from the image
|
||||
"""
|
||||
if not os.path.isfile(self.file_path):
|
||||
return None
|
||||
return self.format_data(self.encoded)
|
||||
|
||||
@staticmethod
|
||||
def format_data(encoded_exif):
|
||||
"""
|
||||
Formats the data into a dictionary
|
||||
"""
|
||||
exif = {
|
||||
"Photographer": {},
|
||||
"Camera": {},
|
||||
"Software": {},
|
||||
"File": {},
|
||||
}
|
||||
|
||||
# Thanks chatGPT xP
|
||||
# the helper function works, so not sure why it triggers pylint
|
||||
for key, value in encoded_exif.items():
|
||||
for mapping_name, mapping_val in EXIF_MAPPING:
|
||||
if key in mapping_val:
|
||||
if len(mapping_val[key]) == 2:
|
||||
exif[mapping_name][mapping_val[key][0]] = {
|
||||
"raw": value,
|
||||
"formatted": (
|
||||
getattr(
|
||||
helpers, # pylint: disable=E0602
|
||||
mapping_val[key][1],
|
||||
)(value)
|
||||
),
|
||||
}
|
||||
else:
|
||||
exif[mapping_name][mapping_val[key][0]] = {
|
||||
"raw": value,
|
||||
}
|
||||
continue
|
||||
|
||||
# Remove empty keys
|
||||
if not exif["Photographer"]:
|
||||
del exif["Photographer"]
|
||||
if not exif["Camera"]:
|
||||
del exif["Camera"]
|
||||
if not exif["Software"]:
|
||||
del exif["Software"]
|
||||
if not exif["File"]:
|
||||
del exif["File"]
|
||||
|
||||
return exif
|
||||
return exif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue