ahmedJaafari commited on
Commit
3139835
1 Parent(s): cf8bf73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -1
app.py CHANGED
@@ -4,6 +4,42 @@ import requests
4
  import gradio as gr
5
  import os
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def submit_video(video_url):
8
  url = os.getenv("URL")
9
  payload = {"video_url": video_url}
@@ -23,5 +59,18 @@ def submit_video(video_url):
23
 
24
  return f"Task Successfully Submited. Task ID: {response.json()['task_id']}"
25
 
26
- demo = gr.Interface(fn=submit_video, inputs=[{"name": "text", "label": "Video URL"}], outputs=[{"name": "text", "label": "Task ID"}])
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  demo.launch()
 
4
  import gradio as gr
5
  import os
6
 
7
+ import requests
8
+
9
+ URL = f"https://youtube.googleapis.com/youtube/v3/search?part=id&key={os.getenv('SECRET_YTB_TOKEN')"
10
+
11
+
12
+ def get_channel(URL, channel_name):
13
+ URL += f"&q={channel_name}&type=channel"
14
+ response = requests.get(URL)
15
+ response.raise_for_status()
16
+ return response.json()["items"][0]["id"]["channelId"]
17
+
18
+
19
+ def get_videos(URL, channel_id, pageToken=None, maxResults=20):
20
+ URL += f"&order=date&maxResults={maxResults}&type=video&channelId={channel_id}"
21
+
22
+ if pageToken:
23
+ URL += f"&pageToken={pageToken}"
24
+
25
+ response = requests.get(URL)
26
+ response.raise_for_status()
27
+ return response.json()
28
+
29
+
30
+
31
+ def get_all_videos(URL, channel_id):
32
+ videos = []
33
+ pageToken = None
34
+ while True:
35
+ response = get_videos(URL, channel_id, pageToken)
36
+ for item in response["items"]:
37
+ videos.append(item["id"]["videoId"])
38
+ pageToken = response.get("nextPageToken")
39
+ if not pageToken:
40
+ break
41
+ return videos
42
+
43
  def submit_video(video_url):
44
  url = os.getenv("URL")
45
  payload = {"video_url": video_url}
 
59
 
60
  return f"Task Successfully Submited. Task ID: {response.json()['task_id']}"
61
 
62
+ def submit_job(video_url, channel_id) :
63
+ if video_url != "":
64
+ return submit_video(video_url)
65
+
66
+ channel_id = get_channel(URL, channel_id)
67
+
68
+ results = get_all_videos(URL, channel_id)
69
+
70
+ for video in results:
71
+ submit_video("https://www.youtube.com/watch?v="+video)
72
+
73
+ return "Channel Successfully Submitted"
74
+
75
+ demo = gr.Interface(fn=submit_job, inputs=[{"name": "text", "label": "Video URL"}, {"name": "text", "label": "Channel ID"}], outputs=[{"name": "text", "label": "Task ID"}])
76
  demo.launch()