|
| 1 | +# Twitter search and user timeline components for Haystack |
| 2 | + |
| 3 | +TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp. |
| 4 | + |
| 5 | +Search Twitter and fetch public user timelines in Haystack RAG pipelines. Each TwexAPI result becomes a Haystack `Document`. |
| 6 | + |
| 7 | +## Components |
| 8 | + |
| 9 | +| Task | Haystack component | TwexAPI route | Output | |
| 10 | +| --- | --- | --- | --- | |
| 11 | +| Search tweets for RAG | `TwexApiTweetSearch` | `POST /twitter/advanced_search/page` | Matching posts as `Document` objects | |
| 12 | +| Retrieve a user's timeline | `TwexApiUserTweetsFetcher` | `POST /twitter/{screen_name}/timeline/page` | Recent user posts as `Document` objects | |
| 13 | + |
| 14 | +```python |
| 15 | +from haystack_integrations.components.websearch.x_api_scraper import ( |
| 16 | + TwexApiTweetSearch, |
| 17 | + TwexApiUserTweetsFetcher, |
| 18 | +) |
| 19 | +``` |
| 20 | + |
| 21 | +Both components read `X_API_SCRAPER_KEY` by default and accept a Haystack `Secret`. They send `Authorization: Bearer`. Set `base_url` for another TwexAPI-compatible endpoint. Results include `documents`, `links`, `has_more`, and `next_cursor`. |
| 22 | + |
| 23 | +Search returns up to 20 tweets per page. Timeline `top_k` maps to the page `count` (1-100). Use REST or an SDK for follower pagination or approved posting. |
| 24 | + |
| 25 | +## Install |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install x-api-scraper-haystack |
| 29 | +``` |
| 30 | + |
| 31 | +## Build a Haystack RAG pipeline with Twitter search |
| 32 | + |
| 33 | +### Search posts |
| 34 | + |
| 35 | +```python |
| 36 | +from haystack import Pipeline |
| 37 | +from haystack.utils import Secret |
| 38 | +from haystack_integrations.components.websearch.x_api_scraper import TwexApiTweetSearch |
| 39 | + |
| 40 | +search = TwexApiTweetSearch(api_key=Secret.from_env_var("X_API_SCRAPER_KEY"), top_k=10) |
| 41 | + |
| 42 | +pipeline = Pipeline() |
| 43 | +pipeline.add_component("x_search", search) |
| 44 | + |
| 45 | +result = pipeline.run({"x_search": {"query": "haystack ai"}}) |
| 46 | +documents = result["x_search"]["documents"] |
| 47 | +``` |
| 48 | + |
| 49 | +### Fetch user posts |
| 50 | + |
| 51 | +```python |
| 52 | +from haystack.utils import Secret |
| 53 | +from haystack_integrations.components.websearch.x_api_scraper import ( |
| 54 | + TwexApiUserTweetsFetcher, |
| 55 | +) |
| 56 | + |
| 57 | +fetcher = TwexApiUserTweetsFetcher(api_key=Secret.from_env_var("X_API_SCRAPER_KEY")) |
| 58 | + |
| 59 | +result = fetcher.run(screen_name="elonmusk") |
| 60 | +documents = result["documents"] |
| 61 | +``` |
| 62 | + |
| 63 | +## Document mapping |
| 64 | + |
| 65 | +Each tweet becomes a Haystack `Document` with this mapping. |
| 66 | + |
| 67 | +- `Document.content`: `full_text` or `text`, or an empty string when both are missing |
| 68 | +- `Document.meta["endpoint"]`: `search` or `timeline` |
| 69 | +- `Document.meta`: available `id`, `url`, and `created_at` source values |
| 70 | +- `Document.meta["author"]`: available author identity and verification data |
| 71 | +- `Document.meta`: available like, retweet, reply, quote, view, and bookmark counts |
| 72 | + |
| 73 | +## License |
| 74 | + |
| 75 | +MIT. TwexAPI is an independent third-party service. Not affiliated with X Corp. "Twitter" and "X" are trademarks of X Corp. |
0 commit comments