DebasishDhal99 commited on
Commit
631450c
·
1 Parent(s): 21f5c0e

Create functions.py

Browse files
Files changed (1) hide show
  1. functions.py +82 -0
functions.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import timedelta
2
+ #from pyyoutube import playlist
3
+ import re
4
+ import gradio as gr
5
+ from urllib.parse import urlparse, parse_qs
6
+ from contextlib import suppress
7
+ import os
8
+
9
+ api_key = os.getenv("api_key_secret")
10
+ import googleapiclient
11
+ from googleapiclient.discovery import build
12
+ from googleapiclient.errors import HttpError
13
+ import datetime
14
+ youtube = build('youtube', 'v3', developerKey=api_key)
15
+
16
+ def get_playlistname_from_link(playlist_link):
17
+ playlist_id = re.search(r'list=(.*)', playlist_link)
18
+ pl_request = youtube.playlistItems().list(
19
+ part="contentDetails,snippet",
20
+ playlistId=playlist_id.group(1),
21
+ maxResults=50
22
+ )
23
+ pl_response= pl_request.execute()
24
+ if 'items' in pl_response:
25
+ playlist = pl_response['items'][0]
26
+ title = playlist['snippet']['title']
27
+ return title
28
+
29
+ def get_playlistname_from_id(playlist_id):
30
+ playlist_id = playlist_id
31
+ pl_request = youtube.playlistItems().list(
32
+ part="contentDetails,snippet",
33
+ playlistId=playlist_id.group(1),
34
+ maxResults=50
35
+ )
36
+ pl_response= pl_request.execute()
37
+ if 'items' in pl_response:
38
+ playlist = pl_response['items'][0]
39
+ title = playlist['snippet']['title']
40
+ return title
41
+
42
+ def get_playlistid_from_link(playlist_link):
43
+ match = re.search(r'list=([^&]+)', playlist_link) #It searches for the string 'list=' followed by >=1 characters that are not '&'.
44
+ if match:
45
+ return match.group(1)
46
+ return None
47
+
48
+ def extract_playlist_id(playlistlink):
49
+ match = re.search(r'list=([^&]+)', playlistlink) #It searches for the string 'list=' followed by >=1 characters that are not '&'.
50
+ if match:
51
+ return match.group(1)
52
+ return None
53
+
54
+ playlist_id = extract_playlist_id(playlistlink)
55
+
56
+ if playlist_id is None:
57
+ return False
58
+
59
+ search_request = youtube.playlists().list(
60
+
61
+ part='id',
62
+ id=playlist_id,
63
+ maxResults=1
64
+ )
65
+
66
+ search_response = search_request.execute()
67
+ if 'items' in search_response:
68
+ try:
69
+ playlistdict = search_response['items'][0]
70
+ print("ID of playlist is:- ",playlistdict['id'])
71
+ return playlistdict['id']
72
+ except:
73
+ #print("Video not found.")
74
+ return False
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+