Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
13 |
from bs4 import BeautifulSoup
|
14 |
import googleapiclient
|
15 |
import googleapiclient.discovery
|
|
|
16 |
|
17 |
app = FastAPI()
|
18 |
|
@@ -167,11 +168,90 @@ async def ig_post_detail(post_id: Optional[str] = None, url: Optional[str] = Non
|
|
167 |
"description": desc,
|
168 |
"username": username,
|
169 |
"name": full_name,
|
170 |
-
"username": username,
|
171 |
"date": date,
|
172 |
}
|
173 |
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
@app.get("/facebook_post_details")
|
176 |
async def fb_post_detail(username: Optional[str] = None, post_id: Optional[str] = None, url: Optional[str] = None, api_access_key: Optional[str] = None):
|
177 |
if not url:
|
|
|
13 |
from bs4 import BeautifulSoup
|
14 |
import googleapiclient
|
15 |
import googleapiclient.discovery
|
16 |
+
from datetime import datetime
|
17 |
|
18 |
app = FastAPI()
|
19 |
|
|
|
168 |
"description": desc,
|
169 |
"username": username,
|
170 |
"name": full_name,
|
|
|
171 |
"date": date,
|
172 |
}
|
173 |
|
174 |
|
175 |
+
|
176 |
+
@app.get("/instagram_post_details_api")
|
177 |
+
async def ig_post_detail_api(post_id: Optional[str] = None, url: Optional[str] = None):
|
178 |
+
if not post_id:
|
179 |
+
# url = f"https://www.instagram.com/p/{post_id}"
|
180 |
+
post_id = url.split("/")[-1]
|
181 |
+
|
182 |
+
query_hash = "2b0673e0dc4580674a88d426fe00ea90"
|
183 |
+
variables = {
|
184 |
+
"shortcode": post_id
|
185 |
+
}
|
186 |
+
variables_json = json.dumps(variables, separators=(',', ':'))
|
187 |
+
url = f"https://www.instagram.com/graphql/query/?query_hash={query_hash}&variables={variables_json}"
|
188 |
+
res = requests.get(url).json()["data"]["shortcut_media"]
|
189 |
+
|
190 |
+
|
191 |
+
if res.get("edge_media_preview_like"):
|
192 |
+
likes = res.get("edge_media_preview_like").get("count", 0)
|
193 |
+
else:
|
194 |
+
likes = 0
|
195 |
+
|
196 |
+
if res.get("edge_media_to_comment"):
|
197 |
+
comments = res.get("edge_media_to_comment").get("count", 0)
|
198 |
+
else:
|
199 |
+
comments = 0
|
200 |
+
|
201 |
+
if res.get("edge_media_to_caption"):
|
202 |
+
desc = ""
|
203 |
+
for x in res.get("edge_media_to_caption"):
|
204 |
+
if x.get("node"):
|
205 |
+
desc += x.get("node").get("text", "") + "\n\n"
|
206 |
+
desc = desc[:-4]
|
207 |
+
|
208 |
+
username = res["owner"].get("username")
|
209 |
+
full_name = res["owner"].get("full_name")
|
210 |
+
date = str(datetime.fromtimestamp(int(res.get("taken_at_timestamp"))))
|
211 |
+
|
212 |
+
# res = requests.get(
|
213 |
+
# url,
|
214 |
+
# headers={
|
215 |
+
# "user-agent": "Googlebot",
|
216 |
+
# "accept-language": "en-US"
|
217 |
+
# },
|
218 |
+
# timeout=(10, 27),
|
219 |
+
# )
|
220 |
+
|
221 |
+
# soup = BeautifulSoup(res.content, "html.parser")
|
222 |
+
|
223 |
+
# meta = soup.find("meta", {"name": "description"})
|
224 |
+
# content = meta.get("content")
|
225 |
+
# like_split = content.split(" likes, ")
|
226 |
+
# likes = like_split[0]
|
227 |
+
# comment_split = like_split[1].split(" comments - ")
|
228 |
+
# comments = comment_split[0]
|
229 |
+
# author_split = comment_split[1].split(": "")
|
230 |
+
# author_date = author_split[0].split(" on ")
|
231 |
+
# username = author_date[0]
|
232 |
+
# date = author_date[1].split(":")[0]
|
233 |
+
|
234 |
+
# name_desc = (
|
235 |
+
# soup.find("meta", {"property": "og:title"})
|
236 |
+
# .get("content")
|
237 |
+
# .split(" on Instagram: ", 1)
|
238 |
+
# )
|
239 |
+
# full_name = name_desc[0]
|
240 |
+
# desc = name_desc[-1]
|
241 |
+
|
242 |
+
return {
|
243 |
+
"insights": {
|
244 |
+
"likeCount": likes,
|
245 |
+
"commentCount": comments,
|
246 |
+
"shareCount": None,
|
247 |
+
},
|
248 |
+
"description": desc,
|
249 |
+
"username": username,
|
250 |
+
"name": full_name,
|
251 |
+
"date": date,
|
252 |
+
}
|
253 |
+
|
254 |
+
|
255 |
@app.get("/facebook_post_details")
|
256 |
async def fb_post_detail(username: Optional[str] = None, post_id: Optional[str] = None, url: Optional[str] = None, api_access_key: Optional[str] = None):
|
257 |
if not url:
|