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,26 +1409,18 @@ 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
for media in sorted_media: filtered_media = Media.objects.filter(source=self.source, published__year=self_year)
# Assign the calculated episode number to the media sorted_media = sorted(filtered_media, key=lambda x: (x.upload_date, x.key))
if media == self:
return position_counter position_counter = 1
year = media.upload_date.year if media.upload_date else media.created.year for media in sorted_media:
if year == self_year: if media == self:
position_counter += 1 return position_counter
position_counter += 1
class MediaServer(models.Model): class MediaServer(models.Model):