-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_scraper_agent.py
80 lines (67 loc) · 2.46 KB
/
twitter_scraper_agent.py
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
#!/usr/bin/env python3
"""
Script for a Twitter/X trends agent using BrowserTool with TaskPlanner.
This script creates a Twitter/X agent with the BrowserTool and GoogleSearchTool
and uses a TaskPlanner to automatically generate and execute a task plan based on the objective.
It demonstrates how to set up an agent to collect information about trending content on Twitter/X.
"""
import asyncio
import os
import json
from pathlib import Path
from nodetool.agents.agent import Agent
from nodetool.chat.providers import get_provider
from nodetool.agents.tools.browser import BrowserTool, GoogleSearchTool
from nodetool.metadata.types import Provider, Task
from nodetool.workflows.processing_context import ProcessingContext
from nodetool.workflows.types import Chunk
async def main():
context = ProcessingContext()
# 2. Initialize provider and model
provider = get_provider(Provider.OpenAI)
model = "gpt-4o"
# Alternatively, you can use Anthropic:
# provider = get_provider(Provider.Anthropic)
# model = "claude-3-7-sonnet-20250219"
# 3. Set up browser and search tools
tools = [
BrowserTool(context.workspace_dir),
GoogleSearchTool(context.workspace_dir),
]
# 4. Create Twitter/X trends collector agent
trends_agent = Agent(
name="Twitter/X Trends Collector",
objective="""
Identify viral accounts by browsing https://x.com/explore/tabs/trending.
Collect the top 10 trending topics and analyze tweets.
""",
provider=provider,
model=model,
tools=tools,
output_type="json",
output_schema={
"type": "object",
"properties": {
"trends": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {"type": "string"},
"url": {"type": "string"},
},
},
},
},
},
)
async for item in trends_agent.execute(context):
if isinstance(item, Chunk):
print(item.content, end="", flush=True)
# 7. Print result summary
print("\n\nTask execution completed.")
trends_results = trends_agent.get_results()
print(f"\nTrends: {json.dumps(trends_results, indent=2)}")
print(f"\nWorkspace: {context.workspace_dir}")
if __name__ == "__main__":
asyncio.run(main())