File size: 4,518 Bytes
ed0485e b4bbaee 8c8dfe8 ed0485e cbff93c ed0485e e5c8ff6 cbff93c ed0485e e5c8ff6 ed0485e 8c8dfe8 ed0485e e5c8ff6 ed0485e 282dd48 ed0485e 282dd48 f508547 282dd48 ed0485e 282dd48 ed0485e 8c8dfe8 ed0485e 8c8dfe8 ed0485e f508547 ed0485e f508547 ed0485e 282dd48 ed0485e 282dd48 ed0485e 282dd48 ed0485e 282dd48 ed0485e 282dd48 b4bbaee cbff93c ed0485e |
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 |
import gradio as gr
import re
import logging
from typing import Tuple, Optional
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def extract_comment_data(comment_text: str) -> Tuple[Optional[str], Optional[str], int, int]:
"""Извлекает данные из комментария"""
try:
# Пропускаем информацию о посте
if 'отметок "Нравится"' in comment_text:
return None, None, 0, 0
# Извлекаем имя пользователя
username_match = re.search(r"Фото профиля ([^\n]+)", comment_text)
if not username_match:
return None, None, 0, 0
username = username_match.group(1).strip()
# Извлекаем текст комментария
lines = comment_text.split('\n')
comment = ""
for i, line in enumerate(lines):
if username in line and i + 1 < len(lines):
comment = lines[i + 1].strip()
# Очищаем комментарий
comment = re.sub(r'\d+\s*(?:ч\.|нед\.)\s*$', '', comment)
comment = re.sub(r'"Нравится":\s*\d+\s*Ответить\s*$', '', comment)
break
# Извлекаем лайки
likes_match = re.search(r'"Нравится":\s*(\d+)', comment_text)
likes = int(likes_match.group(1)) if likes_match else 0
# Извлекаем время
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
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:
# Разделяем на блоки комментариев
blocks = re.split(r'(?=Фото профиля)', all_comments)
blocks = [b.strip() for b in blocks if b.strip()]
comments_data = []
# Обрабатываем каждый блок
for block in blocks:
username, comment, likes, time = extract_comment_data(block)
if username and comment:
comments_data.append({
'username': username,
'comment': comment,
'likes': likes,
'time': time
})
# Формируем выходные данные
usernames = "\n".join(item['username'] for item in comments_data)
comments = "\n".join(item['comment'] for item in comments_data)
likes = "\n".join(str(item['likes']) for item in comments_data)
total_likes = sum(item['likes'] for item in comments_data)
analytics = f"""
📊 Анализ комментариев:
Всего комментариев: {len(comments_data)}
Уникальных пользователей: {len(set(item['username'] for item in comments_data))}
Общее количество лайков: {total_likes}
"""
return analytics, usernames, comments, likes, 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"),
gr.Number(label="Likes", value=0),
gr.Textbox(label="Post Date"),
gr.Textbox(label="Description", lines=3),
gr.Number(label="Comment Count", value=0),
gr.Textbox(label="Comments", lines=10)
],
outputs=[
gr.Textbox(label="Analytics Summary", lines=10),
gr.Textbox(label="Usernames"),
gr.Textbox(label="Comments"),
gr.Textbox(label="Likes Chronology"),
gr.Textbox(label="Total Likes on Comments")
],
title="Instagram Comment Analyzer",
description="Анализатор комментариев Instagram"
)
if __name__ == "__main__":
iface.launch() |