habulaj commited on
Commit
eee505e
verified
1 Parent(s): 9bfbe5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -13
app.py CHANGED
@@ -32,15 +32,19 @@ def greet_json():
32
  # Cookies para a API do TweetHunter
33
  TWEETHUNTER_COOKIES = "_ga_F7Q3BYCL54=GS2.1.s1767919580$o2$g1$t1767919660$j60$l0$h0; _ga=GA1.1.1193193650.1767916115; _fbp=fb.1.1767916115096.41407626306959845; _gid=GA1.2.1486115979.1767916116; _gcl_au=1.1.249737643.1767919602; _ga_BH3EC90N3P=GS2.1.s1767919602$o1$g0$t1767919645$j17$l0$h0; rl_anonymous_id=RS_ENC_v3_IjYxMDgxYWQ0LTQzZWItNDI2MS1iM2NiLWU1Y2Y1OTE4YzdmYyI%3D; rl_page_init_referrer=RS_ENC_v3_IiRkaXJlY3Qi; rl_session=RS_ENC_v3_eyJpZCI6MTc2NzkxOTYwNDUyMSwiZXhwaXJlc0F0IjoxNzY3OTIxNDA0NTIxLCJ0aW1lb3V0IjoxODAwMDAwLCJhdXRvVHJhY2siOnRydWV9; amp_724fdb=5ELVqtiBnGz_OOimEKaKlK...1jeg3gla6.1jeg3gla6.0.0.0; __Host-next-auth.csrf-token=a96665733add52ae5bec69d6cc7ed672a625be6af758e630f11c4238c9bf0dc7%7C803d48ffeb4850440908a7f81faa1eb53093aa583f77d9ae9bcb5f2d7b324b00; __Secure-next-auth.callback-url=https%3A%2F%2Fapp.tweethunter.io; amp_724fdb_tweethunter.io=5ELVqtiBnGz_OOimEKaKlK...1jeg3gla6.1jeg3glo0.0.0.0"
34
 
35
- @app.get("/api/tweet-text/{tweet_id}")
36
- async def get_tweet_full_text(tweet_id: str):
37
  """
38
- Proxy para obter o texto completo de um tweet da API TweetHunter.
 
 
39
  """
40
  try:
41
- url = f"https://tweethunter.io/api/thread?tweetId={tweet_id}"
 
 
42
 
43
- headers = {
44
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0",
45
  "Accept": "application/json",
46
  "Accept-Language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
@@ -51,20 +55,53 @@ async def get_tweet_full_text(tweet_id: str):
51
  }
52
 
53
  async with httpx.AsyncClient() as client:
54
- response = await client.get(url, headers=headers, timeout=10.0)
 
 
 
55
 
