forked from AlotOfBlahaj/Auto_Record_Matsuri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
162 lines (146 loc) · 5.96 KB
/
youtube.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import json
import re
from time import sleep, strftime, localtime, time
from config import sec, api_key
from daemon import VideoDaemon
from tools import get, get_json, get_logger, Database, while_warp
from video_process import process_video
class Youtube(VideoDaemon):
def __init__(self, target_id):
super().__init__(target_id)
self.module = 'Youtube'
self.api_key = api_key
# 品质设置
self.database = Database('Queues')
self.logger = get_logger('Youtube')
# 关于SearchAPI的文档 https://developers.google.com/youtube/v3/docs/search/list
def get_videoid_by_channel_id(self, channel_id):
channel_info = get_json(rf'https://www.googleapis.com/youtube/v3/search?part=snippet&'
rf'channelId={channel_id}&eventType=live&maxResults=1&type=video&'
rf'key={self.api_key}')
# 判断获取的数据是否正确
try:
item = channel_info['items'][0]
except KeyError:
self.logger.exception('Get vid error')
raise RuntimeError
title = item['snippet']['title']
title = title.replace("/", " ")
vid = item['id']['videoId']
date = item['snippet']['publishedAt']
date = date[0:10]
target = f"https://www.youtube.com/watch?v={vid}"
thumbnails = item['snippet']['thumbnails']['high']['url']
return {'Title': title,
'Ref': vid,
'Date': date,
'Target': target,
'Thumbnails': thumbnails}
def get_video_info_by_html(self):
"""
The method is using yfconfig to get information of video including title, video_id, data and thumbnail
:rtype: dict
"""
video_page = get(f'https://www.youtube.com/channel/{self.target_id}/live')
try:
ytplayer_config = json.loads(re.search(r'ytplayer.config\s*=\s*([^\n]+?});', video_page).group(1))
player_response = json.loads(ytplayer_config['args']['player_response'])
video_details = player_response['videoDetails']
title = video_details['title']
vid = video_details['videoId']
target = f"https://www.youtube.com/watch?v={vid}"
thumbnails = video_details['thumbnail']['thumbnails'][-1]['url']
# date = player_response['playabilityStatus']['liveStreamability']['liveStreamabilityRenderer']['offlineSlate'] \
# ['liveStreamOfflineSlateRenderer']['scheduledStartTime']
return {'Title': title,
'Ref': vid,
'Date': strftime("%Y-%m-%d", localtime(time())),
'Target': target,
'Thumbnails': thumbnails}
except KeyError:
self.logger.exception()
def getlive_title(self, vid):
live_info = get_json(rf'https://www.googleapis.com/youtube/v3/videos?id={vid}&key={self.api_key}&'
r'part=liveStreamingDetails,snippet')
# 判断视频是否正确
if live_info['pageInfo']['totalResults'] != 1:
self.logger.error('Getting title Failed')
raise RuntimeError
# JSON中的数组将被转换为列表,此处使用[0]获得其中的数据
item = live_info['items'][0]
title = item['snippet']['title']
date = item['snippet']['publishedAt']
date = date[0:10]
target = f"https://www.youtube.com/watch?v={vid}"
return {'Title': title,
'Ref': vid,
'Target': target,
'Date': date}
@while_warp
def check(self):
try:
html = get(f'https://www.youtube.com/channel/{self.target_id}/featured')
if '"label":"LIVE NOW"' in html:
# vid = self.get_videoid_by_channel_id()
# get_live_info = self.getlive_vid(vid)
video_dict = self.get_video_info_by_html()
if not video_dict:
self.get_videoid_by_channel_id(self.target_id)
video_dict['Provide'] = self.module
process_video(video_dict)
else:
if 'Upcoming live streams' in html:
self.logger.info(f'{self.target_id}: Found A Live Upcoming')
else:
self.logger.info(f'{self.target_id}: Not found Live')
except Exception:
self.logger.exception()
def run(self) -> None:
self.check()
class YoutubeTemp(Youtube):
def __init__(self, vinfo):
super().__init__(None)
self.vinfo = vinfo
self.vid = None
self.db = Database('Queues')
self.logger = get_logger('YoutubeTemp')
@staticmethod
def get_temp_vid(vlink):
reg = r"watch\?v=([A-Za-z0-9_-]{11})"
idre = re.compile(reg)
_id = vlink["_id"]
vid = vlink["Link"]
vid = re.search(idre, vid).group(1)
return {'Vid': vid,
'Id': _id}
def check(self):
self.vinfo = self.get_temp_vid(self.vinfo)
self.vid = self.vinfo['Vid']
html = get("https://www.youtube.com/watch?v=" f"{self.vid}")
if r'"isLive\":true' in html:
video_dict = self.getlive_title(self.vid)
process_video(video_dict)
self.db.delete(self.vinfo)
else:
self.logger.info(f'Not found Live')
def run(self) -> None:
self.check()
def start_temp_daemon():
db = Database('Queues')
while True:
event = []
for target_url in db.select():
p = YoutubeTemp(target_url)
event.append(p)
p.start()
is_running = True
while is_running:
has_running = False
for p in event:
if p.is_alive():
has_running = True
if not has_running:
is_running = False
logger = get_logger('YoutubeTemp')
logger.info('A check has finished.')
sleep(sec)