-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengage2.py
More file actions
45 lines (40 loc) · 1.47 KB
/
engage2.py
File metadata and controls
45 lines (40 loc) · 1.47 KB
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
import requests
from requests_oauthlib import OAuth1
import json
auth = OAuth1(
'C6gVc0T8ZxzKeyyUZAluD8m4F',
'hoWVWcHvie1OE0OVCYos6x7ZJ93aOVGjs8qKKIpvFLf6pEQ1xB',
'2031845931671302144-diQDSbMOszpF5ITmGAFicJ6d7sOsCn',
'D4IeeaBM68Tzl6h1tNshH1cTgpooy18jkuYDlstD7bJqT'
)
r = requests.get(
'https://api.twitter.com/2/tweets/search/recent',
auth=auth,
params={
'query': '(AI agent OR autonomous agent) -is:retweet lang:en',
'max_results': 15,
'tweet.fields': 'author_id,public_metrics',
'expansions': 'author_id',
'user.fields': 'username,public_metrics'
}
)
with open('search_results.json', 'w', encoding='utf-8') as f:
json.dump(r.json(), f, ensure_ascii=False, indent=2)
print("Saved, status:", r.status_code)
data = r.json()
users = {u['id']: u for u in data.get('includes', {}).get('users', [])}
results = []
for t in data.get('data', []):
author = users.get(t['author_id'], {})
m = t.get('public_metrics', {})
results.append({
'id': t['id'],
'username': author.get('username', '?'),
'followers': author.get('public_metrics', {}).get('followers_count', 0),
'likes': m.get('like_count', 0),
'text': t['text']
})
results.sort(key=lambda x: x['likes'], reverse=True)
for item in results[:8]:
print(f"\nID: {item['id']} | @{item['username']} ({item['followers']} followers) | Likes: {item['likes']}")
print(item['text'][:180].encode('ascii', errors='replace').decode())