From b9b702ab85488c84408f2d0ed67e6fbf418876fe Mon Sep 17 00:00:00 2001 From: Someone <10882916+InterN0te@users.noreply.github.com> Date: Sat, 9 Dec 2023 17:55:17 +0100 Subject: [PATCH 1/4] Add season and episode tags in NFO --- tubesync/sync/models.py | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index a5b8d68..7fd8c9e 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1269,6 +1269,21 @@ class Media(models.Model): showtitle.text = str(self.source.name).strip() showtitle.tail = '\n ' nfo.append(showtitle) + # season = upload date year + season = nfo.makeelement('season', {}) + if self.source.source_type == Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: + # If it's a playlist, set season to 1 + season.text = '1' + else: + # If it's not a playlist, set season to upload date year + season.text = str(self.upload_date.year) if self.upload_date else '' + season.tail = '\n ' + nfo.append(season) + # episode = number of video in the year + episode = nfo.makeelement('episode', {}) + episode.text = str(self.calculate_episode_number()) # Remplacez par la logique de calcul + episode.tail = '\n ' + nfo.append(episode) # ratings = media metadata youtube rating value = nfo.makeelement('value', {}) value.text = str(self.rating) @@ -1393,6 +1408,38 @@ class Media(models.Model): f'has no indexer') return indexer(self.url) + def calculate_episode_number(self): + filtered_media = Media.objects.filter(source=self.source) + if self.source.source_type == Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: + # Calculate the episode number based on the position in the playlist + position_counter = 1 + + for media in filtered_media: + if media == self: + return position_counter + position_counter += 1 + else: + # Sort the filtered media by upload_date in ascending order and video ID + sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.id)) + + # Initialize an episode counter by year + episode_counter = {} + + self_year = self.upload_date.year if self.upload_date else self.created.year + + for media in sorted_media: + year = media.upload_date.year + if year not in episode_counter: + # If it's the first video of the year, initialize the counter to 1 + episode_counter[year] = 1 + else: + # If it's not the first video of the year, use the current counter + episode_counter[year] += 1 + + # Assign the calculated episode number to the media + if media == self: + return episode_counter[self_year] + class MediaServer(models.Model): ''' From 034d877d6a5d1d4db3ac70fb44832d9abd8fc107 Mon Sep 17 00:00:00 2001 From: Someone <10882916+InterN0te@users.noreply.github.com> Date: Sat, 9 Dec 2023 17:56:32 +0100 Subject: [PATCH 2/4] Add season and episode tags in NFO test --- tubesync/sync/tests.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tubesync/sync/tests.py b/tubesync/sync/tests.py index 1ca2643..bcaf9cc 100644 --- a/tubesync/sync/tests.py +++ b/tubesync/sync/tests.py @@ -661,6 +661,8 @@ class MediaTestCase(TestCase): '', ' no fancy stuff title', ' testname', + ' 2020', + ' 1', ' ', ' ', ' 1.2345', From f550e32b5eca9459fb33a0a486beb9ee4e9cacd1 Mon Sep 17 00:00:00 2001 From: Someone <10882916+InterN0te@users.noreply.github.com> Date: Sat, 9 Dec 2023 19:33:59 +0100 Subject: [PATCH 3/4] Fix secondary sorting on video key --- tubesync/sync/models.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 7fd8c9e..46dca66 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1410,35 +1410,25 @@ class Media(models.Model): def calculate_episode_number(self): filtered_media = Media.objects.filter(source=self.source) + position_counter = 1 if self.source.source_type == Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: # Calculate the episode number based on the position in the playlist - position_counter = 1 - for media in filtered_media: if media == self: return position_counter position_counter += 1 else: # Sort the filtered media by upload_date in ascending order and video ID - sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.id)) - - # Initialize an episode counter by year - episode_counter = {} - + sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.key)) self_year = self.upload_date.year if self.upload_date else self.created.year - for media in sorted_media: - year = media.upload_date.year - if year not in episode_counter: - # If it's the first video of the year, initialize the counter to 1 - episode_counter[year] = 1 - else: - # If it's not the first video of the year, use the current counter - episode_counter[year] += 1 - # Assign the calculated episode number to the media if media == self: - return episode_counter[self_year] + return position_counter + year = media.upload_date.year if media.upload_date else media.created.year + if year == self_year: + position_counter += 1 + class MediaServer(models.Model): From f86e72aa92c2941b85fc750b54acd6663327d31e Mon Sep 17 00:00:00 2001 From: Someone <10882916+InterN0te@users.noreply.github.com> Date: Sat, 9 Dec 2023 23:13:28 +0100 Subject: [PATCH 4/4] Optimization of episode calculation --- tubesync/sync/models.py | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 46dca66..dff8063 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -1409,26 +1409,18 @@ class Media(models.Model): return indexer(self.url) def calculate_episode_number(self): - filtered_media = Media.objects.filter(source=self.source) - position_counter = 1 if self.source.source_type == Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: - # Calculate the episode number based on the position in the playlist - for media in filtered_media: - if media == self: - return position_counter - position_counter += 1 + sorted_media = Media.objects.filter(source=self.source) else: - # Sort the filtered media by upload_date in ascending order and video ID - sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.key)) self_year = self.upload_date.year if self.upload_date else self.created.year - for media in sorted_media: - # Assign the calculated episode number to the media - if media == self: - return position_counter - year = media.upload_date.year if media.upload_date else media.created.year - if year == self_year: - position_counter += 1 - + filtered_media = Media.objects.filter(source=self.source, published__year=self_year) + sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.key)) + + position_counter = 1 + for media in sorted_media: + if media == self: + return position_counter + position_counter += 1 class MediaServer(models.Model):