hashtagtik / app.py
KIMOSSINO's picture
Update app.py
3bd11cb verified
import gradio as gr
from bs4 import BeautifulSoup
def analyze_videos(file):
try:
# قراءة محتوى الملف
if hasattr(file, 'read'):
content = file.read()
else:
with open(file.name, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
return f"⚠️ خطأ أثناء قراءة الملف: {str(e)}"
# تحليل HTML باستخدام BeautifulSoup
try:
soup = BeautifulSoup(content, 'html.parser')
except Exception as e:
return f"⚠️ خطأ في تحليل محتوى HTML: {str(e)}"
# استخراج بيانات الفيديوهات
videos_data = []
video_elements = soup.find_all('a', class_="css-1wrhn5c-AMetaCaptionLine")
if not video_elements:
return "⚠️ لم يتم العثور على أي بيانات مطابقة."
for video in video_elements:
video_info = {}
# استخراج الرابط
video_info["Link"] = video.get('href', 'رابط غير متوفر')
# استخراج العنوان
title_element = video.find('span', class_="css-j2a19r-SpanText")
video_info["Title"] = title_element.get_text(strip=True) if title_element else "عنوان غير متوفر"
# استخراج الهاشتاغات
hashtags = [
tag.get_text(strip=True)
for tag in video.find_all('strong', class_="css-1p6dp51-StrongText")
]
video_info["Hashtags"] = ", ".join(hashtags) if hashtags else "هاشتاغات غير متوفرة"
# استخراج عدد المشاهدات
views_element = video.find_next('strong', {"data-e2e": "video-views"})
video_info["Views"] = views_element.get_text(strip=True) if views_element else "عدد المشاهدات غير متوفر"
# استخراج عدد الإعجابات
likes_element = video.find('strong', {"data-e2e": "browse-like-count"})
video_info["Likes"] = likes_element.get_text(strip=True) if likes_element else "غير متوفر"
# استخراج عدد التعليقات
comments_element = video.find('strong', {"data-e2e": "browse-comment-count"})
video_info["Comments"] = comments_element.get_text(strip=True) if comments_element else "غير متوفر"
# استخراج عدد المشاركات
shares_element = video.find('strong', {"data-e2e": "undefined-count"})
video_info["Shares"] = shares_element.get_text(strip=True) if shares_element else "غير متوفر"
videos_data.append(video_info)
# تجهيز النصوص النهائية
videos_summary = "\n\n".join(
f"📹 الفيديو {i+1}:\n"
f"- الرابط: {video['Link']}\n"
f"- العنوان: {video['Title']}\n"
f"- الهاشتاغات: {video['Hashtags']}\n"
f"- عدد المشاهدات: {video['Views']}\n"
f"- عدد الإعجابات: {video['Likes']}\n"
f"- عدد التعليقات: {video['Comments']}\n"
f"- عدد المشاركات: {video['Shares']}"
for i, video in enumerate(videos_data)
)
return videos_summary or "⚠️ لم يتم استخراج أي معلومات."
# إنشاء واجهة Gradio
def gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("## 📝 محلل فيديوهات تيك توك")
with gr.Row():
file_input = gr.File(label="📂 رفع ملف HTML", file_types=[".html"])
with gr.Row():
analyze_btn = gr.Button("تحليل البيانات", variant="primary")
with gr.Row():
output_box = gr.Textbox(
label="📜 البيانات المستخرجة",
lines=20,
interactive=False,
placeholder="ستظهر البيانات هنا"
)
analyze_btn.click(
fn=analyze_videos,
inputs=[file_input],
outputs=[output_box],
)
return demo
# تشغيل التطبيق
if __name__ == "__main__":
demo = gradio_interface()
demo.launch()