Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,49 @@
|
|
1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Annotated
|
2 |
+
|
3 |
+
from fastapi import FastAPI, Header
|
4 |
+
|
5 |
+
import html2text
|
6 |
+
import requests
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
|
11 |
+
@app.get("/tiktok_details/")
|
12 |
+
async def read_item(link_detail: str):
|
13 |
+
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"
|
14 |
+
if "https:" in link_detail:
|
15 |
+
url = link_detail
|
16 |
+
elif link_detail[0] == "/":
|
17 |
+
url = "https://tiktok.com" + link_detail
|
18 |
+
else:
|
19 |
+
url = "https://tiktok.com/"+link_detail
|
20 |
+
|
21 |
+
res = requests.get(url, headers={"user-agent":user_agent})
|
22 |
+
text_maker = html2text.HTML2Text()
|
23 |
+
text_maker.ignore_links = True
|
24 |
+
text_maker.ignore_images = True
|
25 |
+
text_maker.bypass_tables = False
|
26 |
+
|
27 |
+
docs = text_maker.handle(res.content.decode("utf-8"))
|
28 |
+
|
29 |
+
content_detail = docs.split("###")[5]
|
30 |
+
|
31 |
+
likes, comments, bookmarks, shares = re.findall(r'\*\*([\w.]+)\*\*', content_detail)
|
32 |
+
|
33 |
+
|
34 |
+
profile = [x.strip() for x in content_detail.split("\n\nSpeed\n\n", 1)[1].split("\n", 6) if x.strip()]
|
35 |
+
username = profile[0]
|
36 |
+
date = profile[1].rsplit(" 路 ", 1)[-1]
|
37 |
+
desc = profile[-1].replace("**", "")
|
38 |
+
|
39 |
+
return {
|
40 |
+
"insights":{
|
41 |
+
"likeCount":likes,
|
42 |
+
"commentCount":comments,
|
43 |
+
"bookmarkCount":bookmarks,
|
44 |
+
"shareCount":shares
|
45 |
+
},
|
46 |
+
"username":username,
|
47 |
+
"date":date,
|
48 |
+
"description":desc
|
49 |
+
}
|