cleaned up code
This commit is contained in:
parent
ea9bff2661
commit
4e575a1f4a
35
generator.py
35
generator.py
@ -1,18 +1,23 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from pod2gen import Podcast, Episode, Media, Person
|
from pod2gen import Podcast, Episode, Media, Person
|
||||||
from mutagen import mp3, mp4
|
from mutagen import mp4
|
||||||
from datetime import timedelta, datetime as dt
|
from datetime import timedelta, datetime as dt
|
||||||
from dateutil.tz import UTC
|
from dateutil.tz import UTC
|
||||||
from stat import S_ISREG, ST_CTIME, ST_MODE
|
|
||||||
|
|
||||||
|
|
||||||
def get_mp3_metadata(file_path, filename):
|
def get_mp4_metadata(file_path:str, filename:str) -> (timedelta, str):
|
||||||
|
"""Extract MP4 metadata from a file.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path (_type_): File path to the MP4 file.
|
||||||
|
str (_type_): Name of the MP4 file.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
_type_: Timedelta and description of the MP4 file.
|
||||||
|
"""
|
||||||
track_seconds = round(mp4.MP4(file_path).info.length)
|
track_seconds = round(mp4.MP4(file_path).info.length)
|
||||||
time_split = time.strftime("%H:%M:%S", time.gmtime(track_seconds)).split(":")
|
time_split = time.strftime("%H:%M:%S", time.gmtime(track_seconds)).split(":")
|
||||||
try:
|
|
||||||
description = mp4.MP4(file_path).tags["COMM::eng"].text[0]
|
|
||||||
except KeyError:
|
|
||||||
try:
|
try:
|
||||||
description = mp4.MP4(file_path).tags["\xa9cmt"][0]
|
description = mp4.MP4(file_path).tags["\xa9cmt"][0]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -26,23 +31,28 @@ def get_mp3_metadata(file_path, filename):
|
|||||||
description,
|
description,
|
||||||
)
|
)
|
||||||
|
|
||||||
def add_episodes_from_folder(folder_path):
|
|
||||||
|
def add_episodes_from_folder(folder_path:str) -> None:
|
||||||
|
"""Loop over all files in a folder and add them to the Podcast.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
folder_path (str): Folder to loop over.
|
||||||
|
"""
|
||||||
file_list = os.listdir(folder_path)
|
file_list = os.listdir(folder_path)
|
||||||
list_of_episodes = [ x for x in file_list if "m4b" in x ]
|
list_of_episodes = [x for x in file_list if "m4b" in x]
|
||||||
# file_list.pop(0)
|
# file_list.pop(0)
|
||||||
for file in list_of_episodes:
|
for file in list_of_episodes:
|
||||||
filename = file[:-4]
|
filename = file[:-4]
|
||||||
ep_details = filename.split(" ")
|
ep_details = filename.split(" ")
|
||||||
file_size = os.stat(f"./{folder_path}/{file}").st_size
|
file_size = os.stat(f"./{folder_path}/{file}").st_size
|
||||||
file_length, description = get_mp3_metadata(f"./{folder_path}/{file}", filename)
|
file_length, description = get_mp4_metadata(f"./{folder_path}/{file}", filename)
|
||||||
try:
|
|
||||||
p.add_episode(
|
p.add_episode(
|
||||||
Episode(
|
Episode(
|
||||||
title=mp4.MP4(f"./{folder_path}/{file}").tags["\xa9nam"][0],
|
title=mp4.MP4(f"./{folder_path}/{file}").tags["\xa9nam"][0],
|
||||||
media=Media(
|
media=Media(
|
||||||
f"https://kithub.k-world.me.uk/Karl/KFMPod/raw/master/{folder_path}/{file}",
|
f"https://kithub.k-world.me.uk/Karl/KFMPod/raw/master/{folder_path}/{file}",
|
||||||
size=file_size,
|
size=file_size,
|
||||||
type='mp3'
|
type="mp3",
|
||||||
),
|
),
|
||||||
summary=description,
|
summary=description,
|
||||||
season=[int(i) for i in folder_path.split() if i.isdigit()][0],
|
season=[int(i) for i in folder_path.split() if i.isdigit()][0],
|
||||||
@ -52,8 +62,7 @@ def add_episodes_from_folder(folder_path):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
owner = Person("Example", "email@example.com")
|
owner = Person("Example", "email@example.com")
|
||||||
# Create the Podcast
|
# Create the Podcast
|
||||||
|
Loading…
x
Reference in New Issue
Block a user