mirror of
https://github.com/SoPat712/BeReal-Export-Manager.git
synced 2025-08-22 02:38:45 -04:00
revised code
This commit is contained in:
39
main.py
39
main.py
@@ -6,6 +6,7 @@ from datetime import datetime as dt
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def init_parser() -> argparse.Namespace:
|
def init_parser() -> argparse.Namespace:
|
||||||
"""
|
"""
|
||||||
Initializes the argparse module.
|
Initializes the argparse module.
|
||||||
@@ -30,6 +31,8 @@ class BeRealExporter:
|
|||||||
self.time_span = self.init_time_span(args)
|
self.time_span = self.init_time_span(args)
|
||||||
self.out_path = args.path.strip().removesuffix('/') if args.path else "./out"
|
self.out_path = args.path.strip().removesuffix('/') if args.path else "./out"
|
||||||
self.verbose = args.verbose
|
self.verbose = args.verbose
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_time_span(args: argparse.Namespace) -> tuple:
|
def init_time_span(args: argparse.Namespace) -> tuple:
|
||||||
"""
|
"""
|
||||||
@@ -44,16 +47,21 @@ class BeRealExporter:
|
|||||||
return dt(args.year, 1, 1), dt(args.year, 12, 31)
|
return dt(args.year, 1, 1), dt(args.year, 12, 31)
|
||||||
else:
|
else:
|
||||||
return dt.fromtimestamp(0), dt.now()
|
return dt.fromtimestamp(0), dt.now()
|
||||||
|
|
||||||
|
|
||||||
def verbose_msg(self, msg: str):
|
def verbose_msg(self, msg: str):
|
||||||
"""
|
"""
|
||||||
Prints an explanation of what is being done to the terminal.
|
Prints an explanation of what is being done to the terminal.
|
||||||
"""
|
"""
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def print_progress_bar(iteration: int, total: int, prefix: str = '', suffix: str = '', decimals: int = 1, length: int = 60, fill: str = '█', print_end: str = "\r"):
|
def print_progress_bar(iteration: int, total: int, prefix: str = '', suffix: str = '', decimals: int = 1, length: int = 60, fill: str = '█', print_end: str = "\r"):
|
||||||
"""
|
"""
|
||||||
Call in a loop to create terminal progress bar.
|
Call in a loop to create terminal progress bar.
|
||||||
|
Not my creation: https://stackoverflow.com/questions/3173320/text-progress-bar-in-terminal-with-block-characters
|
||||||
"""
|
"""
|
||||||
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
|
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
|
||||||
filled_length = int(length * iteration // total)
|
filled_length = int(length * iteration // total)
|
||||||
@@ -61,12 +69,16 @@ class BeRealExporter:
|
|||||||
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
|
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end)
|
||||||
if iteration == total:
|
if iteration == total:
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_img_filename(image: dict) -> str:
|
def get_img_filename(image: dict) -> str:
|
||||||
"""
|
"""
|
||||||
Returns the image filename from an image object (frontImage, backImage, primary, secondary).
|
Returns the image filename from an image object (frontImage, backImage, primary, secondary).
|
||||||
"""
|
"""
|
||||||
return image['path'].split("/")[-1]
|
return image['path'].split("/")[-1]
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_datetime_from_str(time: str) -> dt:
|
def get_datetime_from_str(time: str) -> dt:
|
||||||
"""
|
"""
|
||||||
@@ -74,6 +86,8 @@ class BeRealExporter:
|
|||||||
"""
|
"""
|
||||||
format_string = "%Y-%m-%dT%H:%M:%S.%fZ"
|
format_string = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||||
return dt.strptime(time, format_string)
|
return dt.strptime(time, format_string)
|
||||||
|
|
||||||
|
|
||||||
def export_img(self, old_img_name: str, img_name: str, img_dt: dt, img_location=None):
|
def export_img(self, old_img_name: str, img_name: str, img_dt: dt, img_location=None):
|
||||||
"""
|
"""
|
||||||
Makes a copy of the image and adds EXIF tags to the image.
|
Makes a copy of the image and adds EXIF tags to the image.
|
||||||
@@ -90,6 +104,8 @@ class BeRealExporter:
|
|||||||
else:
|
else:
|
||||||
self.verbose_msg(f"Add metadata to image:\n - DateTimeOriginal={img_dt}")
|
self.verbose_msg(f"Add metadata to image:\n - DateTimeOriginal={img_dt}")
|
||||||
et().set_tags(img_name, tags=tags, params=["-P", "-overwrite_original"])
|
et().set_tags(img_name, tags=tags, params=["-P", "-overwrite_original"])
|
||||||
|
|
||||||
|
|
||||||
def export_memories(self, memories: list):
|
def export_memories(self, memories: list):
|
||||||
"""
|
"""
|
||||||
Exports all memories from the Photos/post directory to the corresponding output folder.
|
Exports all memories from the Photos/post directory to the corresponding output folder.
|
||||||
@@ -99,21 +115,28 @@ class BeRealExporter:
|
|||||||
if not os.path.exists(out_path_memories):
|
if not os.path.exists(out_path_memories):
|
||||||
self.verbose_msg(f"Create {out_path_memories} folder for memories output")
|
self.verbose_msg(f"Create {out_path_memories} folder for memories output")
|
||||||
os.makedirs(out_path_memories)
|
os.makedirs(out_path_memories)
|
||||||
for n, memory in enumerate(memories):
|
|
||||||
|
for i, memory in enumerate(memories):
|
||||||
memory_dt = self.get_datetime_from_str(memory['takenTime'])
|
memory_dt = self.get_datetime_from_str(memory['takenTime'])
|
||||||
types = [('frontImage', 'webp'), ('backImage', 'webp')]
|
types = [('frontImage', 'webp'), ('backImage', 'webp')]
|
||||||
if 'btsMedia' in memory:
|
if 'btsMedia' in memory:
|
||||||
types.append(('btsMedia', 'mp4'))
|
types.append(('btsMedia', 'mp4'))
|
||||||
img_names = [f"{out_path_memories}/{memory_dt.strftime('%Y-%m-%d_%H-%M-%S')}_{t[0].removesuffix('Image').removesuffix('Media')}.{t[1]}" for t in types]
|
img_names = [f"{out_path_memories}/{memory_dt.strftime('%Y-%m-%d_%H-%M-%S')}_{t[0].removesuffix('Image').removesuffix('Media')}.{t[1]}"
|
||||||
|
for t in types]
|
||||||
|
|
||||||
if self.time_span[0] <= memory_dt <= self.time_span[1]:
|
if self.time_span[0] <= memory_dt <= self.time_span[1]:
|
||||||
for img_name, type in zip(img_names, types):
|
for img_name, type in zip(img_names, types):
|
||||||
old_img_name = f"./Photos/post/{self.get_img_filename(memory[type[0]])}"
|
old_img_name = f"./Photos/post/{self.get_img_filename(memory[type[0]])}"
|
||||||
self.verbose_msg(f"\nExport Memory nr {n} {type[0]}:")
|
self.verbose_msg(f"Export Memory nr {i} {type[0]}:")
|
||||||
if 'location' in memory:
|
if 'location' in memory:
|
||||||
self.export_img(old_img_name, img_name, memory_dt, memory['location'])
|
self.export_img(old_img_name, img_name, memory_dt, memory['location'])
|
||||||
else:
|
else:
|
||||||
self.export_img(old_img_name, img_name, memory_dt)
|
self.export_img(old_img_name, img_name, memory_dt)
|
||||||
self.print_progress_bar(n + 1, memory_count, prefix="Exporting Memories", suffix=f"- {memory_dt.strftime('%Y-%m-%d')}")
|
|
||||||
|
self.print_progress_bar(i + 1, memory_count, prefix="Exporting Memories", suffix=f"- {memory_dt.strftime('%Y-%m-%d')}")
|
||||||
|
self.verbose_msg(f"\n\n{'#'*100}\n")
|
||||||
|
|
||||||
|
|
||||||
def export_realmojis(self, realmojis: list):
|
def export_realmojis(self, realmojis: list):
|
||||||
"""
|
"""
|
||||||
Exports all realmojis from the Photos/realmoji directory to the corresponding output folder.
|
Exports all realmojis from the Photos/realmoji directory to the corresponding output folder.
|
||||||
@@ -123,13 +146,15 @@ class BeRealExporter:
|
|||||||
if not os.path.exists(out_path_realmojis):
|
if not os.path.exists(out_path_realmojis):
|
||||||
self.verbose_msg(f"Create {out_path_realmojis} folder for memories output")
|
self.verbose_msg(f"Create {out_path_realmojis} folder for memories output")
|
||||||
os.makedirs(out_path_realmojis)
|
os.makedirs(out_path_realmojis)
|
||||||
for n, realmoji in enumerate(realmojis):
|
|
||||||
|
for i, realmoji in enumerate(realmojis):
|
||||||
realmoji_dt = self.get_datetime_from_str(realmoji['postedAt'])
|
realmoji_dt = self.get_datetime_from_str(realmoji['postedAt'])
|
||||||
img_name = f"{out_path_realmojis}/{realmoji_dt.strftime('%Y-%m-%d_%H-%M-%S')}.webp"
|
img_name = f"{out_path_realmojis}/{realmoji_dt.strftime('%Y-%m-%d_%H-%M-%S')}.webp"
|
||||||
if self.time_span[0] <= realmoji_dt <= self.time_span[1] and realmoji['isInstant']:
|
if self.time_span[0] <= realmoji_dt <= self.time_span[1] and realmoji['isInstant']:
|
||||||
self.verbose_msg(f"\nExport Realmoji nr {n}:")
|
self.verbose_msg(f"Export Realmoji nr {i}:")
|
||||||
self.export_img(f"./Photos/realmoji/{self.get_img_filename(realmoji['media'])}", img_name, realmoji_dt)
|
self.export_img(f"./Photos/realmoji/{self.get_img_filename(realmoji['media'])}", img_name, realmoji_dt)
|
||||||
self.print_progress_bar(n + 1, realmoji_count, prefix="Exporting Realmojis", suffix=f"- Current Date: {realmoji_dt.strftime('%Y-%m-%d')}")
|
self.print_progress_bar(i + 1, realmoji_count, prefix="Exporting Realmojis", suffix=f"- {realmoji_dt.strftime('%Y-%m-%d')}")
|
||||||
|
self.verbose_msg(f"\n\n{'#'*100}\n")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user