Spaces:
Sleeping
Sleeping
Commit ·
321dbb9
1
Parent(s): 08e5d35
improve recent
Browse filesFiles changed (2)
- services.py+22-24
- tvdb.py+22-3
1 unmodified line23456789101112131415161718192021221 unmodified line24252627282930313233343536373839404142434445464748495051525354555657585960616263646566671 unmodified lineclass RecentList:def __init__(self):# Initialize dictionaries to store titles and yearsself.films = {}self.series = {}# Initialize lists to keep track of the sorted entriesself.sorted_films = []self.sorted_series = []def add_entry(self, title, year, media_type):if media_type == 'film':self._update_entry(self.films, self.sorted_films, title, year)elif media_type == 'series':self._update_entry(self.series, self.sorted_series, title, year)def _update_entry(self, dictionary, sorted_list, title, year):try:# Convert year to integeryear = int(year)1 unmodified lineraise ValueError(f"Invalid year: {year}. Year must be an integer.")if title in dictionary:# Remove the old year from the sorted list if it existsold_year = dictionary[title]try:sorted_list.remove((-old_year, title))except ValueError:pass # Ignore if the old entry does not exist in the sorted list# Update or add the new entry in the dictionarydictionary[title] = year# Insert the new year into the sorted listbisect.insort(sorted_list, (-year, title))def get_sorted_entries(self, media_type):if media_type == 'film':# Convert sorted list back to the correct format with positive yearsreturn [ (title, -year) for year, title in self.sorted_films ]elif media_type == 'series':# Convert sorted list back to the correct format with positive yearsreturn [ (title, -year) for year, title in self.sorted_series ]# Example usage:if __name__ == "__main__":media_list = RecentList()# Adding entriesmedia_list.add_entry("Film A", 2022, 'film')media_list.add_entry("Series A", 2023, 'series')media_list.add_entry("Film B", 2021, 'film')media_list.add_entry("Film A", 2024, 'film') # Updating the year of "Film A"media_list.add_entry("Series B", 2021, 'series')# Retrieving and printing sorted entriesprint("Sorted Films:")for title, year in media_list.get_sorted_entries('film'):print(f"Title: {title}, Year: {year}")print("\nSorted Series:")for title, year in media_list.get_sorted_entries('series'):print(f"Title: {title}, Year: {year}")1 unmodified line234567891011121314151617181920211 unmodified line232425262728293031323334353637383940414243444546474849505152535455565758596061626364651 unmodified lineclass RecentList:def __init__(self):# Initialize dictionaries to store titles and associated dataself.films = {}self.series = {}# Initialize lists to keep track of the sorted entriesself.sorted_films = []self.sorted_series = []def add_entry(self, title, year, description, image_link, media_type):if media_type == 'film':self._update_entry(self.films, self.sorted_films, title, year, description, image_link)elif media_type == 'series':self._update_entry(self.series, self.sorted_series, title, year, description, image_link)def _update_entry(self, dictionary, sorted_list, title, year, description, image_link):try:# Convert year to integeryear = int(year)1 unmodified lineraise ValueError(f"Invalid year: {year}. Year must be an integer.")if title in dictionary:# Remove the old entry from the sorted list if it existsold_year = dictionary[title][0] # Get the old yeartry:sorted_list.remove((-old_year, title))except ValueError:pass # Ignore if the old entry does not exist in the sorted list# Update or add the new entry in the dictionarydictionary[title] = (year, description, image_link)# Insert the new year and title into the sorted listbisect.insort(sorted_list, (-year, title))def get_sorted_entries(self, media_type):if media_type == 'film':# Get sorted films with detailsreturn [(title, -year, self.films[title][1], self.films[title][2]) for year, title in self.sorted_films]elif media_type == 'series':# Get sorted series with detailsreturn [(title, -year, self.series[title][1], self.series[title][2]) for year, title in self.sorted_series]# Example usage:if __name__ == "__main__":media_list = RecentList()# Adding entriesmedia_list.add_entry("Film A", 2022, "Description A", "http://link-to-image-a.com", 'film')media_list.add_entry("Series A", 2023, "Description B", "http://link-to-image-b.com", 'series')media_list.add_entry("Film B", 2021, "Description C", "http://link-to-image-c.com", 'film')media_list.add_entry("Film A", 2024, "Updated Description A", "http://updated-link-to-image-a.com", 'film') # Updating the year of "Film A"media_list.add_entry("Series B", 2021, "Description D", "http://link-to-image-d.com", 'series')# Retrieving and printing sorted entriesprint("Sorted Films:")for title, year, description, image_link in media_list.get_sorted_entries('film'):print(f"Title: {title}, Year: {year}, Description: {description}, Image Link: {image_link}")print("\nSorted Series:")for title, year, description, image_link in media_list.get_sorted_entries('series'):print(f"Title: {title}, Year: {year}, Description: {description}, Image Link: {image_link}")
118 unmodified lines119120121122123124125126127128129118 unmodified linesresponse = requests.get(extended_url, headers=headers)response.raise_for_status()extended_data = response.json()cleaned_data=clean_data(extended_data)if media_type == 'movie':recent_list.add_entry(original_title, cleaned_data['data']['year'], 'film')elif media_type == 'series':recent_list.add_entry(original_title, cleaned_data['data']['year'], 'series')# Cache the extended JSON responsejson_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(original_title)}.json")await save_to_json(cleaned_data, json_cache_path)118 unmodified lines119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148118 unmodified linesresponse = requests.get(extended_url, headers=headers)response.raise_for_status()extended_data = response.json()cleaned_data = clean_data(extended_data)# Extract the English descriptiondescription = Noneif cleaned_data['data']['translations'] and cleaned_data['data']['translations']['overviewTranslations']:for overview in cleaned_data['data']['translations']['overviewTranslations']:if overview['language'] == 'eng' and overview['isPrimary']:description = overview['overview']break# Extract the artwork type 15 linkimage_link = Noneif cleaned_data['data']['artworks']:for artwork in cleaned_data['data']['artworks']:if artwork['type'] == 15:image_link = artwork['image']break# Add the entry to the RecentList with description and image_linkif media_type == 'movie':recent_list.add_entry(original_title, cleaned_data['data']['year'], description, image_link, 'film')elif media_type == 'series':recent_list.add_entry(original_title, cleaned_data['data']['year'], description, image_link, 'series')# Cache the extended JSON responsejson_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(original_title)}.json")await save_to_json(cleaned_data, json_cache_path)
services.py+22-24
tvdb.py+22-3