-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt_components.py
More file actions
110 lines (96 loc) · 4.17 KB
/
yt_components.py
File metadata and controls
110 lines (96 loc) · 4.17 KB
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
import dash
from dash import dcc, html, Input, State, Output, callback
import dash_bootstrap_components as dbc
import json
import requests
from config import api_key,api_host
from furl import furl
from pprint import pprint
def youtube_comments_component(comment):
rv=dbc.Row(html.Div([
html.Div(comment,style = {"margin":"20px 20px 20px 20px"}),
],
style = {"margin":"5px","padding":"20px"},
className="h-10 p-5 text-white bg-dark rounded-1",
),
)
return rv
def youtube_title_component(channel_id,title,description,published_at):
rv=dbc.Col(html.Div([
dcc.Link(
dbc.Button(
'Go to channel',
n_clicks = 0,
className="btn btn-secondary"
),
href = f'/ytChannels?param1={channel_id}',
refresh = True
),
html.Div(published_at,style = {"margin":"20px 20px 20px 20px"}),
html.Div(title,style = {"margin":"20px 20px 20px 20px"}),
html.Div(description,style = {"margin":"20px 20px 20px 20px"})
],
style = {"margin":"60px","padding":"20px"},
className="h-10 p-5 text-white bg-dark rounded-1",
),
md=6,
)
return rv
def video_details_component(video_id):
url = f"https://{api_host}/videos"
querystring = {
"part":"contentDetails,snippet,statistics",
"id":video_id
}
headers = {
"X-RapidAPI-Key": api_key,
"X-RapidAPI-Host": api_host
}
response = requests.request("GET", url, headers=headers, params=querystring)
search_dict = json.loads(response.text)
search_list = search_dict['items']
snippet_details = [item['snippet'] for item in search_list]
channel_ids = [item['channelId'] for item in snippet_details]
title = [item['title'] for item in snippet_details]
description = [item['description'] for item in snippet_details]
published_at = [item['publishedAt'] for item in snippet_details]
search_component_children = dbc.Row(
[youtube_title_component(video_id,title,description,published_at) for video_id in channel_ids ],
className="align-items-md-stretch",
)
# pprint(f"search_component_children={search_component_children}")
return search_component_children
def youtube_component(video_id):
children=[
# dash_player.DashPlayer(
# id=f"{video_id}",
# url=f"https://youtu.be/{video_id}",
# controls=False,
# width="30%",
# height="250px",
# ),
html.Iframe(width="30%",
height="250",
src=f"https://www.youtube-nocookie.com/embed/{video_id}",
title="YouTube video player",
allow="accelerometer, autoplay, clipboard-write, encrypted-media, gyroscope, picture-in-picture "
)
]
return children[0]
def video_comments_component(video_id):
url = f"https://{api_host}/commentThreads"
querystring = {"part":"snippet","videoId":video_id,"maxResults":"100"}
headers = {
"X-RapidAPI-Key": api_key,
"X-RapidAPI-Host": api_host
}
response = requests.request("GET", url, headers=headers, params=querystring)
search_dict = json.loads(response.text)
search_list = search_dict['items']
toplevel_comments = [item['snippet']['topLevelComment']['snippet']['textOriginal'] for item in search_list]
rv = dbc.Col(
[youtube_comments_component(comment) for comment in toplevel_comments],
# className="align-items-md-stretch",
)
# pprint(f"search_component_children={search_component_children}")
return rv