generator.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import os
  2. import time
  3. from pod2gen import Podcast, Episode, Media, Person
  4. from mutagen import mp3, mp4
  5. from datetime import timedelta, datetime as dt
  6. from dateutil.tz import UTC
  7. from stat import S_ISREG, ST_CTIME, ST_MODE
  8. def get_mp3_metadata(file_path, filename):
  9. track_seconds = round(mp4.MP4(file_path).info.length)
  10. time_split = time.strftime("%H:%M:%S", time.gmtime(track_seconds)).split(":")
  11. try:
  12. description = mp4.MP4(file_path).tags["COMM::eng"].text[0]
  13. except KeyError:
  14. try:
  15. description = mp4.MP4(file_path).tags["\xa9cmt"][0]
  16. except KeyError:
  17. description = filename.replace("_", " ")
  18. return (
  19. timedelta(
  20. hours=int(time_split[0]),
  21. minutes=int(time_split[1]),
  22. seconds=int(time_split[2]),
  23. ),
  24. description,
  25. )
  26. def add_episodes_from_folder(folder_path):
  27. file_list = os.listdir(folder_path)
  28. list_of_episodes = [ x for x in file_list if "m4b" in x ]
  29. # file_list.pop(0)
  30. for file in list_of_episodes:
  31. filename = file[:-4]
  32. ep_details = filename.split(" ")
  33. file_size = os.stat(f"./{folder_path}/{file}").st_size
  34. file_length, description = get_mp3_metadata(f"./{folder_path}/{file}", filename)
  35. try:
  36. p.add_episode(
  37. Episode(
  38. title=mp4.MP4(f"./{folder_path}/{file}").tags["\xa9nam"][0],
  39. media=Media(
  40. f"https://kithub.k-world.me.uk/Karl/KFMPod/raw/master/{folder_path}/{file}",
  41. size=file_size,
  42. type='mp3'
  43. ),
  44. summary=description,
  45. season=[int(i) for i in folder_path.split() if i.isdigit()][0],
  46. episode_number=int(ep_details[1][-0]),
  47. publication_date=dt.fromtimestamp(
  48. os.stat(f"./{folder_path}/{file}").st_ctime, tz=UTC
  49. ),
  50. )
  51. )
  52. except Exception:
  53. pass
  54. owner = Person("Example", "email@example.com")
  55. # Create the Podcast
  56. p = Podcast(
  57. name="Kurupt FM Podcast",
  58. description="Kurupt FM Podcast",
  59. website="https://kithub.k-world.me.uk/Karl/KFMPod",
  60. explicit=False,
  61. )
  62. p.image = "https://kithub.k-world.me.uk/Karl/KFMPod/raw/master/Season%201/Ep.%201%ea%9e%89%20Music%20[B08DFKGFP8].jpg"
  63. p.owner = owner
  64. p.locked = True
  65. p.withhold_from_itunes = True
  66. add_episodes_from_folder("Season 1")
  67. add_episodes_from_folder("Season 2")
  68. add_episodes_from_folder("Season 3")
  69. # Generate the RSS feed
  70. p.rss_file("feed.xml", minimize=True)