from datetime import datetime, timedelta from collections import namedtuple Finito = namedtuple('Finito', ['Number', 'Date', 'Title']) def assign_episode_dates(start_date, end_date, episode_list, count_from=-1): # Define the episode list # Separate regular and special episodes separator = "Melevisione racconta:" # separator = "Le merende di Balia Bea:" regular_episodes = [ep for ep in episode_list if not ep.startswith(separator)] special_episodes = [ep for ep in episode_list if ep.startswith(separator)] # Initialize pointers for regular and special episodes regular_ptr = 0 special_ptr = 0 current_date = start_date while current_date <= end_date: if current_date >= datetime(2010, 7, 1) and current_date <= datetime(2010, 8, 16): current_date += timedelta(days=1) continue weekday = current_date.weekday() # Monday=0, Sunday=6 if weekday < 4: # Monday to Thursday if regular_ptr < len(regular_episodes): yield Finito(count_from, current_date.strftime("%Y-%m-%d"), regular_episodes[regular_ptr]) count_from += 1 regular_ptr += 1 elif weekday == 4: # Friday if special_ptr < len(special_episodes): yield Finito(count_from, current_date.strftime("%Y-%m-%d"), special_episodes[special_ptr]) count_from += 1 special_ptr += 1 # Move to the next day current_date += timedelta(days=1) # Example usage if __name__ == "__main__": import sys n = 12 # start_date = datetime(2009, 2, 9) # February 9, 2009 # end_date = datetime(2009, 5, 12) # May 12, 2009 # start_date = datetime(2009, 10, 5) # 10 Oct # end_date = datetime(2009, 12, 10) # 10 Dec start_date = datetime(2009, 12, 21) # 21 Dec 2009 end_date = datetime(2010, 9, 10) # 10 Sept 2010 with open(f'{n}.list', 'r') as fp: episodes = [l.strip() for l in fp.readlines()] schedule = list(assign_episode_dates(start_date, end_date, episodes, count_from=236)) for entry in schedule: print(f"{entry}") with open(f'{n}.txt', 'w') as fp: import json fp.write(json.dumps(schedule, indent=4))