Spaces:
Running
Running
File size: 3,327 Bytes
b9c02ee 39d0fe5 153a135 19f91ff 153a135 8cd4b7a a413623 5266a81 a413623 793f723 a413623 793f723 a413623 ced6416 |
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 |
from urllib.parse import urlparse, parse_qs
import re
import os
import googleapiclient
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import datetime
api_key = os.getenv("api_key_secret")
youtube = build('youtube', 'v3', developerKey=api_key)
def playlists_mismatch_func(playlistlink1, playlistlink2, output):
"""Return the ids of videos that are only one of the playlists, and not in the other"""
def extract_playlist_id(playlistlink):
match = re.search(r'list=([^&]+)', playlistlink) #It searches for the string 'list=' followed by >=1 characters that are not '&'.
if match:
return match.group(1)
return None
playlist1id = extract_playlist_id(playlistlink1)
playlist2id = extract_playlist_id(playlistlink2)
print("Playlist IDs obtained")
assert playlist1id!= None, "Playlist 1 link is invalid"
assert playlist2id!= None, "Playlist 2 link is invalid"
vid1_ids = []
vid2_ids = []
def get_video_ids(playlistid):
vid_ids = []
next_page_token = None
while True:
pl_request = youtube.playlistItems().list(
part="contentDetails, snippet",
playlistId=playlistid,
maxResults=50,
pageToken = next_page_token
)
pl_response = pl_request.execute()
for item in pl_response['items']:
vid_ids.append(item['contentDetails']['videoId'])
next_page_token = pl_response.get('nextPageToken')
if next_page_token is None:
break
return vid_ids
vid1_ids = get_video_ids(playlist1id)
print("Playlist 1 video IDs obtained, no. of videos:", len(vid1_ids))
vid2_ids = get_video_ids(playlist2id)
print("Playlist 2 video IDs obtained, no. of videos:", len(vid2_ids))
print("Video IDs obtained")
def get_playlist_name(playlistid):
pl_request = youtube.playlists().list(
part="snippet",
id=playlistid,
maxResults=1
)
pl_response = pl_request.execute()
return pl_response['items'][0]['snippet']['title']
playlist1name = get_playlist_name(playlist1id)
playlist2name = get_playlist_name(playlist2id)
def get_video_name(videoid):
vid_request = youtube.videos().list(
part="snippet",
id=videoid,
maxResults=1
)
vid_response = vid_request.execute()
return vid_response['items'][0]['snippet']['title']
firstnotsecond = list(set(vid1_ids) - set(vid2_ids))
secondnotfirst = list(set(vid2_ids) - set(vid1_ids))
if output == 'id':
return firstnotsecond, secondnotfirst, playlist1name, playlist2name
elif output == 'link':
firstnotsecond = [f"https://youtu.be/{i}" for i in firstnotsecond]
secondnotfirst = [f"https://youtu.be/{i}" for i in secondnotfirst]
return firstnotsecond, secondnotfirst, playlist1name, playlist2name
elif output == 'name':
firstnotsecond = [get_video_name(i) for i in firstnotsecond]
secondnotfirst = [get_video_name(i) for i in secondnotfirst]
return firstnotsecond, secondnotfirst, playlist1name, playlist2name |