2
2
from bs4 import BeautifulSoup
3
3
import json
4
4
5
+
5
6
def getAbout (channel_username ):
6
- """
7
- Returns:
8
- ```
9
- {
10
- "name": Name of the channel
11
- "description": Description of the channel
12
- "channel_url": Link to the channel
13
- "channel_avatar": Channel avatar
14
- "channel_banner": Channel banner
15
- "subscriber_count": No. of subscribers of the channel
16
- "toal_videos": Total videos uploaded in the channel
17
- "total_views": Total views till date of the channel
18
- "join_date": Date the channel joined YouTube
19
- "country": Country of origin of the channel
20
- "links": Additional links provided from the channel
21
- }
22
- ```
23
- """
24
- url = f"https://www.youtube.com/@{ channel_username } /about"
25
- try :
26
- res = requests .get (url )
27
- soup = BeautifulSoup (res .text , "html.parser" )
28
- channel_data = {"channel_data" : []}
29
- link_data = {"link_data" : []}
30
- scripts = soup .find_all ("script" )
31
- req_script = scripts [35 ].text .strip ()
32
- script = req_script [20 :- 1 ]
33
- data = json .loads (script )
7
+ """
8
+ Returns:
9
+ ```
10
+ {
11
+ "name": Name of the channel
12
+ "description": Description of the channel
13
+ "channel_url": Link to the channel
14
+ "channel_avatar": Channel avatar
15
+ "channel_banner": Channel banner
16
+ "subscriber_count": No. of subscribers of the channel
17
+ "toal_videos": Total videos uploaded in the channel
18
+ "total_views": Total views till date of the channel
19
+ "join_date": Date the channel joined YouTube
20
+ "country": Country of origin of the channel
21
+ "links": Additional links provided from the channel
22
+ }
23
+ ```
24
+ """
25
+ url = f"https://www.youtube.com/@{ channel_username } /about"
26
+ try :
27
+ res = requests .get (url )
28
+ soup = BeautifulSoup (res .text , "html.parser" )
29
+ channel_data = {"channel_data" : []}
30
+ link_data = {"link_data" : []}
31
+ scripts = soup .find_all ("script" )
32
+ req_script = scripts [35 ].text .strip ()
33
+ script = req_script [20 :- 1 ]
34
+ data = json .loads (script )
35
+
36
+ metadata = data ["metadata" ]["channelMetadataRenderer" ]
37
+ title = metadata ["title" ]
38
+ desc = metadata ["description" ]
39
+ channel_url = metadata ["vanityChannelUrl" ]
40
+ channel_avatar = metadata ["avatar" ]["thumbnails" ][0 ]["url" ]
41
+ header = data ["header" ]["c4TabbedHeaderRenderer" ]
42
+ channel_banner = header ["banner" ]["thumbnails" ][5 ]["url" ]
43
+ subs = header ["subscriberCountText" ]["simpleText" ]
44
+ total_videos = header ["videosCountText" ]["runs" ][0 ]["text" ]
34
45
35
- metadata = data ["metadata" ]["channelMetadataRenderer" ]
36
- title = metadata ["title" ]
37
- desc = metadata ["description" ]
38
- channel_url = metadata ["vanityChannelUrl" ]
39
- channel_avatar = metadata ["avatar" ]["thumbnails" ][0 ]["url" ]
40
- header = data ["header" ]["c4TabbedHeaderRenderer" ]
41
- channel_banner = header ["banner" ]["thumbnails" ][5 ]["url" ]
42
- subs = header ["subscriberCountText" ]["simpleText" ]
43
- total_videos = header ["videosCountText" ]["runs" ][0 ]["text" ]
46
+ baser = data ["contents" ]["twoColumnBrowseResultsRenderer" ]["tabs" ]
47
+ for b in baser :
48
+ try :
49
+ base = b ["tabRenderer" ]["content" ]["sectionListRenderer" ][
50
+ "contents"
51
+ ][0 ]["itemSectionRenderer" ]["contents" ][0 ][
52
+ "channelAboutFullMetadataRenderer"
53
+ ]
44
54
45
- baser = data ["contents" ]["twoColumnBrowseResultsRenderer" ]["tabs" ]
46
- for b in baser :
47
- try :
48
- base = b ["tabRenderer" ]["content" ]["sectionListRenderer" ][
49
- "contents"
50
- ][0 ]["itemSectionRenderer" ]["contents" ][0 ][
51
- "channelAboutFullMetadataRenderer"
52
- ]
55
+ total_views = base ["viewCountText" ]["simpleText" ]
56
+ join_date = base ["joinedDateText" ]["runs" ][1 ]["text" ]
57
+ country = base ["country" ]["simpleText" ]
53
58
54
- total_views = base ["viewCountText" ]["simpleText" ]
55
- join_date = base ["joinedDateText" ]["runs" ][1 ]["text" ]
56
- country = base ["country" ]["simpleText" ]
59
+ links = base ["primaryLinks" ]
60
+ for i in links :
61
+ link_data ["link_data" ].append (
62
+ {
63
+ "link_url" : i ["navigationEndpoint" ]["urlEndpoint" ][
64
+ "url"
65
+ ],
66
+ "link_name" : i ["title" ]["simpleText" ],
67
+ "link_icon" : i ["icon" ]["thumbnails" ][0 ]["url" ],
68
+ }
69
+ )
70
+ except :
71
+ pass
57
72
58
- links = base ["primaryLinks" ]
59
- for i in links :
60
- link_data ["link_data" ].append (
61
- {
62
- "link_url" : i ["navigationEndpoint" ]["urlEndpoint" ][
63
- "url"
64
- ],
65
- "link_name" : i ["title" ]["simpleText" ],
66
- "link_icon" : i ["icon" ]["thumbnails" ][0 ]["url" ],
67
- }
68
- )
69
- except :
70
- pass
73
+ channel_data ["channel_data" ].append (
74
+ {
75
+ "name" : title ,
76
+ "description" : desc ,
77
+ "channel_url" : channel_url ,
78
+ "channel_avatar" : channel_avatar ,
79
+ "channel_banner" : channel_banner ,
80
+ "subscriber_count" : subs ,
81
+ "toal_videos" : total_videos ,
82
+ "total_views" : total_views ,
83
+ "join_date" : join_date ,
84
+ "country" : country ,
85
+ "links" : link_data ,
86
+ }
87
+ )
88
+ return channel_data ["channel_data" ][0 ]
89
+ except :
90
+ return None
71
91
72
- channel_data ["channel_data" ].append (
73
- {
74
- "name" : title ,
75
- "description" : desc ,
76
- "channel_url" : channel_url ,
77
- "channel_avatar" : channel_avatar ,
78
- "channel_banner" : channel_banner ,
79
- "subscriber_count" : subs ,
80
- "toal_videos" : total_videos ,
81
- "total_views" : total_views ,
82
- "join_date" : join_date ,
83
- "country" : country ,
84
- "links" : link_data ,
85
- }
86
- )
87
- return channel_data ["channel_data" ][0 ]
88
- except :
89
- return None
90
92
91
93
if __name__ == "__main__" :
92
- print (getAbout ("BeaBetterDev" ))
94
+ print (getAbout ("BeaBetterDev" ))
0 commit comments