-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
217 lines (169 loc) · 6.32 KB
/
app.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import json
from datetime import datetime
import pymysql
import requests
from flask import Flask, render_template, make_response, request
from googleapiclient.discovery import build
import config
app = Flask(__name__) # Flask 객체 선언
app.config['JSON_AS_ASCII'] = False # 한글 깨짐 방지
# YouTube API Build
DEVELOPER_KEY = config.key
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
conn = pymysql.connect(host='localhost', user='root', password=config.db_password, charset='utf8mb4')
cursor = conn.cursor()
conn1 = pymysql.connect(host='localhost', user='root', password=config.db_password, charset='utf8mb4')
# 단어 빈도수
@app.route('/live-words/<video_id>')
def live_words(video_id):
cur1 = conn1.cursor()
# 상위 5개 단어
sql = "SELECT word, COUNT(word) FROM summary.chatting WHERE videoid=%s GROUP BY word HAVING COUNT(word) > 0 ORDER BY count(word) DESC"
cur1.execute(sql, video_id)
datas = cur1.fetchmany(5)
conn1.commit()
try:
results = [[datas[0][0], datas[0][1]], [datas[1][0], datas[1][1]], [datas[2][0], datas[2][1]],
[datas[3][0], datas[3][1]], [datas[4][0], datas[4][1]]]
except IndexError:
results = 0
response = make_response(json.dumps(results))
response.content_type = 'application/json'
return response
# 감정 분석 그래프
@app.route('/live-segment/<video_id>')
def live_segment(video_id):
cur = conn.cursor()
# 5분간 반응 조회
sql = "SELECT reaction_score FROM summary.chatting WHERE videoid=%s AND created_at > date_add(now(), interval -5 minute)"
cur.execute(sql, video_id)
datas = cur.fetchall()
conn.commit()
num = 0
for i in range(len(datas)):
num += datas[i][0]
try:
avg_reaction = num / len(datas)
except ZeroDivisionError:
avg_reaction = 50
# print(int(avg_reaction))
results = [int(avg_reaction), 100 - int(avg_reaction)] # [긍정 비율, 부정 비율]
response = make_response(json.dumps(results))
response.content_type = 'application/json'
return response
@app.route("/summary/")
def hello():
return render_template('index.html')
@app.route("/summary/<video_id>", methods=['GET'])
def summary(video_id):
# Todo : NLP
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
search_response = youtube.videos().list(
part="id, snippet, liveStreamingDetails, statistics",
id=video_id
).execute()
cur = conn.cursor()
# 채팅 수
sql = "SELECT COUNT(id) FROM summary.chatting WHERE videoid = %s"
cur.execute(sql, video_id)
chat_num = cur.fetchall()
# print(chat_num[0][0])
conn.commit()
# 현재 시청자 수
try:
viewers = search_response['items'][0]['liveStreamingDetails']['concurrentViewers']
except KeyError:
viewers = 0
# 누적 시청자 수
try:
viewers_accumulate = search_response['items'][0]['statistics']['viewCount']
except KeyError:
viewers_accumulate = 0
# 방송 제목
try:
streaming_title = search_response['items'][0]['snippet']['title']
except KeyError:
streaming_title = 0
# 구독자 수
try:
channel_id = search_response['items'][0]['snippet']['channelId']
channel_response = youtube.channels().list(
part='id, snippet, statistics',
id=channel_id
).execute()
subscriber = channel_response['items'][0]['statistics']['subscriberCount']
except KeyError:
subscriber = 0
# 좋아요 수
try:
like_count = search_response['items'][0]['statistics']['likeCount']
except KeyError:
like_count = 0
# 실시간 방송 썸네일
try:
thumbnail = search_response['items'][0]['snippet']['thumbnails']['medium']['url']
except KeyError:
thumbnail = 0
# 방송 런타임
start_time = search_response['items'][0]['liveStreamingDetails']['actualStartTime']
now = datetime.now()
past = datetime.strptime(start_time, "%Y-%m-%dT%H:%M:%SZ")
diff = now - past
daysH = diff.days * 24
hour = int(diff.seconds / 3600)
min = int(diff.seconds / 60) - hour * 60
sec = int(diff.seconds % 60)
runtime = "%d 시간 %d 분 %d 초" % (hour + daysH, min, sec)
# 실시간 방송 차트 시간
now = datetime.now()
now_time = now.strftime('%H:%M:%S')
viewers_chart = [float(int(now_time[3:5])) + int(now_time[6:8]) * 0.01, int(viewers)]
results = [chat_num[0][0], runtime, viewers, viewers_accumulate, streaming_title, subscriber, like_count, thumbnail,
viewers_chart]
response = make_response(json.dumps(results, ensure_ascii=False))
response.content_type = 'application/json'
return response
@app.route('/', methods=['GET', 'POST'])
def index():
search_url = 'https://www.googleapis.com/youtube/v3/search'
video_url = 'https://www.googleapis.com/youtube/v3/videos'
key = config.key
videos = []
if request.method == 'POST':
search_params = {
'key': key,
'q': request.form.get('query'),
'part': 'snippet',
'maxResults': 9,
'type': 'video',
'eventType': 'live',
'regionCode': 'KR'
}
r = requests.get(search_url, params=search_params)
results = r.json()['items']
video_ids = []
for result in results:
video_ids.append(result['id']['videoId'])
video_params = {
'key': key,
'id': ','.join(video_ids),
'part': 'id,snippet,liveStreamingDetails',
'maxResults': 9
}
r = requests.get(video_url, params=video_params)
results = r.json()['items']
for result in results:
video_data = {
'id': result['id'],
'url': f'https://www.youtube.com/watch?v={result["id"]}',
'thumbnail': result['snippet']['thumbnails']['high']['url'],
'title': result['snippet']['title'],
'startTime': result['liveStreamingDetails']['actualStartTime'],
}
videos.append(video_data)
if not videos:
videos = 0
return render_template('search.html', videos=videos)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=False)