Optimization of episode calculation

This commit is contained in:
Someone 2023-12-09 23:13:28 +01:00 committed by GitHub
parent f550e32b5e
commit f86e72aa92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 17 deletions

View File

@ -1409,28 +1409,20 @@ class Media(models.Model):
return indexer(self.url) return indexer(self.url)
def calculate_episode_number(self): 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: if self.source.source_type == Source.SOURCE_TYPE_YOUTUBE_PLAYLIST:
# Calculate the episode number based on the position in the playlist sorted_media = Media.objects.filter(source=self.source)
for media in filtered_media:
if media == self:
return position_counter
position_counter += 1
else: 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 self_year = self.upload_date.year if self.upload_date else self.created.year
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: for media in sorted_media:
# Assign the calculated episode number to the media
if media == self: if media == self:
return position_counter return position_counter
year = media.upload_date.year if media.upload_date else media.created.year
if year == self_year:
position_counter += 1 position_counter += 1
class MediaServer(models.Model): class MediaServer(models.Model):
''' '''
A remote media server, such as a Plex server. A remote media server, such as a Plex server.