File size: 10,350 Bytes
ed0485e
8c00b86
2a2a846
 
 
 
1d5717d
cbff93c
2a2a846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed0485e
 
 
2a2a846
282dd48
2a2a846
 
1d5717d
2a2a846
 
 
 
1d5717d
 
 
2a2a846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282dd48
2a2a846
1d5717d
2a2a846
 
1d5717d
 
2a2a846
 
1d5717d
8c8dfe8
ed0485e
1d5717d
 
 
 
 
 
 
 
 
 
 
 
ed0485e
8c8dfe8
2a2a846
 
 
 
 
 
 
8c00b86
f508547
2a2a846
 
f508547
2a2a846
282dd48
ed0485e
282dd48
2a2a846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282dd48
 
2a2a846
 
 
 
 
282dd48
2a2a846
 
76cf8fb
282dd48
bf4724c
2a2a846
 
 
 
 
 
 
 
 
2e66c7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a2a846
2e66c7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a2a846
2e66c7c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import gradio as gr
import re
import emoji
import logging
from typing import Tuple, Optional
from functools import lru_cache
from collections import Counter

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def count_emojis(text: str) -> int:
    """Подсчет количества эмодзи в тексте"""
    return len([c for c in text if c in emoji.EMOJI_DATA])

def extract_mentions(text: str) -> list:
    """Извлечение упоминаний пользователей"""
    return re.findall(r'@(\w+)', text)

def is_spam(text: str) -> bool:
    """Определение спам-комментариев"""
    spam_indicators = ['🔥' * 3, '❤️' * 3, 'follow me', 'check my']
    return any(indicator in text.lower() for indicator in spam_indicators)

def extract_comment_data(comment_text: str) -> Tuple[Optional[str], Optional[str], int, int]:
    """Извлекает данные из комментария"""
    try:
        # Extract username
        username_match = re.search(r'Фото профиля\s+(.+?)\n', comment_text)
        username = username_match.group(1).strip() if username_match else None
        
        if not username:
            return None, None, 0, 0
        
        # Extract comment text
        comment_lines = comment_text.split('\n')
        comment = ""
        time_pattern = r'\d+\s*(?:ч\.|нед\.)'
        
        # Identify where the comment text starts
        for i, line in enumerate(comment_lines):
            if re.search(time_pattern, line):
                if i + 1 < len(comment_lines):
                    comment = comment_lines[i + 1].strip()
                break
        
        # Clean up comment text
        comment = re.sub(r'\d+\s*(?:ч\.|нед\.)\s*$', '', comment)
        comment = re.sub(r'"Нравится":\s*\d+\s*Ответить\s*$', '', comment)
        
        # Extract likes
        likes_match = re.search(r'"Нравится":\s*(\d+)', comment_text)
        likes = int(likes_match.group(1)) if likes_match else 0
        
        # Extract time
        time_match = re.search(r'(\d+)\s*(?:ч\.|нед\.)', comment_text)
        time = int(time_match.group(1)) if time_match else 0
        
        return username, comment.strip(), likes, time

    except Exception as e:
        logger.error(f"Error extracting data: {e}")
        return None, None, 0, 0

@lru_cache(maxsize=100)
def analyze_post(content_type: str, link: str, post_likes: int, 
                post_date: str, description: str, comment_count: int, 
                all_comments: str) -> Tuple[str, str, str, str, str]:
    """Анализирует пост и комментарии"""
    try:
        if not all_comments or 'Фото профиля' not in all_comments:
            return "Ошибка: неверный формат данных", "", "", "", "0"

        blocks = re.split(r'(?=Фото профиля)', all_comments)
        blocks = [b.strip() for b in blocks if b.strip()]
        
        comments_data = []
        total_emojis = 0
        mentions = []
        spam_count = 0
        
        for block in blocks:
            username, comment, likes, time = extract_comment_data(block)
            if username and comment:
                emoji_count = count_emojis(comment)
                comment_mentions = extract_mentions(comment)
                is_spam_comment = is_spam(comment)
                
                comments_data.append({
                    'username': username,
                    'comment': comment,
                    'likes': likes,
                    'time': time,
                    'emoji_count': emoji_count,
                    'mentions': comment_mentions,
                    'is_spam': is_spam_comment
                })
                
                total_emojis += emoji_count
                mentions.extend(comment_mentions)
                if is_spam_comment:
                    spam_count += 1

        # Подсчет статистики
        total_comments = len(comments_data)
        unique_users = len(set(item['username'] for item in comments_data))
        total_likes = sum(item['likes'] for item in comments_data)
        avg_likes = total_likes / total_comments if total_comments > 0 else 0

        # Топ комментаторы
        commenter_counts = Counter(item['username'] for item in comments_data)
        top_commenters = commenter_counts.most_common(5)

        analytics = f"""
        📊 Подробный анализ комментариев:
        Основные метрики:
        • Всего комментариев: {total_comments}
        • Уникальных пользователей: {unique_users}
        • Общее количество лайков: {total_likes}
        • Среднее количество лайков: {avg_likes:.1f}
        Дополнительная информация:
        • Использовано эмодзи: {total_emojis}
        • Количество упоминаний: {len(mentions)}
        • Выявлено спам-комментариев: {spam_count}
        Топ комментаторы:
        {chr(10).join(f'• {user}: {count} комментария' for user, count in top_commenters if count > 1)}
        """

        return (
            analytics,
            "\n".join(item['username'] for item in comments_data),
            "\n".join(item['comment'] for item in comments_data),
            "\n".join(str(item['likes']) for item in comments_data),
            str(total_likes)
        )

    except Exception as e:
        logger.error(f"Analysis error: {e}")
        return str(e), "", "", "", "0"

