-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (91 loc) · 3.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
import logging
from pythonjsonlogger import jsonlogger
# Set logging parameters
log = logging.getLogger()
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
logHandler.setFormatter(formatter)
log.addHandler(logHandler)
log.setLevel(logging.INFO)
# define playlists and sort criteria
EXECLUDED_PLAYLISTS = []
INCLUDE_PLAYLISTS = []
SORT_CRITERIA = 'videoOwnerChannelTitle' ## Change this to the criteria you want to sort by
# Assuming other parts of your code setup are correct
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
flow = InstalledAppFlow.from_client_secrets_file(
'client_secrets.json', scopes=scopes)
# Use run_local_server instead of run_console
credentials = flow.run_local_server(port=0)
youtube = build('youtube', 'v3', credentials=credentials)
def get_playlist_items(playlist_id, nextPageToken=None):
request = youtube.playlistItems().list(
part="snippet,contentDetails",
playlistId=playlist_id,
pageToken=nextPageToken,
maxResults=50
)
response = request.execute()
items=response['items']
nextPageToken = response.get('nextPageToken')
if nextPageToken:
items.extend(get_playlist_items(playlist_id, nextPageToken))
return items
def edit_playlist(playlist):
# Get the current items in the playlist
current_items = get_playlist_items(playlist['id'])
# Sort playlists by sort criteria
current_items.sort(key=lambda x: x['snippet'][SORT_CRITERIA])
# Update the playlist with the new position
for i in range(len(current_items)):
try:
request = youtube.playlistItems().update(
part="snippet",
body={
'id': current_items[i]['id'],
'snippet': {
'playlistId': current_items[i]['snippet']['playlistId'],
'resourceId':current_items[i]['snippet']['resourceId'],
'position': i
}
}
)
response = request.execute()
except Exception as e:
log.error(f'Error updating playlist {playlist['snippet']['title']}: {e}')
return None
return playlist['snippet']['title']
# Retrieve a list of the authenticated user's playlists
nextPageToken = True
playlists = []
while nextPageToken:
nextPageToken = None
request = youtube.playlists().list(
part="snippet,contentDetails",
mine=True,
maxResults=50,
pageToken = nextPageToken
)
response = request.execute()
if response:
playlists.extend(response['items'])
nextPageToken = response.get('nextPageToken')
else:
break
updated_playlist = []
for playlist in playlists:
print('Sorting the playlist: ', playlist['snippet']['title'])
if len(INCLUDE_PLAYLISTS) > 0:
if playlist['snippet']['title'] not in INCLUDE_PLAYLISTS:
updated_playlist.append(edit_playlist(playlist))
elif playlist['snippet']['title'] not in EXECLUDED_PLAYLISTS:
updated_playlist.append(edit_playlist(playlist))
print("\n\n **************************************************************")
print('Updated playlists:')
for playlist in updated_playlist:
print(' -', playlist)
print("\n\n **************************************************************")