56
- if response.status_code != 200:
57
- return JSONResponse(
58
- status_code=response.status_code,
59
- content={"error": f"TweetHunter API returned {response.status_code}"}
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
 
 
64
  except httpx.TimeoutException:
65
- raise HTTPException(status_code=504, detail="Timeout ao conectar com TweetHunter API")
66
  except Exception as e:
67
- raise HTTPException(status_code=500, detail=f"Erro ao buscar texto do tweet: {str(e)}")
68
 
69
  @app.post("/create-post")
70
  async def create_post(request: PostRequest):
 
32
  # Cookies para a API do TweetHunter
33
  TWEETHUNTER_COOKIES = "_ga_F7Q3BYCL54=GS2.1.s1767919580$o2$g1$t1767919660$j60$l0$h0; _ga=GA1.1.1193193650.1767916115; _fbp=fb.1.1767916115096.41407626306959845; _gid=GA1.2.1486115979.1767916116; _gcl_au=1.1.249737643.1767919602; _ga_BH3EC90N3P=GS2.1.s1767919602$o1$g0$t1767919645$j17$l0$h0; rl_anonymous_id=RS_ENC_v3_IjYxMDgxYWQ0LTQzZWItNDI2MS1iM2NiLWU1Y2Y1OTE4YzdmYyI%3D; rl_page_init_referrer=RS_ENC_v3_IiRkaXJlY3Qi; rl_session=RS_ENC_v3_eyJpZCI6MTc2NzkxOTYwNDUyMSwiZXhwaXJlc0F0IjoxNzY3OTIxNDA0NTIxLCJ0aW1lb3V0IjoxODAwMDAwLCJhdXRvVHJhY2siOnRydWV9; amp_724fdb=5ELVqtiBnGz_OOimEKaKlK...1jeg3gla6.1jeg3gla6.0.0.0; __Host-next-auth.csrf-token=a96665733add52ae5bec69d6cc7ed672a625be6af758e630f11c4238c9bf0dc7%7C803d48ffeb4850440908a7f81faa1eb53093aa583f77d9ae9bcb5f2d7b324b00; __Secure-next-auth.callback-url=https%3A%2F%2Fapp.tweethunter.io; amp_724fdb_tweethunter.io=5ELVqtiBnGz_OOimEKaKlK...1jeg3gla6.1jeg3glo0.0.0.0"
34
 
35
+ @app.get("/api/tweet/{tweet_id}")
36
+ async def get_tweet_data(tweet_id: str):
37
  """
38
+ Busca dados completos de um tweet combinando duas APIs:
39
+ - react-tweet.vercel.app: metadados (likes, coment谩rios, verified_type, etc.)
40
+ - tweethunter.io: texto completo do tweet
41
  """
42
  try:
43
+ # URLs das duas APIs
44
+ metadata_url = f"https://react-tweet.vercel.app/api/tweet/{tweet_id}"
45
+ fulltext_url = f"https://tweethunter.io/api/thread?tweetId={tweet_id}"
46
 
47
+ tweethunter_headers = {
48
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0",
49
  "Accept": "application/json",
50
  "Accept-Language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
 
55
  }
56
 
57
  async with httpx.AsyncClient() as client:
58
+ # Fazer as duas requisi莽玫es em paralelo
59
+ import asyncio
60
+ metadata_task = client.get(metadata_url, timeout=10.0)
61
+ fulltext_task = client.get(fulltext_url, headers=tweethunter_headers, timeout=10.0)
62
 
63
+ metadata_response, fulltext_response = await asyncio.gather(
64
+ metadata_task,
65
+ fulltext_task,
66
+ return_exceptions=True
67
+ )
68
+
69
+ # Processar resposta de metadados (obrigat贸ria)
70
+ if isinstance(metadata_response, Exception):
71
+ raise HTTPException(status_code=500, detail=f"Erro ao buscar metadados: {str(metadata_response)}")
72
+
73
+ if metadata_response.status_code != 200:
74
+ raise HTTPException(
75
+ status_code=metadata_response.status_code,
76
+ detail=f"API de metadados retornou {metadata_response.status_code}"
77
  )
78
 
79
+ metadata = metadata_response.json()
80
+
81
+ # Processar resposta de texto completo (opcional - fallback para texto truncado)
82
+ fulltext_html = None
83
+ if not isinstance(fulltext_response, Exception) and fulltext_response.status_code == 200:
84
+ try:
85
+ fulltext_data = fulltext_response.json()
86
+ if isinstance(fulltext_data, list) and len(fulltext_data) > 0:
87
+ fulltext_html = fulltext_data[0].get("textHtml")
88
+ except:
89
+ pass
90
+
91
+ # Combinar os dados
92
+ result = {
93
+ "data": metadata.get("data", {}),
94
+ "fullTextHtml": fulltext_html
95
+ }
96
+
97
+ return result
98
 
99
+ except HTTPException:
100
+ raise
101
  except httpx.TimeoutException:
102
+ raise HTTPException(status_code=504, detail="Timeout ao conectar com APIs")
103
  except Exception as e:
104
+ raise HTTPException(status_code=500, detail=f"Erro ao buscar dados do tweet: {str(e)}")
105
 
106
  @app.post("/create-post")
107
  async def create_post(request: PostRequest):