generator.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import os
  2. import time
  3. from pod2gen import Podcast, Episode, Media, Person
  4. from mutagen import mp3
  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):
  9. track_seconds = round(mp3.MP3(file_path).info.length)
  10. time_split = time.strftime('%H:%M:%S', time.gmtime(track_seconds)).split(':')
  11. try:
  12. description = mp3.MP3(file_path).tags['COMM::eng'].text[0]
  13. except KeyError:
  14. try:
  15. description = mp3.MP3(file_path).tags['TXXX:COMM::eng'].text[0]
  16. except KeyError:
  17. description = filename.replace("_", " ")
  18. return timedelta(hours=int(time_split[0]), minutes=int(time_split[1]), seconds=int(time_split[2])), description
  19. owner = Person("Example", "email@example.com")
  20. # Create the Podcast
  21. p = Podcast(
  22. name="Rob Beckett and Josh Widdicombe's Parenting Hell",
  23. description="Rob Beckett and Josh Widdicombe's Parenting Hell",
  24. website="https://kithub.k-world.me.uk/Karl/RandJPod",
  25. explicit=False,
  26. )
  27. p.image = "https://kithub.k-world.me.uk/Karl/RandJPod/raw/main/cover.jpeg"
  28. p.owner = owner
  29. p.locked = True
  30. p.withhold_from_itunes = True
  31. file_list = os.listdir("mp3")
  32. # file_list.pop(0)
  33. for file in (file_list):
  34. filename = file[:-4]
  35. ep_details = filename.split("_")
  36. file_size = os.stat(f"./mp3/{filename}.mp3").st_size
  37. file_length, description = get_mp3_metadata(f"./mp3/{filename}.mp3")
  38. p.add_episode(Episode(
  39. title=filename.replace("_", " "),
  40. media=Media(f"https://kithub.k-world.me.uk/Karl/RandJPod/raw/main/mp3/{filename}.mp3", size=file_size),
  41. summary=description,
  42. season=int(ep_details[0][1:]),
  43. episode_number=int(ep_details[1][2:]),
  44. publication_date = dt.fromtimestamp(os.stat(f"./mp3/{filename}.mp3").st_ctime, tz=UTC)
  45. ))
  46. # Generate the RSS feed
  47. p.rss_file('feed.xml', minimize=True)