File size: 6,639 Bytes
fe42b4a
e512cee
 
 
 
fe42b4a
e512cee
fe42b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e512cee
 
fe42b4a
 
 
 
 
 
 
 
e512cee
fe42b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e512cee
 
 
 
fe42b4a
e512cee
 
 
fe42b4a
 
 
 
 
 
 
e512cee
fe42b4a
 
62a9a6a
 
 
 
 
 
 
fe42b4a
 
e512cee
 
62a9a6a
 
 
 
fe42b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
62a9a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import plotly.graph_objects as go
from collections import defaultdict
import datetime
import re
import requests

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

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

# μ±„νŒ… 크둀링 ν•¨μˆ˜
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)

    # μ±„νŒ… 데이터λ₯Ό 순차적으둜 μš”μ²­ν•˜μ—¬ κ°€μ Έμ˜€κΈ°
    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}")

                # μ‹œκ°„λŒ€λ³„λ‘œ μ±„νŒ… 개수 계산
                time_obj = datetime.datetime.strptime(formatted_time, '%H:%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

        # λ‹€μŒ μš”μ²­μ„ μœ„ν•΄ 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

# λ²„νŠΌμ„ λˆŒλ €μ„ λ•Œ μ±„νŒ… 크둀링 μ‹œμž‘
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:
            btn = 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=True  # xμΆ• μ‹œκ°„ λ ˆμ΄λΈ” ν‘œμ‹œ
        ),
        hovermode="x unified",  # 마우슀λ₯Ό μ˜¬λ Έμ„ λ•Œ ν•΄λ‹Ή xμΆ•μ—μ„œ 툴팁 ν‘œμ‹œ
        showlegend=True,  # λ²”λ‘€ ν‘œμ‹œ
        margin=dict(l=50, r=50, t=100, b=100)  # κ·Έλž˜ν”„ μ•„λž˜μͺ½ 여백을 μ‘°μ •
    )

    # κ·Έλž˜ν”„ 좜λ ₯
    st.plotly_chart(fig)