najimq59 commited on
Commit
0908b36
·
verified ·
1 Parent(s): 649dd50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -6,6 +6,9 @@ from curl_cffi import requests
6
  from lxml import html
7
  import pycountry
8
 
 
 
 
9
 
10
  class TikTokUser:
11
  def __init__(self, data) -> None:
@@ -27,11 +30,9 @@ class TikTokUser:
27
  self.video_count = stats.get("videoCount", 0)
28
  self.friends = stats.get("friendCount", 0)
29
 
30
- # تحويل الطابع الزمني إلى صيغة قابلة للقراءة
31
  if self.created:
32
  self.created = datetime.datetime.fromtimestamp(self.created).strftime("%Y-%m-%d %H:%M:%S")
33
 
34
- # تحويل رمز البلد واللغة إلى الأسماء
35
  if self.country:
36
  c = pycountry.countries.get(alpha_2=self.country.upper())
37
  self.country = c.name if c else self.country
@@ -40,7 +41,6 @@ class TikTokUser:
40
  l = pycountry.languages.get(alpha_2=self.language.lower())
41
  self.language = l.name if l else self.language
42
 
43
- # إضافة رابط bio إذا وُجد
44
  if bio_link := user.get("bioLink", ""):
45
  self.bio += "\n" + bio_link.get("link", "")
46
 
@@ -84,7 +84,7 @@ def classify_bio(bio: str) -> str:
84
  يصنف السيرة الذاتية إلى فئات مبسطة بناءً على كلمات مفتاحية.
85
  """
86
  lower_bio = bio.lower()
87
- if any(kw in lower_bio for kw in ["tech", "ai", "developer", "software", "python"]):
88
  return "تكنولوجيا"
89
  if any(kw in lower_bio for kw in ["sports", "football", "fitness", "gym", "athlete"]):
90
  return "رياضة"
@@ -95,23 +95,25 @@ def classify_bio(bio: str) -> str:
95
  return "عام"
96
 
97
 
98
- def extract_following(page_html: str, limit: int = 10) -> list[str]:
99
  """
100
- يحاول استخراج قائمة الأسماء (uniqueId) للحسابات التي يتابعها المستخدم،
101
- بأخذ أول N = limit اسمًا يظهر في HTML. هذه طريقة تقديرية وليست رسمية.
102
  """
103
- # نبحث عن كل مواضع "uniqueId":"someUser"
104
- matches = re.findall(r'"uniqueId":"(.*?)"', page_html)
105
- # نمسح duplicates ونقتصر على first 'limit'
106
- seen = set()
107
- following = []
108
- for uid in matches:
109
- if uid not in seen:
110
- seen.add(uid)
111
- following.append(uid)
112
- if len(following) >= limit:
113
- break
114
- return following
 
 
115
 
116
 
117
  def lookup_tiktok(username: str):
@@ -123,19 +125,16 @@ def lookup_tiktok(username: str):
123
  # تصنيف السيرة الذاتية
124
  category = classify_bio(user.bio)
125
 
126
- # استخراج أول 10 أسماء يتابعها
127
- following_list = extract_following(page_html, limit=10)
128
-
129
- # تنسيق قائمة المتابعين الذين يتابعهم
130
  if following_list:
131
  following_md = "\n".join(f"- @{uid}" for uid in following_list)
132
  else:
133
- following_md = "لا توجد بيانات متاحة أو الحساب لا يعرض من يتابعهم."
134
 
135
- # تركيب النص النهائي
136
  info_md = f"""
137
- **الاسم (Name):** {user.name}
138
- **اسم المستخدم:** @{user.username}
139
  **البلد (Country):** {user.country}
140
  **اللغة (Language):** {user.language}
141
  **تاريخ الإنشاء (Created):** {user.created}
 
6
  from lxml import html
7
  import pycountry
8
 
9
+ # إضافة مكتبة TikTokApi لجلب قائمة المتابعين الذين يتابعهم المستخدم
10
+ from TikTokApi import TikTokApi
11
+
12
 
13
  class TikTokUser:
14
  def __init__(self, data) -> None:
 
30
  self.video_count = stats.get("videoCount", 0)
31
  self.friends = stats.get("friendCount", 0)
32
 
 
33
  if self.created:
34
  self.created = datetime.datetime.fromtimestamp(self.created).strftime("%Y-%m-%d %H:%M:%S")
35
 
 
36
  if self.country:
37
  c = pycountry.countries.get(alpha_2=self.country.upper())
38
  self.country = c.name if c else self.country
 
41
  l = pycountry.languages.get(alpha_2=self.language.lower())
42
  self.language = l.name if l else self.language
43
 
 
44
  if bio_link := user.get("bioLink", ""):
45
  self.bio += "\n" + bio_link.get("link", "")
46
 
 
84
  يصنف السيرة الذاتية إلى فئات مبسطة بناءً على كلمات مفتاحية.
85
  """
86
  lower_bio = bio.lower()
87
+ if any(kw in lower_bio for kw in ["tech", "ai", "developer", "python", "software"]):
88
  return "تكنولوجيا"
89
  if any(kw in lower_bio for kw in ["sports", "football", "fitness", "gym", "athlete"]):
90
  return "رياضة"
 
95
  return "عام"
96
 
97
 
98
+ def get_following_list(username: str, limit: int = 10) -> list[str]:
99
  """
100
+ يستخدم TikTokApi لجلب قائمة أسماء المستخدمين التي يتابعها الحساب،
101
+ وينتهي عند أول `limit` مستخدم.
102
  """
103
+ following_usernames = []
104
+ try:
105
+ with TikTokApi() as api:
106
+ user = api.user(username=username)
107
+ count = 0
108
+ for f in user.following(count=limit):
109
+ following_usernames.append(f.as_dict.get("uniqueId", ""))
110
+ count += 1
111
+ if count >= limit:
112
+ break
113
+ except Exception:
114
+ # إذا فشل TikTokApi لأي سبب (مثل تغيير API)، نعطي قائمة فارغة
115
+ return []
116
+ return following_usernames
117
 
118
 
119
  def lookup_tiktok(username: str):
 
125
  # تصنيف السيرة الذاتية
126
  category = classify_bio(user.bio)
127
 
128
+ # جلب حتى 10 أشخاص يتابعهم المستخدم
129
+ following_list = get_following_list(username, limit=10)
 
 
130
  if following_list:
131
  following_md = "\n".join(f"- @{uid}" for uid in following_list)
132
  else:
133
+ following_md = "لا توجد بيانات متاحة أو تعذَّر جلب القائمة."
134
 
135
+ # إعداد النص النهائي المنسق
136
  info_md = f"""
137
+ **(Name):** {user.name}
 
138
  **البلد (Country):** {user.country}
139
  **اللغة (Language):** {user.language}
140
  **تاريخ الإنشاء (Created):** {user.created}