|
| 1 | +# Prefect tasks for Twitter search and X API workflows |
| 2 | + |
| 3 | +TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp. |
| 4 | + |
| 5 | +Schedule 6 read-only Twitter search, profile, timeline, and trend tasks in Prefect 3. Store API keys in Prefect blocks. |
| 6 | + |
| 7 | +## Tasks |
| 8 | + |
| 9 | +| Workflow step | Prefect task | TwexAPI route | |
| 10 | +| --- | --- | --- | |
| 11 | +| Search tweets | `search_tweets` | `POST /twitter/advanced_search/page` | |
| 12 | +| Read tweet detail | `get_tweet` | `POST /v2/tweet/detail` | |
| 13 | +| Search users | `search_users` | `GET /twitter/search-user/{keyword}/{target_count}` | |
| 14 | +| Read a profile | `get_user` | `GET /twitter/{screen_name}/about` | |
| 15 | +| Read a timeline page | `get_user_tweets` | `POST /twitter/{screen_name}/timeline/page` | |
| 16 | +| Read trending tweets | `get_trends` | `GET /twitter/global-trending/tweets` | |
| 17 | + |
| 18 | +Use REST or an SDK for follower pagination or approved posting. |
| 19 | + |
| 20 | +## Install |
| 21 | + |
| 22 | +```bash |
| 23 | +pip install prefect-x-api-scraper |
| 24 | +``` |
| 25 | + |
| 26 | +## Store credentials |
| 27 | + |
| 28 | +```bash |
| 29 | +prefect block register -m prefect_x_api_scraper |
| 30 | +``` |
| 31 | + |
| 32 | +```python |
| 33 | +from prefect_x_api_scraper import TwexApiCredentials |
| 34 | + |
| 35 | +credentials = TwexApiCredentials(api_key="your_twexapi_key") |
| 36 | +credentials.save("twexapi", overwrite=True) |
| 37 | +``` |
| 38 | + |
| 39 | +Keep API keys in Prefect blocks. Never put them in flow source files. |
| 40 | + |
| 41 | +## Create a flow |
| 42 | + |
| 43 | +```python |
| 44 | +from prefect import flow |
| 45 | +from prefect_x_api_scraper import TwexApiCredentials, get_trends, search_tweets |
| 46 | + |
| 47 | + |
| 48 | +@flow |
| 49 | +async def twitter_signal_flow() -> dict: |
| 50 | + credentials = TwexApiCredentials.load("twexapi") |
| 51 | + |
| 52 | + tweets = await search_tweets( |
| 53 | + credentials, |
| 54 | + '"prefect" OR "workflow orchestration"', |
| 55 | + sort_by="Latest", |
| 56 | + ) |
| 57 | + trends = await get_trends(credentials, country="United States", count=10) |
| 58 | + |
| 59 | + return {"tweets": tweets, "trends": trends} |
| 60 | +``` |
| 61 | + |
| 62 | +## Import tasks |
| 63 | + |
| 64 | +```python |
| 65 | +from prefect_x_api_scraper import ( |
| 66 | + get_trends, |
| 67 | + get_tweet, |
| 68 | + get_user, |
| 69 | + get_user_tweets, |
| 70 | + search_tweets, |
| 71 | + search_users, |
| 72 | +) |
| 73 | +``` |
| 74 | + |
| 75 | +Tasks return the raw TwexAPI JSON payload and support Prefect `with_options`: |
| 76 | + |
| 77 | +```python |
| 78 | +from prefect_x_api_scraper import search_tweets |
| 79 | + |
| 80 | +search_recent_tweets = search_tweets.with_options( |
| 81 | + name="Search recent tweets", |
| 82 | + retries=2, |
| 83 | + retry_delay_seconds=10, |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +The credentials block sends `Authorization: Bearer`. |
| 88 | + |
| 89 | +## Documentation |
| 90 | + |
| 91 | +- [TwexAPI docs](https://docs.twexapi.io) |
| 92 | +- [REST overview](https://docs.twexapi.io/api-reference/overview) |
| 93 | +- [Prefect tasks](https://docs.prefect.io/v3/develop/write-tasks) |
| 94 | + |
| 95 | +TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp. |
0 commit comments