# Создаем интерфейс Gradio
iface = gr.Interface(
    fn=analyze_post,
    inputs=[
        gr.Radio(
            choices=["Photo", "Video"],
            label="Content Type",
            value="Photo"
        ),
        gr.Textbox(
            label="Link to Post",
            placeholder="Вставьте ссылку на пост"
        ),
        gr.Number(
            label="Likes",
            value=0,
            minimum=0
        ),
        gr.Textbox(
            label="Post Date",
            placeholder="YYYY-MM-DD"
        ),
        gr.Textbox(
            label="Description",
            lines=3,
            placeholder="Описание поста"
        ),
        gr.Number(
            label="Comment Count",
            value=0,
            minimum=0
        ),
        gr.Textbox(
            label="Comments",
            lines=10,
            placeholder="Вставьте комментарии"
        )
    ],
    outputs=[
        gr.Textbox(label="Analytics Summary", lines=15),
        gr.Textbox(label="Usernames"),
        gr.Textbox(label="Comments"),
        gr.Textbox(label="Likes Chronology"),
        gr.Textbox(label="Total Likes on Comments")
    ],
    title="Enhanced Instagram Comment Analyzer",
    description="Анализатор комментариев Instagram с расширенной аналитикой",
    theme="default"
)

if __name__ == "__main__":
    try:
        iface.launch(
            share=True,  # Создает публичную ссылку
            debug=True,  # Включает режим отладки
            show_error=True  # Показывает подробности ошибок
        )
    except Exception as e:
        logger.error(f"Error launching interface: {e}", exc_info=True)
import re
import emoji
import gradio as gr
from collections import defaultdict, Counter

def extract_comment_data(comment_text: str) -> dict:
    """Extracts data from a comment string."""
    comment_data = {}
    
    # Username extraction (improved robustness)
    match = re.search(r"Фото профиля\s*(.+?)\n", comment_text)
    comment_data["username"] = match.group(1).strip() if match else None
    if not comment_data["username"]:
        return None # Skip if no username found

    # Comment text extraction (handling multiple lines & various time formats)
    lines = comment_text.splitlines()
    comment_text = ""
    for i, line in enumerate(lines):
        if re.search(r"\d+\s*(?:нед\.|ч\.)", line): #Matches days or hours
            comment_text = "\n".join(lines[i+1:]).strip()
            break
        comment_text += line + "\n"
    comment_text = comment_text.strip()
    comment_data["comment"] = comment_text

    # Likes extraction (more flexible regex)
    match = re.search(r'"Нравится":\s*(\d+)', comment_text)
    comment_data["likes"] = int(match.group(1)) if match else 0

    # Time extraction (more robust to variations)
    time_match = re.search(r"(\d+)\s*(?:нед\.|ч\.)", comment_text)
    comment_data["time"] = int(time_match.group(1)) if time_match else None


    return comment_data

def analyze_comments(comments_text: str) -> dict:
    """Analyzes a block of comments text."""
    comments = []
    blocks = re.split(r'(Фото профиля)', comments_text, flags=re.IGNORECASE)
    for i in range(1,len(blocks),2):
        comment_data = extract_comment_data(blocks[i])
        if comment_data:
            comments.append(comment_data)

    # Aggregate data
    analytics = defaultdict(int)
    unique_users = set()
    top_commenters = Counter()

    for comment in comments:
        analytics["total_comments"] += 1
        unique_users.add(comment["username"])
        analytics["total_likes"] += comment["likes"]
        top_commenters[comment["username"]] += 1
        analytics["emojis"] += len(emoji.demojize(comment["comment"])) # Counts emojis

    analytics["unique_users"] = len(unique_users)
    analytics["avg_likes"] = analytics["total_likes"] / analytics["total_comments"] if analytics["total_comments"] > 0 else 0
    analytics["top_commenters"] = dict(top_commenters.most_common(5))


    return analytics, comments


iface = gr.Interface(
    fn=analyze_comments,
    inputs=gr.Textbox(label="Instagram Comments (Paste here)", lines=10),
    outputs=[
        gr.Textbox(label="Analytics Summary"),
        gr.JSON(label="Individual Comment Data")
    ],
    title="Enhanced Instagram Comment Analyzer",
    description="Improved analyzer for Instagram comments.",
)

iface.launch(share=True)