jonathanjordan21 commited on
Commit
ab28310
·
verified ·
1 Parent(s): 4f2d693

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py CHANGED
@@ -6,6 +6,7 @@ import html2text
6
  import requests
7
  import httpx
8
  import re
 
9
 
10
  from fastapi.middleware.cors import CORSMiddleware
11
 
@@ -58,6 +59,80 @@ async def linkedin_post_details(post_id: str):
58
  "is_edited": edited,
59
  "insights": {"likeCount": likes, "commentCount": comments, "shareCount": None, "viewCount":None},
60
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
 
63
  @app.get("/google_search")
@@ -86,6 +161,8 @@ async def google_search(q: str, delimiter: str = "\n---\n", sites: Annotated[lis
86
 
87
  @app.get("/tiktok_video_details")
88
  async def tiktok_video_details(username: str, video_id:str):
 
 
89
  url = f"https://www.tiktok.com/{username}/video/{video_id}"
90
  # user_agent = "LinkedInBot"
91
  user_agent = "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
 
6
  import requests
7
  import httpx
8
  import re
9
+ import json
10
 
11
  from fastapi.middleware.cors import CORSMiddleware
12
 
 
59
  "is_edited": edited,
60
  "insights": {"likeCount": likes, "commentCount": comments, "shareCount": None, "viewCount":None},
61
  }
62
+
63
+
64
+ @app.get("/facebook_post_detail")
65
+ async def fb_post_detail(username: str, post_id: str):
66
+ url = f"https://www.facebook.com/{username}/posts/{post_id}"
67
+ user_agent = "Googlebot"
68
+
69
+ res = requests.get(
70
+ url,
71
+ headers={
72
+ "user-agent": user_agent,
73
+ "accept-language": "en-US"
74
+ },
75
+ timeout=(10, 27),
76
+ )
77
+
78
+ soup = BeautifulSoup(res.content, "html.parser")
79
+
80
+ script_tags = soup.find_all("script")
81
+ print(len(script_tags))
82
+ for script_tag in script_tags:
83
+ try:
84
+ if "important_reactors" in script_tag.string:
85
+ splitter = '"reaction_count":{"count":'
86
+ total_react, reaction_split = script_tag.string.split(splitter, 2)[1].split("},", 1)
87
+ total_react = total_react.split(',"')[0]
88
+ pattern = r"\[.*?\]"
89
+
90
+ reactions = re.search(pattern, reaction_split)
91
+
92
+ if reactions:
93
+ reactions = json.loads(reactions.group(0))
94
+ else:
95
+ reactions = []
96
+
97
+ reactions = [
98
+ dict(
99
+ name=reaction["node"]["localized_name"].lower(),
100
+ count=reaction["reaction_count"],
101
+ is_visible=reaction["visible_in_bling_bar"],
102
+ )
103
+ for reaction in reactions
104
+ ]
105
+
106
+ splitter = '"share_count":{"count":'
107
+
108
+ shares = script_tag.string.split(splitter, 2)[1].split(",")[0]
109
+ splitter = '"comments":{"total_count":'
110
+ comments = script_tag.string.split(splitter, 2)[1].split("}")[0]
111
+ likes = [x.get("count") for x in reactions if x.get("name") == "like"][0]
112
+
113
+ print(total_react, reactions, shares, comments, likes)
114
+ if '"message":{"text":"' in script_tag.string:
115
+ desc = script_tag.string.split('"message":{"text":"', 1)[-1].split('"},')[0]
116
+ except Exception as e:
117
+ print(e)
118
+ continue
119
+
120
+ name = soup.find("meta", {"property": "og:title"}).get("content")
121
+
122
+
123
+ return {
124
+ "insights": {
125
+ "likeCount": likes,
126
+ "commentCount": comments,
127
+ "shareCount": shares,
128
+ "reactionCount": total_react,
129
+ "reactions": reactions,
130
+ },
131
+ "description": desc,
132
+ "username": username,
133
+ "name": name,
134
+ "date": None,
135
+ }
136
 
137
 
138
  @app.get("/google_search")
 
161
 
162
  @app.get("/tiktok_video_details")
163
  async def tiktok_video_details(username: str, video_id:str):
164
+ if username[0] != @:
165
+ username = "@" + username
166
  url = f"https://www.tiktok.com/{username}/video/{video_id}"
167
  # user_agent = "LinkedInBot"
168
  user_agent = "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"