-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathytinfo.py
70 lines (57 loc) · 2.66 KB
/
ytinfo.py
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
from googleapiclient.discovery import build
from youtube_transcript_api.formatters import TextFormatter
from youtube_transcript_api.formatters import JSONFormatter
from youtube_transcript_api import YouTubeTranscriptApi
class YtInfo:
def __init__(self):
self.subtitle_json=''
self.subtitle_text=''
#Searching Channel Info From Channel Username
def channel_info_by_username(self,channel_username,API_KEY):
#API_KEY_GOOGLE=
channel_username=channel_username[25:]
api_service_name = "youtube"
api_version = "v3"
#Getting Channel Data
youtube=build(api_service_name,api_version,developerKey=API_KEY)
def get_channel_stats():
request=youtube.channels().list(part='snippet,contentDetails,statistics',forUsername=channel_username)
response=request.execute()
#Tried to get channel name in dict...
# print(response['items'][0]['snippet']['title'])
with open("C:\\Users\\Kashif\\Documents\\hackathon_proj\\channel_info.json",'w') as file:
file.write(str(response))
return response
get_channel_stats()
#Searching Channel Info From Channel Id
def channel_info_by_id(self,channel_id,API_KEY):
#API_KEY_GOOGLE=
channel_id=channel_id[32:57]
api_service_name = "youtube"
api_version = "v3"
#Getting Channel Data
youtube=build(api_service_name,api_version,developerKey=API_KEY)
def get_channel_stats():
request=youtube.channels().list(part='snippet,contentDetails,statistics',id=channel_id)
response=request.execute()
#Tried to get channel name in dict...
# print(response['items'][0]['snippet']['title'])
with open("C:\\Users\\Kashif\\Documents\\hackathon_proj\\channel_info.json",'w') as file:
file.write(str(response))
return response
get_channel_stats()
#Subtitle Scraping From YT Videos
def subtitle_info(self,yt_video_link):
def Get_subtitle(video_code):
self.subtitle_json=YouTubeTranscriptApi.get_transcript(video_code)
print(self.subtitle_json)
with open("./data/subtitle.txt",'w') as file:
self.subtitle_text = TextFormatter().format_transcript(self.subtitle_json)
file.write(str(self.subtitle_text))
with open("./data/subtitle.json",'w') as file:
file.write(str(self.subtitle_json))
try:
Get_subtitle(yt_video_link[32:43])
except:
Get_subtitle(yt_video_link[17:29])
return self.subtitle_json