jonathanjordan21 commited on
Commit
385e9be
·
verified ·
1 Parent(s): 1ecee5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -41
app.py CHANGED
@@ -55,7 +55,7 @@ async def linkedin_post_details(post_id: str):
55
  "content": content,
56
  "date": date,
57
  "is_edited": edited,
58
- "insights": {"likeCount": likes, "commentCount": comments, "shareCount": None},
59
  }
60
 
61
 
@@ -84,51 +84,56 @@ async def google_search(q: str, delimiter: str = "\n---\n", sites: Annotated[lis
84
 
85
 
86
  @app.get("/tiktok_video_details")
87
- async def read_item(username: str, video_id:str):
88
- user_agent = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36"
89
- # user_agent = "Googlebot/2.1"
90
- # if "https:" in link_detail:
91
- # url = link_detail
92
- # elif link_detail[0] == "/":
93
- # url = "https://tiktok.com" + link_detail
94
- # else:
95
- # url = "https://tiktok.com/"+link_detail
96
-
97
- url = f"https://www.tiktok.com/@{username}/video/{video_id}"
98
-
99
- with httpx.Client() as client:
100
- res = client.get(url, headers={"User-Agent":user_agent})
101
-
102
- # res = requests.get(url, headers={"user-agent":user_agent})
103
- text_maker = html2text.HTML2Text()
104
- text_maker.ignore_links = True
105
- text_maker.ignore_images = True
106
- text_maker.bypass_tables = False
107
 
108
- print("RESPONSE DETAIlL", res.content.decode("utf-8"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
- docs = text_maker.handle(res.content.decode("utf-8"))
111
 
112
- print("DOCS", docs)
113
 
114
- content_detail = docs.split("###")[5]
115
 
116
- likes, comments, bookmarks, shares = re.findall(r'\*\*([\w.]+)\*\*', content_detail)
117
 
118
 
119
- profile = [x.strip() for x in content_detail.split("\n\nSpeed\n\n", 1)[1].split("\n", 6) if x.strip()]
120
- username = profile[0]
121
- date = profile[1].rsplit(" · ", 1)[-1]
122
- desc = profile[-1].replace("**", "")
123
 
124
- return {
125
- "insights":{
126
- "likeCount":likes,
127
- "commentCount":comments,
128
- "bookmarkCount":bookmarks,
129
- "shareCount":shares
130
- },
131
- "username":username,
132
- "date":date,
133
- "description":desc
134
- }
 
55
  "content": content,
56
  "date": date,
57
  "is_edited": edited,
58
+ "insights": {"likeCount": likes, "commentCount": comments, "shareCount": None, "viewCount":None},
59
  }
60
 
61
 
 
84
 
85
 
86
  @app.get("/tiktok_video_details")
87
+ async def tiktok_video_details(username: str, video_id:str):
88
+ url = f"https://www.tiktok.com/{username}/video/{video_id}"
89
+ user_agent = "LinkedInBot/1.0 (compatible; Mozilla/5.0; Jakarta Commons-HttpClient/3.1 +http://www.linkedin.com)/1.0 (LinkedInBot; https://www.linkedin.com/; [email protected])"
90
+ res = requests.get(url, headers={"user-agent": user_agent})
91
+
92
+ soup = BeautifulSoup(res.content, "html.parser")
93
+
94
+ insights = soup.find("meta", {"property": "og:description"}).get("content")
95
+ likes = insights.split(" ", 1)[0]
96
+ desc = insights.rsplit(" comments. “", 1)[-1][:-1]
97
+ comments = insights.split(", ", 1)[-1].split(" ", 1)[0]
 
 
 
 
 
 
 
 
 
98
 
99
+ name = soup.find("meta", {"property": "og:title"}).get("content")[9:]
100
+
101
+ content = {
102
+ "insights": {"likeCount": likes, "commentCount": comments, "shareCount":None, "viewCount":None},
103
+ "description": desc,
104
+ "username": username,
105
+ "name": name,
106
+ }
107
+
108
+ # text_maker = html2text.HTML2Text()
109
+ # text_maker.ignore_links = True
110
+ # text_maker.ignore_images = True
111
+ # text_maker.bypass_tables = False
112
+
113
+ # print("RESPONSE DETAIlL", res.content.decode("utf-8"))
114
 
115
+ # docs = text_maker.handle(res.content.decode("utf-8"))
116
 
117
+ # print("DOCS", docs)
118
 
119
+ # content_detail = docs.split("###")[5]
120
 
121
+ # likes, comments, bookmarks, shares = re.findall(r'\*\*([\w.]+)\*\*', content_detail)
122
 
123
 
124
+ # profile = [x.strip() for x in content_detail.split("\n\nSpeed\n\n", 1)[1].split("\n", 6) if x.strip()]
125
+ # username = profile[0]
126
+ # date = profile[1].rsplit(" · ", 1)[-1]
127
+ # desc = profile[-1].replace("**", "")
128
 
129
+ # return {
130
+ # "insights":{
131
+ # "likeCount":likes,
132
+ # "commentCount":comments,
133
+ # "bookmarkCount":bookmarks,
134
+ # "shareCount":shares
135
+ # },
136
+ # "username":username,
137
+ # "date":date,
138
+ # "description":desc
139
+ # }