Spaces:
Running
Running
DebasishDhal99
commited on
Commit
•
73db5a0
1
Parent(s):
4563355
Create average_duration.py
Browse filesFile for average duration function.
- average_duration.py +156 -0
average_duration.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import pyyoutube
|
2 |
+
from datetime import timedelta
|
3 |
+
#from pyyoutube import playlist
|
4 |
+
import re
|
5 |
+
import gradio as gr
|
6 |
+
from urllib.parse import urlparse, parse_qs
|
7 |
+
from contextlib import suppress
|
8 |
+
|
9 |
+
api_key = "AIzaSyDThR6EJnLkH56c6CpxY7HUZ_bMn4NCAKU"
|
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 playlist_average_duration_func(youtubelink,videoid=False):
|
17 |
+
|
18 |
+
def playlist_exist_check(playlistlink):
|
19 |
+
|
20 |
+
def extract_playlist_id(playlistlink):
|
21 |
+
match = re.search(r'list=([^&]+)', playlistlink) #It searches for the string 'list=' followed by >=1 characters that are not '&'.
|
22 |
+
if match:
|
23 |
+
return match.group(1)
|
24 |
+
return None
|
25 |
+
|
26 |
+
playlist_id = extract_playlist_id(playlistlink)
|
27 |
+
|
28 |
+
if playlist_id is None:
|
29 |
+
return False
|
30 |
+
|
31 |
+
search_request = youtube.playlists().list(
|
32 |
+
|
33 |
+
part='id',
|
34 |
+
id=playlist_id,
|
35 |
+
maxResults=1
|
36 |
+
)
|
37 |
+
|
38 |
+
search_response = search_request.execute()
|
39 |
+
if 'items' in search_response:
|
40 |
+
try:
|
41 |
+
playlistdict = search_response['items'][0]
|
42 |
+
print("ID of playlist is:- ",playlistdict['id'])
|
43 |
+
return playlistdict['id']
|
44 |
+
except:
|
45 |
+
#print("Video not found.")
|
46 |
+
return False
|
47 |
+
|
48 |
+
playlistid = playlist_exist_check(youtubelink)
|
49 |
+
if playlistid == False or playlistid==None:
|
50 |
+
print("Playlist doesn't exist")
|
51 |
+
return False
|
52 |
+
print("1st check passed - Playlist link is valid")
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
#This section retrieves the video ids of all the videos in the playlist, and stores them in a list. 50 in one iteration.
|
58 |
+
|
59 |
+
vid_ids = []
|
60 |
+
next_page_token = None
|
61 |
+
while True:
|
62 |
+
|
63 |
+
|
64 |
+
pl_request = youtube.playlistItems().list(
|
65 |
+
part="contentDetails,snippet",
|
66 |
+
playlistId=playlistid,
|
67 |
+
maxResults=50, #This is the max limit of videos that can be fetched in one go form a playlist as youtube data v3 API results are paginated
|
68 |
+
pageToken=next_page_token
|
69 |
+
)
|
70 |
+
pl_response = pl_request.execute()
|
71 |
+
# print("Reponse obtained from youtube")
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
for item in pl_response['items']:
|
76 |
+
vid_id = item['contentDetails']['videoId']
|
77 |
+
vid_ids.append(vid_id)
|
78 |
+
if videoid==True:
|
79 |
+
print(item['contentDetails']['videoId'])
|
80 |
+
|
81 |
+
next_page_token = pl_response.get("nextPageToken")
|
82 |
+
if not next_page_token:
|
83 |
+
break
|
84 |
+
print("2nd check passed - Playlist read")
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
#This section obtains the playlist name from the playlist id
|
89 |
+
pl_request = youtube.playlists().list(
|
90 |
+
part="snippet",
|
91 |
+
id=playlistid,
|
92 |
+
maxResults=1
|
93 |
+
)
|
94 |
+
pl_response = pl_request.execute()
|
95 |
+
playlist = pl_response['items'][0]
|
96 |
+
title = playlist['snippet']['title']
|
97 |
+
print("Playlist Title:", title)
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# title = playlist['snippet']['title']
|
105 |
+
# print("Playlist Title:", title)
|
106 |
+
#This section retrieves the duration of each video in the playlist, and stores them in a list. 50 in one iteration
|
107 |
+
|
108 |
+
|
109 |
+
iterations = len(vid_ids)//50+1
|
110 |
+
duration_list = []
|
111 |
+
for i in range(iterations):
|
112 |
+
start_index = i * 50
|
113 |
+
end_index = (i + 1) * 50
|
114 |
+
batch_ids = vid_ids[start_index:end_index]
|
115 |
+
vid_request = youtube.videos().list(
|
116 |
+
part="contentDetails",
|
117 |
+
id=','.join(batch_ids)
|
118 |
+
)
|
119 |
+
|
120 |
+
vid_response = vid_request.execute()
|
121 |
+
|
122 |
+
|
123 |
+
for item in vid_response['items']:
|
124 |
+
duration = item['contentDetails']['duration']
|
125 |
+
duration = duration[2:]
|
126 |
+
hours = 0
|
127 |
+
minutes = 0
|
128 |
+
seconds = 0
|
129 |
+
|
130 |
+
if "H" in duration:
|
131 |
+
hours_index = duration.index("H")
|
132 |
+
hours = int(duration[:hours_index])
|
133 |
+
duration = duration[hours_index+1:]
|
134 |
+
|
135 |
+
if "M" in duration:
|
136 |
+
minutes_index = duration.index("M")
|
137 |
+
minutes = int(duration[:minutes_index])
|
138 |
+
duration = duration[minutes_index+1:]
|
139 |
+
|
140 |
+
if "S" in duration:
|
141 |
+
seconds_index = duration.index("S")
|
142 |
+
seconds = int(duration[:seconds_index])
|
143 |
+
|
144 |
+
duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
145 |
+
duration_list.append(duration)
|
146 |
+
print("3rd check passed - Individual video duration calculated")
|
147 |
+
total_duration = sum(duration_list, timedelta())
|
148 |
+
#Find the average duration of each video in the playlist
|
149 |
+
average_duration = total_duration/len(vid_ids)
|
150 |
+
print("Total duration of playlist is:- ",total_duration)
|
151 |
+
print("Total no. of videos is = ",len(vid_ids))
|
152 |
+
print("Average duration of each video is:- ",average_duration)
|
153 |
+
#Convert the average suration into HH:MM:SS format
|
154 |
+
# average_duration_format = s
|
155 |
+
return str(average_duration)
|
156 |
+
|