Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[videoscripts/.git] / aired_today_playlist.py
1 """
2 Create a Plex Playlist with what aired on this day in history (month-day), sort by oldest first.
3 If Playlist from yesterday exists delete and create today's.
4 If today's Playlist exists exit.
5 """
6
7 import operator
8 from plexapi.server import PlexServer
9 import requests
10 import datetime
11
12 PLEX_URL = 'http://localhost:32400'
13 PLEX_TOKEN = 'xxxxx'
14
15 LIBRARY_NAMES = ['Movies', 'TV Shows'] # Your library names
16
17 today = datetime.datetime.now().date()
18
19 TODAY_PLAY_TITLE = 'Aired Today {}-{}'.format(today.month, today.day)
20
21 plex = PlexServer(PLEX_URL, PLEX_TOKEN)
22
23 def remove_old():
24     # Remove old Aired Today Playlists
25     for playlist in plex.playlists():
26         if playlist.title.startswith('Aired Today') and playlist.title != TODAY_PLAY_TITLE:
27             playlist.delete()
28             print('Removing old Aired Today Playlists: {}'.format(playlist.title))
29         elif playlist.title == TODAY_PLAY_TITLE:
30             print('{} already exists. No need to make again.'.format(TODAY_PLAY_TITLE))
31             exit(0)
32
33
34 def get_all_content(library_name):
35     # Get all movies or episodes from LIBRARY_NAME
36     child_lst = []
37     for library in library_name:
38         for child in plex.library.section(library).all():
39             if child.type == 'movie':
40                 child_lst += [child]
41             elif child.type == 'show':
42                 child_lst += child.episodes()
43             else:
44                 pass
45     return child_lst
46
47
48 def find_air_dates(content_lst):
49     # Find what aired with today's month-day
50     aired_lst = []
51     for video in content_lst:
52         try:
53             ad_month = str(video.originallyAvailableAt.month)
54             ad_day = str(video.originallyAvailableAt.day)
55             
56             if ad_month == str(today.month) and ad_day == str(today.day):
57                 aired_lst += [[video] + [str(video.originallyAvailableAt)]]
58         except Exception as e:
59             # print(e)
60             pass
61         
62         # Sort by original air date, oldest first
63         aired_lst = sorted(aired_lst, key=operator.itemgetter(1))
64
65     # Remove date used for sorting
66     play_lst = [x[0] for x in aired_lst]
67     return play_lst
68
69
70 remove_old()
71 play_lst = find_air_dates(get_all_content(LIBRARY_NAMES))
72 # Create Playlist
73 if play_lst:
74     plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
75 else:
76     print('Found nothing aired on this day in history.')
77