najimq59 commited on
Commit
7c771ea
·
verified ·
1 Parent(s): 6de5caf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import datetime
3
+ import json
4
+ import re
5
+
6
+ import pycountry # قد لا تكون ضرورية هنا ما لم نكن نعرض بيانات مستخدم من الفيديو
7
+ from curl_cffi import requests
8
+ from lxml import html
9
+
10
+ # دالة لجلب تفاصيل الفيديو من رابط تيك توك
11
+ def get_tiktok_video_details(video_url):
12
+ # استخدام نمط regex لاستخلاص معرف الفيديو من الرابط (إذا كان موجوداً)
13
+ # تيك توك لديها روابط مختلفة، مثل:
14
+ # https://www.tiktok.com/@username/video/VIDEO_ID
15
+ # https://www.tiktok.com/t/VIDEO_ID/
16
+ # https://vm.tiktok.com/ZM.../
17
+ match_video_id = re.search(r'(?:video/|/v/|/t/|vm\.tiktok\.com/ZM)([0-9A-Za-z_-]+)', video_url)
18
+
19
+ if not match_video_id:
20
+ return "خطأ: رابط الفيديو غير صالح. يرجى إدخال رابط فيديو تيك توك صحيح."
21
+
22
+ video_id = match_video_id.group(1)
23
+
24
+ # بناء رابط صفحة الفيديو (قد لا يكون URL الأساسي هو الأفضل دائماً، أحياناً vm.tiktok.com يعمل بشكل أفضل)
25
+ # لكن سنستخدم الصيغة الأكثر شيوعاً التي تعرض معلومات المستخدم أيضاً.
26
+ # تيك توك تعيد التوجيه من الروابط القصيرة إلى الروابط الطويلة تلقائياً.
27
+ final_url = f"https://www.tiktok.com/foryou?is_from_webapp=1&sender_device=pc&web_id=7372990422119335456&vid={video_id}"
28
+ # يمكننا تجربة أيضاً: f"https://www.tiktok.com/embed/v2/{video_id}" أو f"https://www.tiktok.com/share/video/{video_id}"
29
+
30
+ print(f"محاولة جلب بيانات الفيديو من: {final_url}")
31
+
32
+ try:
33
+ r = requests.get(final_url, impersonate="chrome", timeout=15)
34
+ r.raise_for_status() # ترفع استثناء لأخطاء HTTP
35
+
36
+ tree = html.fromstring(r.text, parser=html.HTMLParser(encoding="utf8"))
37
+
38
+ # البحث عن السكربت الذي يحتوي على البيانات الأولية
39
+ # يختلف المسار لبيانات الفيديو عن بيانات المستخدم
40
+ script_tag = tree.xpath('//script[@id="__UNIVERSAL_DATA_FOR_REHYDRATION__"]')
41
+
42
+ if not script_tag:
43
+ return ("خطأ: لم يتم العثور على وسم البيانات في الصفحة. "
44
+ "قد يكون هيكل الصفحة قد تغير، أو تم حظر الطلب.")
45
+
46
+ data = json.loads(script_tag[0].text)
47
+
48
+ # المسار المتوقع لبيانات الفيديو في JSON
49
+ # هذا المسار يمكن أن يتغير مع تحديثات تيك توك
50
+ video_data_raw = data.get("__DEFAULT_SCOPE__", {})\
51
+ .get("webapp.video-detail", {})\
52
+ .get("itemInfo", {})\
53
+ .get("itemStruct", {})
54
+
55
+ if not video_data_raw:
56
+ return (f"خطأ: لم يتم العثور على تفاصيل للفيديو: '{video_id}'. "
57
+ "تأكد من صحة الرابط وأن الفيديو متاح للعامة.")
58
+
59
+ # استخلاص البيانات من الكائن المستخرج
60
+ video_info = {
61
+ "title": video_data_raw.get("desc", "لا يتوفر عنوان"),
62
+ "author_username": video_data_raw.get("author", {}).get("uniqueId", "غير معروف"),
63
+ "author_nickname": video_data_raw.get("author", {}).get("nickname", "غير معروف"),
64
+ "create_time": datetime.datetime.fromtimestamp(video_data_raw.get("createTime", 0)).strftime('%Y-%m-%d %H:%M:%S') if video_data_raw.get("createTime") else "غير معروف",
65
+ "play_count": f"{video_data_raw.get('stats', {}).get('playCount', 0):,}",
66
+ "digg_count": f"{video_data_raw.get('stats', {}).get('diggCount', 0):,}", # الإعجابات
67
+ "comment_count": f"{video_data_raw.get('stats', {}).get('commentCount', 0):,}",
68
+ "share_count": f"{video_data_raw.get('stats', {}).get('shareCount', 0):,}",
69
+ "save_count": f"{video_data_raw.get('stats', {}).get('collectCount', 0):,}", # حفظ الفيديو
70
+ "cover_url": video_data_raw.get("video", {}).get("cover", {}).get("url_list", [""])[0],
71
+ "video_url": f"https://www.tiktok.com/@{video_data_raw.get('author', {}).get('uniqueId', 'username')}/video/{video_id}"
72
+ }
73
+
74
+ # بناء مخرج HTML المنسق
75
+ output_html = f"""
76
+ <div style="direction: rtl; text-align: right; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; padding: 20px; border-radius: 8px; background-color: #ffffff; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
77
+ <h2 style="margin: 0; color: #FE2C55; font-size: 1.8em; border-bottom: 2px solid #FE2C55; padding-bottom: 5px;">{video_info['title']}</h2>
78
+ <p style="margin: 10px 0; color: #555; font-size: 1.1em;"><strong>المستخدم:</strong> <a href="https://www.tiktok.com/@{video_info['author_username']}" target="_blank" style="color: #007bff; text-decoration: none;">@{video_info['author_username']} ({video_info['author_nickname']})</a></p>
79
+ <p style="margin: 5px 0; color: #555; font-size: 1.1em;"><strong>تاريخ الإنشاء:</strong> {video_info['create_time']}</p>
80
+
81
+ <div style="display: flex; justify-content: center; margin-top: 20px;">
82
+ <img src="{video_info['cover_url']}" alt="صورة غلاف الفيديو" style="max-width: 100%; height: auto; border-radius: 8px; object-fit: cover; box-shadow: 0 4px 10px rgba(0,0,0,0.15);">
83
+ </div>
84
+
85
+ <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 15px; margin-top: 25px; text-align: center;">
86
+ <div style="background-color: #f0f2f5; padding: 10px; border-radius: 8px;">
87
+ <strong style="display: block; font-size: 1.4em; color: #FE2C55;">{video_info['play_count']}</strong>
88
+ <span style="font-size: 0.9em; color: #777;">مشاهدة</span>
89
+ </div>
90
+ <div style="background-color: #f0f2f5; padding: 10px; border-radius: 8px;">
91
+ <strong style="display: block; font-size: 1.4em; color: #28a745;">{video_info['digg_count']}</strong>
92
+ <span style="font-size: 0.9em; color: #777;">إعجاب</span>
93
+ </div>
94
+ <div style="background-color: #f0f2f5; padding: 10px; border-radius: 8px;">
95
+ <strong style="display: block; font-size: 1.4em; color: #6c757d;">{video_info['comment_count']}</strong>
96
+ <span style="font-size: 0.9em; color: #777;">تعليق</span>
97
+ </div>
98
+ <div style="background-color: #f0f2f5; padding: 10px; border-radius: 8px;">
99
+ <strong style="display: block; font-size: 1.4em; color: #007bff;">{video_info['share_count']}</strong>
100
+ <span style="font-size: 0.9em; color: #777;">مشاركة</span>
101
+ </div>
102
+ <div style="background-color: #f0f2f5; padding: 10px; border-radius: 8px;">
103
+ <strong style="display: block; font-size: 1.4em; color: #ffc107;">{video_info['save_count']}</strong>
104
+ <span style="font-size: 0.9em; color: #777;">حفظ</span>
105
+ </div>
106
+ </div>
107
+
108
+ <div style="margin-top: 25px; text-align: center;">
109
+ <a href="{video_info['video_url']}" target="_blank" style="background-color: #FE2C55; color: white; padding: 10px 20px; border-radius: 5px; text-decoration: none; font-weight: bold; display: inline-block;">مشاهدة الفيديو على تيك توك</a>
110
+ </div>
111
+
112
+ <p style="font-size: 0.9em; color: #a00; margin-top: 30px; padding-top: 15px; border-top: 1px dashed #f00; text-align: center; font-weight: bold;">
113
+ ⚠️ ملاحظة هامة: البيانات مستخلصة من صفحة تيك توك.
114
+ <span style="display: block; margin-top: 5px; font-size: 0.8em; color: #c00;">هذه الطريقة غير رسمية وغير مستقرة وقد تتوقف عن العمل في أي وقت.</span>
115
+ <span style="display: block; margin-top: 5px; font-size: 0.8em; color: #c00;">استخدم على مسؤوليتك الخاصة ولأغراض تعليمية فقط. لا يمكن لهذا التطبيق كتابة تعليقات.</span>
116
+ </p>
117
+ </div>
118
+ """
119
+ return gr.update(value=output_html, visible=True), gr.update(value="", visible=False)
120
+
121
+ except requests.exceptions.RequestException as e:
122
+ error_msg = f"خطأ في الاتصال بتيك توك: {e}. قد يكون عنوان IP محظوراً."
123
+ return gr.update(value="", visible=False), gr.update(value=error_msg, visible=True)
124
+ except (KeyError, ValueError, IndexError, json.JSONDecodeError) as e:
125
+ error_msg = f"خطأ في تحليل بيانات الفيديو: {e}. قد يكون هيكل صفحة تيك توك قد تغير."
126
+ return gr.update(value="", visible=False), gr.update(value=error_msg, visible=True)
127
+ except Exception as e:
128
+ error_msg = f"حدث خطأ غير متوقع: {e}. يرجى المحاولة مرة أخرى."
129
+ return gr.update(value="", visible=False), gr.update(value=error_msg, visible=True)
130
+
131
+ # --- واجهة Gradio ---
132
+ with gr.Blocks(theme="soft", title="جالب معلومات فيديو تيك توك") as demo:
133
+ gr.Markdown("# جالب معلومات فيديو تيك توك")
134
+ gr.Markdown("أدخل رابط فيديو تيك توك (مثال: `https://www.tiktok.com/@username/video/VIDEO_ID`) للحصول على تفاصيله.")
135
+
136
+ with gr.Row():
137
+ video_url_input = gr.Textbox(label="رابط فيديو تيك توك", placeholder="مثال: https://www.tiktok.com/@cristiano/video/7300762635957095713", scale=3)
138
+ submit_button = gr.Button("جلب التفاصيل", scale=1)
139
+
140
+ # مكونات الإخراج
141
+ output_html_display = gr.HTML(label="تفاصيل الفيديو", visible=False)
142
+ error_message_display = gr.Markdown(visible=False)
143
+
144
+ # تعريف التفاعل
145
+ submit_button.click(
146
+ fn=get_tiktok_video_details,
147
+ inputs=video_url_input,
148
+ outputs=[output_html_display, error_message_display]
149
+ )
150
+
151
+ if __name__ == "__main__":
152
+ demo.launch()