File size: 9,389 Bytes
fe42b4a
e512cee
 
 
 
fe42b4a
e512cee
996dd27
 
d4389b7
dbad888
 
 
fe42b4a
 
 
 
 
 
 
996dd27
 
 
 
fe42b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e512cee
 
79f1dd6
fe42b4a
 
79f1dd6
fe42b4a
 
 
 
 
 
e512cee
fe42b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e512cee
8a9f40a
 
 
 
 
 
 
e512cee
 
fe42b4a
e512cee
 
 
fe42b4a
79f1dd6
996dd27
79f1dd6
fe42b4a
 
 
 
 
 
e512cee
fe42b4a
996dd27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe42b4a
62a9a6a
 
 
 
 
 
 
fe42b4a
 
e512cee
 
62a9a6a
 
 
 
fe42b4a
 
 
 
 
 
 
71edee1
fe42b4a
 
 
 
 
 
62a9a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
996dd27
62a9a6a
 
 
 
 
 
 
 
 
996dd27
62a9a6a
 
 
 
 
406ed1d
62a9a6a
 
 
 
996dd27
62a9a6a
996dd27
 
62a9a6a
 
996dd27
 
 
 
 
 
 
 
 
 
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from collections import defaultdict
import datetime
import re
import requests
import yt_dlp
import os

# νŽ˜μ΄μ§€ ꡬ성을 'wide'둜 μ„€μ •ν•˜μ—¬ μ—¬λ°± μ΅œμ†Œν™”
st.set_page_config(layout="wide")

# Streamlit 제λͺ© 및 μ„€λͺ…
st.title("VOD μ±„νŒ… 크둀러")
st.write("VOD URL을 μž…λ ₯ν•˜κ³  μ±„νŒ… 데이터λ₯Ό ν¬λ‘€λ§ν•©λ‹ˆλ‹€.")

# URL μž…λ ₯ λ°›κΈ°
vod_url = st.text_input("VOD URL μž…λ ₯")

# μ„ νƒλœ μ‹œκ°„λŒ€λ₯Ό μ €μž₯ν•  곡간
if 'selected_times' not in st.session_state:
    st.session_state['selected_times'] = []

