KIMOSSINO commited on
Commit
4dbac94
·
verified ·
1 Parent(s): a568782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -52
app.py CHANGED
@@ -1,29 +1,18 @@
1
  import gradio as gr
2
- from collections import Counter
3
  from bs4 import BeautifulSoup
4
  import requests
5
  import re
6
  from urllib.parse import urlparse
7
- import json
8
- from youtube_transcript_api import YouTubeTranscriptApi
9
 
10
  def extract_youtube_id(url):
11
  """استخراج معرف فيديو يوتيوب من الرابط"""
12
- if match := re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url):
13
- return match.group(1)
14
- return None
15
 
16
  def extract_tiktok_id(url):
17
  """استخراج معرف فيديو تيكتوك من الرابط"""
18
- if match := re.search(r'video/(\d+)', url):
19
- return match.group(1)
20
- return None
21
-
22
- def extract_instagram_shortcode(url):
23
- """استخراج معرف منشور انستغرام من الرابط"""
24
- if match := re.search(r'/(p|reel)/([A-Za-z0-9_-]+)', url):
25
- return match.group(2)
26
- return None
27
 
28
  def get_hashtags_from_text(text):
29
  """استخراج الهاشتاغات من النص"""
@@ -32,55 +21,54 @@ def get_hashtags_from_text(text):
32
  def extract_from_url(url):
33
  """استخراج البيانات من الرابط"""
34
  domain = urlparse(url).netloc
35
-
36
  headers = {
37
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
38
  }
39
 
40
  try:
 
41
  if 'youtube.com' in domain or 'youtu.be' in domain:
42
  video_id = extract_youtube_id(url)
43
  if not video_id:
44
  return "رابط يوتيوب غير صالح", "", ""
45
-
46
- api_url = f"https://www.youtube.com/watch?v={video_id}"
47
- response = requests.get(api_url, headers=headers)
48
 
49
- if 'watch-title' in response.text:
50
- soup = BeautifulSoup(response.text, 'html.parser')
51
- title = soup.find('meta', property='og:title')['content']
52
- description = soup.find('meta', property='og:description')['content']
53
- hashtags = get_hashtags_from_text(description)
54
-
55
- return title, description, "\n".join(hashtags)
56
-
 
 
 
 
 
 
 
 
 
57
  elif 'tiktok.com' in domain:
58
  video_id = extract_tiktok_id(url)
59
  if not video_id:
60
  return "رابط تيكتوك غير صالح", "", ""
61
-
62
- response = requests.get(url, headers=headers)
63
- if response.status_code == 200:
64
- soup = BeautifulSoup(response.text, 'html.parser')
65
- title = soup.find('meta', property='og:title')['content']
66
- description = soup.find('meta', property='og:description')['content']
67
- hashtags = get_hashtags_from_text(description)
68
-
69
- return title, description, "\n".join(hashtags)
70
-
71
- elif 'instagram.com' in domain:
72
- shortcode = extract_instagram_shortcode(url)
73
- if not shortcode:
74
- return "رابط انستغرام غير صالح", "", ""
75
-
76
  response = requests.get(url, headers=headers)
77
- if response.status_code == 200:
78
- soup = BeautifulSoup(response.text, 'html.parser')
79
- title = soup.find('meta', property='og:title')['content']
80
- description = soup.find('meta', property='og:description')['content']
81
- hashtags = get_hashtags_from_text(description)
82
-
83
- return title, description, "\n".join(hashtags)
 
 
 
 
 
 
 
84
 
85
  except Exception as e:
86
  return f"حدث خطأ: {str(e)}", "", ""
@@ -93,8 +81,8 @@ def gradio_interface():
93
 
94
  with gr.Row():
95
  url_input = gr.Textbox(
96
- label="🔗 أدخل رابط يوتيوب/تيكتوك/انستغرام",
97
- placeholder="https://..."
98
  )
99
 
100
  with gr.Row():
@@ -131,4 +119,4 @@ def gradio_interface():
131
 
132
  if __name__ == "__main__":
133
  demo = gradio_interface()
134
- demo.launch()
 
1
  import gradio as gr
 
2
  from bs4 import BeautifulSoup
3
  import requests
4
  import re
5
  from urllib.parse import urlparse
 
 
6
 
7
  def extract_youtube_id(url):
8
  """استخراج معرف فيديو يوتيوب من الرابط"""
9
+ match = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', url)
10
+ return match.group(1) if match else None
 
11
 
12
  def extract_tiktok_id(url):
13
  """استخراج معرف فيديو تيكتوك من الرابط"""
14
+ match = re.search(r'video/(\d+)', url)
15
+ return match.group(1) if match else None
 
 
 
 
 
 
 
16
 
17
  def get_hashtags_from_text(text):
18
  """استخراج الهاشتاغات من النص"""
 
21
  def extract_from_url(url):
22
  """استخراج البيانات من الرابط"""
23
  domain = urlparse(url).netloc
 
24
  headers = {
25
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
26
  }
27
 
28
  try:
29
+ # استخراج البيانات من YouTube
30
  if 'youtube.com' in domain or 'youtu.be' in domain:
31
  video_id = extract_youtube_id(url)
32
  if not video_id:
33
  return "رابط يوتيوب غير صالح", "", ""
 
 
 
34
 
35
+ response = requests.get(url, headers=headers)
36
+ soup = BeautifulSoup(response.text, 'html.parser')
37
+
38
+ # استخراج العنوان
39
+ title_element = soup.find('h1', {'id': 'title'})
40
+ title = title_element.text.strip() if title_element else "العنوان غير متوفر"
41
+
42
+ # استخراج الوصف
43
+ description_element = soup.find('div', {'id': 'description'})
44
+ description = description_element.text.strip() if description_element else "الوصف غير متوفر"
45
+
46
+ # استخراج الهاشتاغات
47
+ hashtags = get_hashtags_from_text(description)
48
+
49
+ return title, description, "\n".join(hashtags)
50
+
51
+ # استخراج البيانات من TikTok
52
  elif 'tiktok.com' in domain:
53
  video_id = extract_tiktok_id(url)
54
  if not video_id:
55
  return "رابط تيكتوك غير صالح", "", ""
56
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  response = requests.get(url, headers=headers)
58
+ soup = BeautifulSoup(response.text, 'html.parser')
59
+
60
+ # استخراج العنوان
61
+ title_element = soup.find('h1')
62
+ title = title_element.text.strip() if title_element else "العنوان غير متوفر"
63
+
64
+ # استخراج الوصف
65
+ description = title # الوصف موجود داخل نفس العنصر <h1>
66
+
67
+ # استخراج الهاشتاغات
68
+ hashtags_elements = soup.find_all('a', {'class': re.compile(r'.*hashtag.*')})
69
+ hashtags = [tag.text.strip() for tag in hashtags_elements]
70
+
71
+ return title, description, "\n".join(hashtags)
72
 
73
  except Exception as e:
74
  return f"حدث خطأ: {str(e)}", "", ""
 
81
 
82
  with gr.Row():
83
  url_input = gr.Textbox(
84
+ label="🔗 أدخل رابط يوتيوب/تيكتوك",
85
+ placeholder="https://.../video/..."
86
  )
87
 
88
  with gr.Row():
 
119
 
120
  if __name__ == "__main__":
121
  demo = gradio_interface()
122
+ demo.launch()