intervals.py 852 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import re
  2. def get_intervals(text: str):
  3. intervals = []
  4. for line in text.split("\n"):
  5. timepoints = re.findall(r"[\d\d:]+\d\d", line)
  6. if not timepoints:
  7. continue
  8. timepoint = timepoints[0]
  9. title = line.replace(timepoint, "").strip()
  10. intervals.append((timepoint, title))
  11. return _convert_intervals(intervals)
  12. def _convert_intervals(intervals):
  13. intervals_list = []
  14. for timepoint, title in intervals:
  15. timepoint = ":".join(
  16. ["0"*(2 - len(time)) + time for time in timepoint.split(":")]
  17. )
  18. timepoint = "00:"*(3 - len(timepoint.split(":"))) + timepoint
  19. intervals_list.append({
  20. "start": timepoint,
  21. "title": title
  22. })
  23. for i in range(len(intervals_list)):
  24. if i < len(intervals_list) - 1:
  25. intervals_list[i]["end"] = intervals_list[i+1]["start"]
  26. else:
  27. intervals_list[i]["end"] = "99:59:59"
  28. return intervals_list