Spaces:
Sleeping
Sleeping
File size: 16,595 Bytes
e3bbf73 673e694 de04e67 673e694 ee2ef41 13e03cb ab28310 c9775c6 1fe8b82 559f4a6 346a29c df94e38 c347662 559f4a6 1fe8b82 559f4a6 3ef97e3 ccd1a73 c9775c6 7a4ba1a c9775c6 1ecee5c 8951fb1 ae0b5d8 43bcca3 1ecee5c 2e4e6d4 5f5602d 2e4e6d4 5f5602d 2e4e6d4 432378a 7d68b57 5f5602d 67359d5 5f5602d 67359d5 5f5602d 7d68b57 1b686c4 1ecee5c 2e4e6d4 538543b 5f5602d 538543b 2e4e6d4 1ecee5c 2e4e6d4 ab28310 f20de99 d76a753 f20de99 371382c f20de99 c347662 3ff42f0 c347662 f20de99 3d17e73 ae0b5d8 f48a877 ab28310 5ea408d 7f4d638 ab28310 3d17e73 ac26503 74bdd56 ac26503 afade17 c494fec afade17 c494fec 3d17e73 caff860 ab28310 7f4d638 ab28310 3d17e73 ab28310 1ecee5c 673e694 346a29c 5d14fc1 6acd1bf 7effc07 51cbaf5 533417a a270de9 346a29c 633923e 346a29c 5d14fc1 47bf472 ae39fb8 5d14fc1 ae39fb8 5d14fc1 47bf472 5d14fc1 1ecee5c 8951fb1 ae0b5d8 f48a877 ae0b5d8 ceb1554 ae0b5d8 385e9be cce7a99 385e9be cce7a99 b42350d cce7a99 385e9be cce7a99 385e9be cce7a99 385e9be cce7a99 673e694 cce7a99 673e694 cce7a99 d4da168 cce7a99 673e694 cce7a99 673e694 cce7a99 4f2d693 673e694 cce7a99 3ef97e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
from typing import Annotated, Optional
from fastapi import FastAPI, Header, Query
import html2text
import requests
import httpx
import re
import json
import newspaper
from fastapi.middleware.cors import CORSMiddleware
from bs4 import BeautifulSoup
import googleapiclient
import googleapiclient.discovery
from datetime import datetime
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/news_details")
def news_details(url: str):
article = newspaper.Article(url)
article.download()
article.parse()
return {
"title": article.title,
"description": article.text,
"author": article.authors,
"date": article.publish_date,
}
@app.get("/linkedin_post_details")
async def linkedin_post_details(post_id: Optional[str] = None, url: Optional[str] = None):
if not url:
url = "https://www.linkedin.com/posts/"+post_id
res = requests.get(url, headers={"user-agent":"Googlebot", "accept-language": "en-US"})
soup = BeautifulSoup(res.content, "html.parser")
script_tags = soup.find_all("script")
for script_tag in script_tags:
try:
script_tag = json.loads(script_tag.string)
if script_tag.get("datePublished"):
desc = script_tag.get("articleBody")
if not desc:
desc = script_tag.get("description")
author = script_tag.get("author")
full_name = author.get("name")
username = author.get("url").rsplit("/", 1)[-1]
user_type = author.get("@type").lower()
date = script_tag.get("datePublished")
except Exception as e:
continue
spans = soup.find_all(
"span", {"data-test-id": "social-actions__reaction-count"}
)
if spans:
reactions = spans[0].text.strip()
else:
reactions = '0'
try:
comments = str(soup.find("a", {"data-test-id": "social-actions__comments"}).get(
"data-num-comments"
))
except:
comments = '0'
return {
"insights": {
"likeCount": None,
# "commentCount": int(comments.replace(",", "")),
"commentCount": comments,
"shareCount": None,
# "reactionCount": int(reactions.replace(",", "")),
"reactionCount":reactions,
"reactions": [],
},
"description": desc,
"username": username,
"name": full_name,
"userType": user_type,
"date": date,
}
# async def linkedin_post_details(post_id: str):
# url = "https://www.linkedin.com/posts/"+post_id
# res = requests.get(url, headers={"user-agent":"Googlebot", "accept-language": "en-US"})
# text_maker = html2text.HTML2Text()
# text_maker.ignore_links = True
# text_maker.ignore_images = True
# text_maker.bypass_tables = False
# docs = text_maker.handle(res.content.decode("utf-8"))
# chunks = docs.split("\n\n#")
# linkedin_content = chunks[1]
# user = linkedin_content.split("\n\n", 5)
# full_name = user[1]
# bio = user[2]
# try:
# date, edited = user[3].split(" ")
# edited = True
# except:
# date = user[3].strip()
# edited = False
# content = "\n\n".join(user[5:])
# insights = chunks[3].split("\n\n")[2]
# likes = insights.split(" ", 1)[0].strip()
# comments = insights.rsplit(" ", 2)[1].strip()
# username = url.rsplit("/",1)[-1].split("_")[0]
# return {
# "userDetails": {"full_name": full_name, "username":username,"bio": bio},
# "content": content,
# "date": date,
# "is_edited": edited,
# "insights": {"likeCount": likes, "commentCount": comments, "shareCount": None, "viewCount":None},
# "username":username
# }
@app.get("/instagram_post_details")
async def ig_post_detail(post_id: Optional[str] = None, url: Optional[str] = None):
if not url:
url = f"https://www.instagram.com/p/{post_id}"
res = requests.get(
url,
headers={
"user-agent": "Googlebot",
"accept-language": "en-US"
},
timeout=(10, 27),
)
soup = BeautifulSoup(res.content, "html.parser")
meta = soup.find("meta", {"name": "description"})
content = meta.get("content")
like_split = content.split(" likes, ")
likes = like_split[0]
comment_split = like_split[1].split(" comments - ")
comments = comment_split[0]
author_split = comment_split[1].split(": "")
author_date = author_split[0].split(" on ")
username = author_date[0]
date = author_date[1].split(":")[0]
name_desc = (
soup.find("meta", {"property": "og:title"})
.get("content")
.split(" on Instagram: ", 1)
)
full_name = name_desc[0]
desc = name_desc[-1]
return {
"insights": {
"likeCount": likes,
"commentCount": comments,
"shareCount": None,
},
"description": desc,
"username": username,
"name": full_name,
"date": date,
}
@app.get("/instagram_post_details_api")
async def ig_post_detail_api(post_id: Optional[str] = None, url: Optional[str] = None):
if not post_id:
# url = f"https://www.instagram.com/p/{post_id}"
post_id = url.split("/")[-1]
query_hash = "2b0673e0dc4580674a88d426fe00ea90"
variables = {
"shortcode": post_id
}
variables_json = json.dumps(variables, separators=(',', ':'))
url = f"https://www.instagram.com/graphql/query/?query_hash={query_hash}&variables={variables_json}"
res = requests.get(url, headers={"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"}).json()
print(res)
res = res["data"]["shortcut_media"]
if res.get("edge_media_preview_like"):
likes = res.get("edge_media_preview_like").get("count", 0)
else:
likes = 0
if res.get("edge_media_to_comment"):
comments = res.get("edge_media_to_comment").get("count", 0)
else:
comments = 0
if res.get("edge_media_to_caption"):
desc = ""
for x in res.get("edge_media_to_caption"):
if x.get("node"):
desc += x.get("node").get("text", "") + "\n\n"
desc = desc[:-4]
username = res["owner"].get("username")
full_name = res["owner"].get("full_name")
date = str(datetime.fromtimestamp(int(res.get("taken_at_timestamp"))))
# res = requests.get(
# url,
# headers={
# "user-agent": "Googlebot",
# "accept-language": "en-US"
# },
# timeout=(10, 27),
# )
# soup = BeautifulSoup(res.content, "html.parser")
# meta = soup.find("meta", {"name": "description"})
# content = meta.get("content")
# like_split = content.split(" likes, ")
# likes = like_split[0]
# comment_split = like_split[1].split(" comments - ")
# comments = comment_split[0]
# author_split = comment_split[1].split(": "")
# author_date = author_split[0].split(" on ")
# username = author_date[0]
# date = author_date[1].split(":")[0]
# name_desc = (
# soup.find("meta", {"property": "og:title"})
# .get("content")
# .split(" on Instagram: ", 1)
# )
# full_name = name_desc[0]
# desc = name_desc[-1]
return {
"insights": {
"likeCount": likes,
"commentCount": comments,
"shareCount": None,
},
"description": desc,
"username": username,
"name": full_name,
"date": date,
}
@app.get("/facebook_post_details")
async def fb_post_detail(username: Optional[str] = None, post_id: Optional[str] = None, url: Optional[str] = None, api_access_key: Optional[str] = None):
if not url:
url = f"https://www.facebook.com/{username}/posts/{post_id}"
else:
username = url.split("//www.facebook.com/",1)[-1].split("/",1)[0]
user_agent = "Googlebot"
res = requests.get(
url,
headers={
"user-agent": user_agent,
"accept-language": "en-US"
},
timeout=(10, 27),
)
soup = BeautifulSoup(res.content, "html.parser")
script_tags = soup.find_all("script")
print(len(script_tags))
for script_tag in script_tags:
try:
if "important_reactors" in script_tag.string:
splitter = '"reaction_count":{"count":'
total_react, reaction_split = script_tag.string.split(splitter, 2)[1].split("},", 1)
total_react = total_react.split(',"')[0]
pattern = r"\[.*?\]"
reactions = re.search(pattern, reaction_split)
if reactions:
reactions = json.loads(reactions.group(0))
else:
reactions = []
reactions = [
dict(
name=reaction["node"]["localized_name"].lower(),
count=reaction["reaction_count"],
is_visible=reaction["visible_in_bling_bar"],
)
for reaction in reactions
]
splitter = '"share_count":{"count":'
shares = script_tag.string.split(splitter, 2)[1].split(",")[0]
splitter = '"comments":{"total_count":'
comments = script_tag.string.split(splitter, 2)[1].split("}")[0]
likes = [x.get("count") for x in reactions if x.get("name") == "like"]
likes = likes[0] if likes else 0
print(total_react, reactions, shares, comments, likes)
if '"message":{"text":"' in script_tag.string:
desc = script_tag.string.split('"message":{"text":"', 1)[-1].split('"},')[0]
except Exception as e:
print(e)
continue
name = soup.find("meta", {"property": "og:title"}).get("content")
date = None
if api_access_key:
if not post_id:
post_id = url.split("/")[-1]
try:
post_details = requests.get(
f"https://graph.facebook.com/v20.0/1066512588151225_{post_id}?fields=place,shares,targeting,updated_time,created_time,description,child_attachments,caption,event,message,message_tags,story,status_type,source,coordinates,backdated_time,story_tags,scheduled_publish_time,properties,attachments&access_token={api_access_key}",
headers={"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",}
).json()
if post_details.get("updated_time"):
date = post_details.get("updated_time")
else:
date = post_details.get("created_time")
except Exception as e:
print(e)
else:
post_details = None
return {
"insights": {
"likeCount": likes,
"commentCount": int(comments),
"shareCount": int(shares),
"reactionCount": int(total_react),
"reactions": reactions,
},
"description": desc,
"username": username,
"name": name,
"date": date,
"details":post_details
}
@app.get("/google_search")
async def google_search(q: str, delimiter: str = "\n---\n", sites: Annotated[list[str] | None, Query()] = None):
print(sites)
print(type(sites))
url = f"https://www.google.com/search?q={q} "
if sites:
url += " OR ".join(["site:"+site for site in sites])
texts = ""
soup = BeautifulSoup(requests.get(url).content, "html.parser")
for div in soup.find_all("div")[24:]:
if len(div.find_parents("div")) == 8: # Depth 4 means 3 parent divs (0-indexed)
# print(div.get_text().strip())
href = div.find(href=True, recursive=True)
text = div.find(text=True, recursive=False)
if href and text:
print(text)
text = f'[{text}]({href["href"].split("/url?q=")[-1]})'
if text != None and text.strip():
texts += text + delimiter
return {"results":texts}
@app.get("/google_search_urls")
async def google_search_url(q: str, sites: Annotated[list[str] | None, Query()] = None, start:int = 0, user_agent="Twitterbot"):
url = f"https://www.google.com/search?start={start}&q={q} "
if sites:
url += " OR ".join(["site:"+site for site in sites])
res = requests.get(
url,
headers={
"user-agent": user_agent,
"accept-language": "en-US"
},
timeout=(10, 27),
)
soup = BeautifulSoup(res.content, "html.parser")
prefix = "/url?q=h"
len_prefix = len(prefix)
docs = []
for div in soup.find_all(True):
if len(div.find_parents()) == 2: # Depth 4 means 3 parent divs (0-indexed)
a_tags = div.find_all("a")
for a in a_tags:
doc = a.get("href")
if (
doc[:len_prefix] == prefix
and "google.com" not in doc[len_prefix - 1 :]
):
docs.append(
doc[len_prefix - 1 :]
.split("&")[0]
.replace("%3F", "?")
.replace("%3D", "=")
)
return {"results":docs}
@app.get("/tiktok_video_details")
async def tiktok_video_details(username: Optional[str] = None, video_id:Optional[str] = None, url: Optional[str] = None):
if not url:
if username[0] != "@":
username = "@" + username
url = f"https://www.tiktok.com/{username}/video/{video_id}"
else:
username = url.split("//www.tiktok.com/",1)[-1].split("/")[0]
# user_agent = "LinkedInBot"
user_agent = "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)"
res = requests.get(url, headers={"user-agent": user_agent})
# soup = BeautifulSoup(res.content, "html.parser")
# insights = soup.find("meta", {"property": "og:description"}).get("content")
# likes = insights.split(" ", 1)[0]
# desc = insights.rsplit(" comments. “", 1)[-1][:-1]
# comments = insights.split(", ", 1)[-1].split(" ", 1)[0]
# name = soup.find("meta", {"property": "og:title"}).get("content")[9:]
# return {
# "insights": {"likeCount": likes, "commentCount": comments, "shareCount":None, "viewCount":None},
# "description": desc,
# "username": username,
# "name": name,
# }
text_maker = html2text.HTML2Text()
text_maker.ignore_links = True
text_maker.ignore_images = True
text_maker.bypass_tables = False
print("RESPONSE DETAIlL", res.content.decode("utf-8"))
docs = text_maker.handle(res.content.decode("utf-8"))
print("DOCS", docs)
content_detail = docs.split("###")[5]
likes, comments, bookmarks, shares = re.findall(r'\*\*([\w.]+)\*\*', content_detail)
profile = [x.strip() for x in content_detail.split("\n\nSpeed\n\n", 1)[1].split("\n", 6) if x.strip()]
username = profile[0]
date = profile[1].rsplit(" · ", 1)[-1]
desc = profile[-1][2:].replace("**", "")
return {
"insights":{
"likeCount":likes,
"commentCount":comments,
"bookmarkCount":bookmarks,
"shareCount":shares
},
"username":username,
"date":date,
"description":desc
}
@app.get("/youtube_video_details")
async def yt_vid_detail(api_key:str, video_id: Optional[str] = None, url: Optional[str] = None):
# yt_ids = [doc.split("?v=")[-1] for doc in docs]
if url:
video_id = url.split("?v=")[-1]
youtube = googleapiclient.discovery.build(
"youtube", "v3", developerKey=api_key
)
# request = youtube.search().list(part="snippet", q="sari roti", type="video")
request = youtube.videos().list(
part="snippet,statistics,topicDetails",
# id=",".join(yt_ids),
id = video_id,
)
return request.execute()["items"]
|