# μ±„νŒ… 크둀링 ν•¨μˆ˜
def crawl_chats(vod_url):
    # URL μ„€μ •
    url = vod_url + "/chats"

    # μš”μ²­ 헀더 (ν•„μš”μ‹œ μΏ ν‚€λ‚˜ 기타 헀더 정보 μΆ”κ°€)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
        "Accept": "application/json, text/plain, */*",
        "Referer": "https://chzzk.naver.com/video/3646597",
        "Origin": "https://chzzk.naver.com",
        "Cookie": "your-cookie-string-here"  # 둜그인 ν›„ 개발자 λ„κ΅¬μ˜ Request Headers νƒ­μ—μ„œ μΏ ν‚€ κ°’ 볡사
    }

    # 초기 μš”μ²­ νŒŒλΌλ―Έν„° μ„€μ •
    params = {
        "playerMessageTime": 0,  # μ‹œμž‘ μ‹œμ  (0 = 0초)
        "previousVideoChatSize": 50  # κ°€μ Έμ˜¬ μ±„νŒ… λ©”μ‹œμ§€ 수
    }

    chat_logs = []
    chat_counts = defaultdict(int)
    laugh_counts = defaultdict(int)
    total_chats_collected = 0  # 총 μˆ˜μ§‘λœ μ±„νŒ… 개수

    # μ±„νŒ… 데이터λ₯Ό 순차적으둜 μš”μ²­ν•˜μ—¬ κ°€μ Έμ˜€κΈ°
    status_text = st.empty()  # μƒνƒœ λ©”μ‹œμ§€ 좜λ ₯용
    while True:
        # API μš”μ²­ 보내기
        response = requests.get(url, params=params, headers=headers)

        # μš”μ²­ κ²°κ³Όκ°€ 성곡적이지 μ•Šμ„ 경우 μ’…λ£Œ
        if response.status_code != 200:
            return f"API μš”μ²­ μ‹€νŒ¨: {response.status_code}", None, None

        # JSON λ°μ΄ν„°λ‘œ λ³€ν™˜
        data = response.json()

        # μ±„νŒ… λ©”μ‹œμ§€κ°€ ν¬ν•¨λœ 'videoChats' λ°°μ—΄ κ°€μ Έμ˜€κΈ°
        chats = data.get("content", {}).get("videoChats", [])

        # μ±„νŒ… λ©”μ‹œμ§€κ°€ μ—†λ‹€λ©΄ μ’…λ£Œ
        if not chats:
            break

        # μ±„νŒ… λ©”μ‹œμ§€(content)와 playerMessageTime(μ±„νŒ… μ‹œκ°„)만 μˆ˜μ§‘
        for chat in chats:
            chat_content = chat.get("content")  # μ±„νŒ… λ©”μ‹œμ§€ λ‚΄μš©
            message_time = chat.get("playerMessageTime")  # μ±„νŒ… λ©”μ‹œμ§€ μž…λ ₯ μ‹œκ°„

            # 순수 ν…μŠ€νŠΈλ§Œ ν¬ν•¨λœ μ±„νŒ… λ©”μ‹œμ§€ 필터링
            if not re.search(r'{:[^}]*:}', chat_content):  # 이λͺ¨ν‹°μ½˜ ν˜•μ‹μ˜ λ©”μ‹œμ§€κ°€ 없을 λ•Œλ§Œ μˆ˜μ§‘
                # λ°€λ¦¬μ΄ˆ μ‹œκ°„μ„ "μ‹œκ°„:λΆ„:초" ν˜•μ‹μœΌλ‘œ λ³€ν™˜
                hours, remainder = divmod(message_time // 1000, 3600)
                minutes, seconds = divmod(remainder, 60)

                if hours > 0:
                    formatted_time = f"{hours:02}:{minutes:02}:{seconds:02}"  # "HH:MM:SS" ν˜•μ‹
                else:
                    formatted_time = f"{minutes:02}:{seconds:02}"  # "MM:SS" ν˜•μ‹

                # μ±„νŒ… λ‘œκ·Έμ— μΆ”κ°€
                chat_logs.append(f"{formatted_time} - {chat_content}")

                # μ‹œκ°„λŒ€λ³„λ‘œ μ±„νŒ… 개수 계산
                try:
                    # HH:MM:SS ν˜•μ‹μΌ 경우 처리
                    time_obj = datetime.datetime.strptime(formatted_time, '%H:%M:%S')
                except ValueError:
                    # MM:SS ν˜•μ‹μΌ 경우 처리
                    time_obj = datetime.datetime.strptime(formatted_time, '%M:%S')

                minute_key = time_obj.replace(second=0)  # λΆ„ λ‹¨μœ„λ‘œ λ³€ν™˜ν•˜μ—¬ 집계
                chat_counts[minute_key] += 1

                # 'γ…‹γ…‹γ…‹γ…‹'κ°€ ν¬ν•¨λœ μ±„νŒ… 개수 카운트
                if len(re.findall(r'γ…‹', chat_content)) >= 4:
                    laugh_counts[minute_key] += 1

        total_chats_collected += len(chats)
        status_text.text(f"ν˜„μž¬κΉŒμ§€ μˆ˜μ§‘λœ μ±„νŒ… λ©”μ‹œμ§€ 개수: {total_chats_collected}")

        # λ‹€μŒ μš”μ²­μ„ μœ„ν•΄ playerMessageTime νŒŒλΌλ―Έν„° μ—…λ°μ΄νŠΈ
        next_time = data["content"].get("nextPlayerMessageTime")
        if next_time is None:
            break
        params["playerMessageTime"] = next_time

    return "\n".join(chat_logs), chat_counts, laugh_counts

# ν΄λ¦­ν•œ μ‹œκ°„λŒ€λ₯Ό 선택 및 좜λ ₯
def handle_time_selection(click_data):
    if click_data:
        selected_time = click_data['points'][0]['x']
        if selected_time not in st.session_state['selected_times']:
            st.session_state['selected_times'].append(selected_time)

# μ„ νƒλœ μ‹œκ°„λŒ€λ₯Ό ν‘œμ‹œ 및 μ‚­μ œ κΈ°λŠ₯
def display_selected_times():
    if st.session_state['selected_times']:
        st.write("### μ„ νƒλœ μ‹œκ°„λŒ€")
        for time in st.session_state['selected_times']:
            col1, col2 = st.columns([9, 1])
            col1.write(f"{time}")
            if col2.button("X", key=f"remove_{time}"):
                st.session_state['selected_times'].remove(time)

# yt-dlpλ₯Ό μ‚¬μš©ν•˜μ—¬ μ„ νƒλœ μ‹œκ°„λŒ€μ˜ μ˜μƒμ„ λ‹€μš΄λ‘œλ“œ
def download_clips():
    if st.session_state['selected_times']:
        st.write("### μ˜μƒ λ‹€μš΄λ‘œλ“œ")
        for idx, start_time in enumerate(st.session_state['selected_times']):
            start_time_obj = datetime.datetime.strptime(start_time, '%H:%M:%S')
            end_time_obj = start_time_obj + datetime.timedelta(minutes=1)

            # μ‹œμž‘ μ‹œκ°„κ³Ό 끝 μ‹œκ°„μ„ HH:MM:SS ν˜•μ‹μœΌλ‘œ λ³€ν™˜
            start_time_str = start_time_obj.strftime('%H:%M:%S')
            end_time_str = end_time_obj.strftime('%H:%M:%S')

            # yt-dlp λ‹€μš΄λ‘œλ“œ λͺ…λ Ήμ–΄ μ‹€ν–‰
            output_filename = f"clip_{idx + 1}.mp4"
            ydl_opts = {
                'outtmpl': output_filename,
                'download_sections': [f"*{start_time_str}-{end_time_str}"]
            }

            st.write(f"λ‹€μš΄λ‘œλ“œ 쀑: {start_time_str} ~ {end_time_str} | 파일λͺ…: {output_filename}")
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                ydl.download([vod_url])

# λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ μ±„νŒ… 크둀링 μ‹œμž‘
if 'chat_logs' not in st.session_state:
    st.session_state['chat_logs'] = None
if 'chat_counts' not in st.session_state:
    st.session_state['chat_counts'] = None
if 'laugh_counts' not in st.session_state:
    st.session_state['laugh_counts'] = None

if st.button("크둀링 μ‹œμž‘"):
    if vod_url:
        chat_logs, chat_counts, laugh_counts = crawl_chats(vod_url)

        st.session_state['chat_logs'] = chat_logs
        st.session_state['chat_counts'] = chat_counts
        st.session_state['laugh_counts'] = laugh_counts

        # 파일둜 μ €μž₯
        file_name = "chat_logs.txt"
        with open(file_name, "w") as file:
            file.write(chat_logs)

        # λ‹€μš΄λ‘œλ“œ λ²„νŠΌ ν‘œμ‹œ
        with open(file_name, "rb") as file:
            st.download_button(
                label="μ±„νŒ… 둜그 λ‹€μš΄λ‘œλ“œ",
                data=file,
                file_name=file_name,
                mime="text/plain"
            )
    else:
        st.warning("URL을 μž…λ ₯ν•˜μ„Έμš”.")

# 이전에 ν¬λ‘€λ§ν•œ κ²°κ³Όκ°€ 있으면 κ·Έλž˜ν”„μ™€ λ‹€μš΄λ‘œλ“œ λ²„νŠΌ ν‘œμ‹œ
if st.session_state['chat_logs']:
    # λ°μ΄ν„°ν”„λ ˆμž„ 생성
    times = [time.strftime('%H:%M:%S') for time in st.session_state['chat_counts'].keys()]
    chat_numbers = list(st.session_state['chat_counts'].values())
    laugh_numbers = [st.session_state['laugh_counts'].get(time, 0) for time in st.session_state['chat_counts'].keys()]
    df = pd.DataFrame({'μ‹œκ°„': times, '전체 μ±„νŒ… 개수': chat_numbers, 'γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수': laugh_numbers})

    # Plotly μ„  κ·Έλž˜ν”„ 그리기
    fig = go.Figure()

    # 전체 μ±„νŒ… 개수 μ„  κ·Έλž˜ν”„ μΆ”κ°€
    fig.add_trace(go.Scatter(
        x=df['μ‹œκ°„'],
        y=df['전체 μ±„νŒ… 개수'],
        mode='lines',
        name='전체 μ±„νŒ… 개수',
        line=dict(color='blue'),
        hovertemplate='%{x} - 전체 μ±„νŒ… 개수: %{y}<extra></extra>'
    ))

    # γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수 μ„  κ·Έλž˜ν”„ μΆ”κ°€
    fig.add_trace(go.Scatter(
        x=df['μ‹œκ°„'],
        y=df['γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수'],
        mode='lines',
        name='γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수',
        line=dict(color='red'),
        hovertemplate='%{x} - γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수: %{y}<extra></extra>'
    ))

    # κ·Έλž˜ν”„ λ ˆμ΄μ•„μ›ƒ μ„€μ •
    fig.update_layout(
        title="λΆ„λ‹Ή μ±„νŒ… 및 γ…‹γ…‹γ…‹γ…‹ μ±„νŒ… 개수",
        xaxis_title="μ‹œκ°„",
        yaxis_title="μ±„νŒ… 개수",
        xaxis=dict(showticklabels=False),  # xμΆ• μ‹œκ°„ λ ˆμ΄λΈ” μˆ¨κΉ€
        hovermode="x unified",  # 마우슀λ₯Ό μ˜¬λ Έμ„ λ•Œ ν•΄λ‹Ή xμΆ•μ—μ„œ 툴팁 ν‘œμ‹œ
        showlegend=True,
        margin=dict(l=50, r=50, t=100, b=100)
    )

    # κ·Έλž˜ν”„ 좜λ ₯ 및 클릭 이벀트 처리
    st.plotly_chart(fig, use_container_width=True)
    click_data = st.plotly_chart(fig).get_current_click()
    handle_time_selection(click_data)

    # μ„ νƒλœ μ‹œκ°„λŒ€ ν‘œμ‹œ
    display_selected_times()

    # λ‹€μš΄λ‘œλ“œ λ²„νŠΌ ν‘œμ‹œ
    if st.button("μ„ νƒλœ μ‹œκ°„λŒ€μ˜ μ˜μƒ λ‹€μš΄λ‘œλ“œ"):
        download_clips()