twitterviewer 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- twitterviewer-0.1.0/.gitignore +12 -0
- twitterviewer-0.1.0/LICENSE +21 -0
- twitterviewer-0.1.0/PKG-INFO +266 -0
- twitterviewer-0.1.0/README.md +245 -0
- twitterviewer-0.1.0/openapi.json +2298 -0
- twitterviewer-0.1.0/pyproject.toml +58 -0
- twitterviewer-0.1.0/src/twitterviewer/__init__.py +73 -0
- twitterviewer-0.1.0/src/twitterviewer/_base.py +326 -0
- twitterviewer-0.1.0/src/twitterviewer/_client.py +678 -0
- twitterviewer-0.1.0/src/twitterviewer/_version.py +1 -0
- twitterviewer-0.1.0/src/twitterviewer/errors.py +81 -0
- twitterviewer-0.1.0/src/twitterviewer/params.py +13 -0
- twitterviewer-0.1.0/src/twitterviewer/py.typed +0 -0
- twitterviewer-0.1.0/src/twitterviewer/types.py +423 -0
- twitterviewer-0.1.0/tests/__init__.py +0 -0
- twitterviewer-0.1.0/tests/conftest.py +147 -0
- twitterviewer-0.1.0/tests/test_async_client.py +120 -0
- twitterviewer-0.1.0/tests/test_client.py +315 -0
- twitterviewer-0.1.0/tests/test_integration.py +75 -0
- twitterviewer-0.1.0/tests/test_openapi.py +121 -0
- twitterviewer-0.1.0/tests/test_retries.py +212 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TwitterViewer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: twitterviewer
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for the TwitterViewer API: read public X (Twitter) posts, threads, profiles and replies.
|
|
5
|
+
Project-URL: Homepage, https://twitterviewer.ai
|
|
6
|
+
Project-URL: Repository, https://github.com/TwitterViewer/twitterviewer-python
|
|
7
|
+
Project-URL: Issues, https://github.com/TwitterViewer/twitterviewer-python/issues
|
|
8
|
+
Author-email: TwitterViewer <support@twitterviewer.ai>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: ai-agents,api,markdown,twitter,twitterviewer,x
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Requires-Dist: httpx<1,>=0.25
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# TwitterViewer Python SDK
|
|
23
|
+
|
|
24
|
+
Official Python client for the [TwitterViewer](https://twitterviewer.ai) API: read public X (Twitter) posts, threads, profiles and replies as structured data, Markdown, plain text or PDF.
|
|
25
|
+
|
|
26
|
+
TwitterViewer is an independent service, not the official X API. All X access is read-only.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install twitterviewer
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Python 3.9 or newer. The only dependency is [httpx](https://www.python-httpx.org/).
|
|
35
|
+
|
|
36
|
+
## Quickstart
|
|
37
|
+
|
|
38
|
+
The API needs a [Plus](https://twitterviewer.ai/pricing) subscription. Create an API key on the [account page](https://twitterviewer.ai/account/api) and export it:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
export TWITTERVIEWER_API_KEY=tv_live_...
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from twitterviewer import TwitterViewer
|
|
46
|
+
|
|
47
|
+
with TwitterViewer() as tv:
|
|
48
|
+
thread = tv.convert("https://x.com/NASA/status/1234567890")
|
|
49
|
+
print(thread.title)
|
|
50
|
+
print(thread.markdown)
|
|
51
|
+
print(f"{thread.credits.charged} credits used, {thread.credits.remaining} left")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
With asyncio:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import asyncio
|
|
58
|
+
|
|
59
|
+
from twitterviewer import AsyncTwitterViewer
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def main() -> None:
|
|
63
|
+
async with AsyncTwitterViewer() as tv:
|
|
64
|
+
profile = await tv.convert("@NASA", max=10)
|
|
65
|
+
for post in profile.posts:
|
|
66
|
+
print(post.created_at, post.url, post.text)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
asyncio.run(main())
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`AsyncTwitterViewer` has the same methods as `TwitterViewer`.
|
|
73
|
+
|
|
74
|
+
## Configuration
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
tv = TwitterViewer(
|
|
78
|
+
api_key="tv_live_...", # default: TWITTERVIEWER_API_KEY
|
|
79
|
+
base_url="https://twitterviewer.ai", # default: TWITTERVIEWER_BASE_URL, then this
|
|
80
|
+
timeout=90.0, # seconds per attempt, None to disable
|
|
81
|
+
max_retries=2, # retries after the first attempt
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Pass `http_client=httpx.Client(...)` (or `httpx.AsyncClient`) to use a proxy or a custom transport. You close a client you pass in.
|
|
86
|
+
|
|
87
|
+
## Methods
|
|
88
|
+
|
|
89
|
+
| Method | API operation |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `convert()` | `POST /api/v1/convert` |
|
|
92
|
+
| `convert_by_query()` | `GET /api/v1/convert` |
|
|
93
|
+
| `convert_text()` | `POST /api/v1/convert`, document body |
|
|
94
|
+
| `convert_pdf()` | `POST /api/v1/convert`, PDF body |
|
|
95
|
+
| `replies()` | `GET /api/v1/replies` |
|
|
96
|
+
| `credits()` | `GET /api/v1/credits` |
|
|
97
|
+
| `swap_profile()` | `GET /api/v1/swap/{handle}` |
|
|
98
|
+
| `swap_post()` | `GET /api/v1/swap/{handle}/status/{id}` |
|
|
99
|
+
|
|
100
|
+
Responses are frozen dataclasses with snake_case attributes; timestamps are `datetime` objects. Every response also has `rate_limit_remaining`.
|
|
101
|
+
|
|
102
|
+
### convert
|
|
103
|
+
|
|
104
|
+
A post link returns the author's thread by default. A profile link or `@handle` returns recent posts.
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
post = tv.convert("https://x.com/NASA/status/1234567890", scope="post")
|
|
108
|
+
conversation = tv.convert("https://x.com/NASA/status/1234567890", scope="conversation")
|
|
109
|
+
print(len(conversation.replies))
|
|
110
|
+
|
|
111
|
+
profile = tv.convert("@NASA", max=50, replies=True, reposts=False)
|
|
112
|
+
for tweet in profile.posts:
|
|
113
|
+
print(tweet.like_count, tweet.text)
|
|
114
|
+
|
|
115
|
+
note = tv.convert("@NASA", format="obsidian", stats="off")
|
|
116
|
+
print(note.markdown) # Obsidian Markdown
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
| Argument | Values | Applies to |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `scope` | `"thread"` (default), `"post"`, `"conversation"` | post links |
|
|
122
|
+
| `stats` | `"all"` (default), `"root"`, `"off"` | rendered documents |
|
|
123
|
+
| `max` | 1 to 200, default 20 | profile links |
|
|
124
|
+
| `replies`, `reposts` | `bool`, default `False` | profile links |
|
|
125
|
+
|
|
126
|
+
### convert_by_query
|
|
127
|
+
|
|
128
|
+
The same call sent as a `GET` request.
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
result = tv.convert_by_query("@NASA", max=5)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### convert_text
|
|
135
|
+
|
|
136
|
+
Returns only the document.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
doc = tv.convert_text("https://x.com/NASA/status/1234567890", format="markdown")
|
|
140
|
+
print(doc.content, doc.posts_returned)
|
|
141
|
+
|
|
142
|
+
plain = tv.convert_text("@NASA", format="text")
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### convert_pdf
|
|
146
|
+
|
|
147
|
+
A PDF rendered on the server.
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
pdf = tv.convert_pdf("https://x.com/NASA/status/1234567890", paper="letter")
|
|
151
|
+
pdf.save(pdf.filename or "thread.pdf")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### replies
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
result = tv.replies("https://x.com/NASA/status/1234567890", max=40)
|
|
158
|
+
for reply in result.replies:
|
|
159
|
+
print(reply.author.handle, reply.text)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### credits
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
balance = tv.credits()
|
|
166
|
+
print(balance.total, balance.plan.remaining, balance.plan.expires_at, balance.purchased.remaining)
|
|
167
|
+
if balance.unlimited:
|
|
168
|
+
print("unlimited account")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### swap_profile and swap_post
|
|
172
|
+
|
|
173
|
+
The path form of `convert`: the same path as on x.com. Markdown is returned unless you ask for another `format`, and the return type follows it.
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
markdown = tv.swap_profile("NASA", max=10) # TextDocument
|
|
177
|
+
data = tv.swap_post("NASA", "1234567890", format="json") # ConvertResult
|
|
178
|
+
pdf = tv.swap_post("NASA", "1234567890", format="pdf") # PdfDocument
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Errors
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from twitterviewer import APIConnectionError, APIError, ErrorCode
|
|
185
|
+
|
|
186
|
+
try:
|
|
187
|
+
tv.convert("https://x.com/NASA/status/1234567890")
|
|
188
|
+
except APIError as error:
|
|
189
|
+
if error.code == ErrorCode.LIMIT_REACHED:
|
|
190
|
+
print(error.details.get("reason")) # PLUS_REQUIRED or INSUFFICIENT_CREDITS
|
|
191
|
+
elif error.code == ErrorCode.RATE_LIMITED:
|
|
192
|
+
print("retry in", error.retry_after_sec, "seconds")
|
|
193
|
+
else:
|
|
194
|
+
print(error.status, error.code, error.message)
|
|
195
|
+
except APIConnectionError as error:
|
|
196
|
+
print("network problem:", error)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
All exceptions derive from `TwitterViewerError`:
|
|
200
|
+
|
|
201
|
+
| Exception | Raised when |
|
|
202
|
+
| --- | --- |
|
|
203
|
+
| `APIError` | The server returned an error status. Has `status`, `code`, `message`, `retry_after_sec`, `details`. |
|
|
204
|
+
| `APIConnectionError` | No usable response. |
|
|
205
|
+
| `APITimeoutError` | A subclass of `APIConnectionError`: the timeout expired. |
|
|
206
|
+
| `InvalidResponseError` | A successful response could not be parsed. |
|
|
207
|
+
|
|
208
|
+
`ValueError` is raised before any request for arguments that cannot be sent, such as a malformed username in `swap_post`.
|
|
209
|
+
|
|
210
|
+
| `ErrorCode` | Status | Meaning |
|
|
211
|
+
| --- | --- | --- |
|
|
212
|
+
| `INVALID_INPUT` | 400 | Missing or invalid parameter, or not an X link. |
|
|
213
|
+
| `UNAUTHORIZED` | 401 | Missing or unknown API key. |
|
|
214
|
+
| `LIMIT_REACHED` | 402 | No Plus plan (`PLUS_REQUIRED`), or not enough credits (`INSUFFICIENT_CREDITS`). |
|
|
215
|
+
| `PROTECTED` | 403 | The X account is protected. |
|
|
216
|
+
| `NOT_FOUND` | 404 | The post or account does not exist. |
|
|
217
|
+
| `SUSPENDED` | 410 | The X account is suspended. |
|
|
218
|
+
| `RATE_LIMITED` | 429 | Too many requests. |
|
|
219
|
+
| `INTERNAL` | 500 | Unexpected server error. |
|
|
220
|
+
| `UNSUPPORTED` | 501 | No data source can serve the request right now. |
|
|
221
|
+
| `UPSTREAM` | 502 | The X data source or PDF rendering failed. |
|
|
222
|
+
| `SERVICE_UNAVAILABLE` | 503 | Part of the service is unavailable. |
|
|
223
|
+
|
|
224
|
+
`ErrorCode` also defines the remaining codes of the API specification.
|
|
225
|
+
|
|
226
|
+
## Retries
|
|
227
|
+
|
|
228
|
+
Every method follows the same rules. Up to `max_retries` retries (default 2):
|
|
229
|
+
|
|
230
|
+
- Connection failures, timeouts and dropped connections: exponential backoff with jitter.
|
|
231
|
+
- `429` and `5xx`, including gateway error pages without a JSON body and `503` with `details.reason = IN_PROGRESS`: waits `retry_after_sec` when sent, otherwise exponential backoff.
|
|
232
|
+
- `402`, other `4xx` and `501 UNSUPPORTED` are not retried.
|
|
233
|
+
- If the server asks to wait more than 120 seconds, the error is raised immediately.
|
|
234
|
+
|
|
235
|
+
Retrying does not charge twice: see [Credits and rate limits](#credits-and-rate-limits).
|
|
236
|
+
|
|
237
|
+
## Credits and rate limits
|
|
238
|
+
|
|
239
|
+
- **1 credit = 1 post returned.** A quoted post counts as one more post. Author profile data is free.
|
|
240
|
+
- **A repeated request by the same account within 10 minutes returns the first result and is not charged again**, so retries are safe. The same request means:
|
|
241
|
+
- convert: the same link (case-insensitive) with the same `scope`, `max`, `replies` and `reposts`. Changing `format` or `stats` does not matter; PDF is tracked separately.
|
|
242
|
+
- replies: the same post id and `max`.
|
|
243
|
+
- If the first result was partial (limited by the balance) and the account now has credits, the request is treated as new and charged.
|
|
244
|
+
- An identical request sent while the first is still running waits for its result. If it is still running after a minute, the server answers `503` (`IN_PROGRESS`) with a retry delay.
|
|
245
|
+
- Errors and empty results are free.
|
|
246
|
+
- Plus adds 3,000 credits a month.
|
|
247
|
+
- 30 requests a minute per account. Every authenticated request counts, including `credits()` and failed calls.
|
|
248
|
+
|
|
249
|
+
## Development
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
uv sync
|
|
253
|
+
uv run pytest
|
|
254
|
+
uv run ruff check . && uv run ruff format --check . && uv run mypy
|
|
255
|
+
uv build
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Integration tests run only when both variables are set. They spend credits.
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
TWITTERVIEWER_BASE_URL=http://localhost:3400 TWITTERVIEWER_API_KEY=tv_live_... uv run pytest -m integration
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## License
|
|
265
|
+
|
|
266
|
+
MIT
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# TwitterViewer Python SDK
|
|
2
|
+
|
|
3
|
+
Official Python client for the [TwitterViewer](https://twitterviewer.ai) API: read public X (Twitter) posts, threads, profiles and replies as structured data, Markdown, plain text or PDF.
|
|
4
|
+
|
|
5
|
+
TwitterViewer is an independent service, not the official X API. All X access is read-only.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install twitterviewer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Python 3.9 or newer. The only dependency is [httpx](https://www.python-httpx.org/).
|
|
14
|
+
|
|
15
|
+
## Quickstart
|
|
16
|
+
|
|
17
|
+
The API needs a [Plus](https://twitterviewer.ai/pricing) subscription. Create an API key on the [account page](https://twitterviewer.ai/account/api) and export it:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
export TWITTERVIEWER_API_KEY=tv_live_...
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from twitterviewer import TwitterViewer
|
|
25
|
+
|
|
26
|
+
with TwitterViewer() as tv:
|
|
27
|
+
thread = tv.convert("https://x.com/NASA/status/1234567890")
|
|
28
|
+
print(thread.title)
|
|
29
|
+
print(thread.markdown)
|
|
30
|
+
print(f"{thread.credits.charged} credits used, {thread.credits.remaining} left")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
With asyncio:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
import asyncio
|
|
37
|
+
|
|
38
|
+
from twitterviewer import AsyncTwitterViewer
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def main() -> None:
|
|
42
|
+
async with AsyncTwitterViewer() as tv:
|
|
43
|
+
profile = await tv.convert("@NASA", max=10)
|
|
44
|
+
for post in profile.posts:
|
|
45
|
+
print(post.created_at, post.url, post.text)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
asyncio.run(main())
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`AsyncTwitterViewer` has the same methods as `TwitterViewer`.
|
|
52
|
+
|
|
53
|
+
## Configuration
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
tv = TwitterViewer(
|
|
57
|
+
api_key="tv_live_...", # default: TWITTERVIEWER_API_KEY
|
|
58
|
+
base_url="https://twitterviewer.ai", # default: TWITTERVIEWER_BASE_URL, then this
|
|
59
|
+
timeout=90.0, # seconds per attempt, None to disable
|
|
60
|
+
max_retries=2, # retries after the first attempt
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Pass `http_client=httpx.Client(...)` (or `httpx.AsyncClient`) to use a proxy or a custom transport. You close a client you pass in.
|
|
65
|
+
|
|
66
|
+
## Methods
|
|
67
|
+
|
|
68
|
+
| Method | API operation |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `convert()` | `POST /api/v1/convert` |
|
|
71
|
+
| `convert_by_query()` | `GET /api/v1/convert` |
|
|
72
|
+
| `convert_text()` | `POST /api/v1/convert`, document body |
|
|
73
|
+
| `convert_pdf()` | `POST /api/v1/convert`, PDF body |
|
|
74
|
+
| `replies()` | `GET /api/v1/replies` |
|
|
75
|
+
| `credits()` | `GET /api/v1/credits` |
|
|
76
|
+
| `swap_profile()` | `GET /api/v1/swap/{handle}` |
|
|
77
|
+
| `swap_post()` | `GET /api/v1/swap/{handle}/status/{id}` |
|
|
78
|
+
|
|
79
|
+
Responses are frozen dataclasses with snake_case attributes; timestamps are `datetime` objects. Every response also has `rate_limit_remaining`.
|
|
80
|
+
|
|
81
|
+
### convert
|
|
82
|
+
|
|
83
|
+
A post link returns the author's thread by default. A profile link or `@handle` returns recent posts.
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
post = tv.convert("https://x.com/NASA/status/1234567890", scope="post")
|
|
87
|
+
conversation = tv.convert("https://x.com/NASA/status/1234567890", scope="conversation")
|
|
88
|
+
print(len(conversation.replies))
|
|
89
|
+
|
|
90
|
+
profile = tv.convert("@NASA", max=50, replies=True, reposts=False)
|
|
91
|
+
for tweet in profile.posts:
|
|
92
|
+
print(tweet.like_count, tweet.text)
|
|
93
|
+
|
|
94
|
+
note = tv.convert("@NASA", format="obsidian", stats="off")
|
|
95
|
+
print(note.markdown) # Obsidian Markdown
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| Argument | Values | Applies to |
|
|
99
|
+
| --- | --- | --- |
|
|
100
|
+
| `scope` | `"thread"` (default), `"post"`, `"conversation"` | post links |
|
|
101
|
+
| `stats` | `"all"` (default), `"root"`, `"off"` | rendered documents |
|
|
102
|
+
| `max` | 1 to 200, default 20 | profile links |
|
|
103
|
+
| `replies`, `reposts` | `bool`, default `False` | profile links |
|
|
104
|
+
|
|
105
|
+
### convert_by_query
|
|
106
|
+
|
|
107
|
+
The same call sent as a `GET` request.
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
result = tv.convert_by_query("@NASA", max=5)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### convert_text
|
|
114
|
+
|
|
115
|
+
Returns only the document.
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
doc = tv.convert_text("https://x.com/NASA/status/1234567890", format="markdown")
|
|
119
|
+
print(doc.content, doc.posts_returned)
|
|
120
|
+
|
|
121
|
+
plain = tv.convert_text("@NASA", format="text")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### convert_pdf
|
|
125
|
+
|
|
126
|
+
A PDF rendered on the server.
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
pdf = tv.convert_pdf("https://x.com/NASA/status/1234567890", paper="letter")
|
|
130
|
+
pdf.save(pdf.filename or "thread.pdf")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### replies
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
result = tv.replies("https://x.com/NASA/status/1234567890", max=40)
|
|
137
|
+
for reply in result.replies:
|
|
138
|
+
print(reply.author.handle, reply.text)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### credits
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
balance = tv.credits()
|
|
145
|
+
print(balance.total, balance.plan.remaining, balance.plan.expires_at, balance.purchased.remaining)
|
|
146
|
+
if balance.unlimited:
|
|
147
|
+
print("unlimited account")
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### swap_profile and swap_post
|
|
151
|
+
|
|
152
|
+
The path form of `convert`: the same path as on x.com. Markdown is returned unless you ask for another `format`, and the return type follows it.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
markdown = tv.swap_profile("NASA", max=10) # TextDocument
|
|
156
|
+
data = tv.swap_post("NASA", "1234567890", format="json") # ConvertResult
|
|
157
|
+
pdf = tv.swap_post("NASA", "1234567890", format="pdf") # PdfDocument
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Errors
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from twitterviewer import APIConnectionError, APIError, ErrorCode
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
tv.convert("https://x.com/NASA/status/1234567890")
|
|
167
|
+
except APIError as error:
|
|
168
|
+
if error.code == ErrorCode.LIMIT_REACHED:
|
|
169
|
+
print(error.details.get("reason")) # PLUS_REQUIRED or INSUFFICIENT_CREDITS
|
|
170
|
+
elif error.code == ErrorCode.RATE_LIMITED:
|
|
171
|
+
print("retry in", error.retry_after_sec, "seconds")
|
|
172
|
+
else:
|
|
173
|
+
print(error.status, error.code, error.message)
|
|
174
|
+
except APIConnectionError as error:
|
|
175
|
+
print("network problem:", error)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
All exceptions derive from `TwitterViewerError`:
|
|
179
|
+
|
|
180
|
+
| Exception | Raised when |
|
|
181
|
+
| --- | --- |
|
|
182
|
+
| `APIError` | The server returned an error status. Has `status`, `code`, `message`, `retry_after_sec`, `details`. |
|
|
183
|
+
| `APIConnectionError` | No usable response. |
|
|
184
|
+
| `APITimeoutError` | A subclass of `APIConnectionError`: the timeout expired. |
|
|
185
|
+
| `InvalidResponseError` | A successful response could not be parsed. |
|
|
186
|
+
|
|
187
|
+
`ValueError` is raised before any request for arguments that cannot be sent, such as a malformed username in `swap_post`.
|
|
188
|
+
|
|
189
|
+
| `ErrorCode` | Status | Meaning |
|
|
190
|
+
| --- | --- | --- |
|
|
191
|
+
| `INVALID_INPUT` | 400 | Missing or invalid parameter, or not an X link. |
|
|
192
|
+
| `UNAUTHORIZED` | 401 | Missing or unknown API key. |
|
|
193
|
+
| `LIMIT_REACHED` | 402 | No Plus plan (`PLUS_REQUIRED`), or not enough credits (`INSUFFICIENT_CREDITS`). |
|
|
194
|
+
| `PROTECTED` | 403 | The X account is protected. |
|
|
195
|
+
| `NOT_FOUND` | 404 | The post or account does not exist. |
|
|
196
|
+
| `SUSPENDED` | 410 | The X account is suspended. |
|
|
197
|
+
| `RATE_LIMITED` | 429 | Too many requests. |
|
|
198
|
+
| `INTERNAL` | 500 | Unexpected server error. |
|
|
199
|
+
| `UNSUPPORTED` | 501 | No data source can serve the request right now. |
|
|
200
|
+
| `UPSTREAM` | 502 | The X data source or PDF rendering failed. |
|
|
201
|
+
| `SERVICE_UNAVAILABLE` | 503 | Part of the service is unavailable. |
|
|
202
|
+
|
|
203
|
+
`ErrorCode` also defines the remaining codes of the API specification.
|
|
204
|
+
|
|
205
|
+
## Retries
|
|
206
|
+
|
|
207
|
+
Every method follows the same rules. Up to `max_retries` retries (default 2):
|
|
208
|
+
|
|
209
|
+
- Connection failures, timeouts and dropped connections: exponential backoff with jitter.
|
|
210
|
+
- `429` and `5xx`, including gateway error pages without a JSON body and `503` with `details.reason = IN_PROGRESS`: waits `retry_after_sec` when sent, otherwise exponential backoff.
|
|
211
|
+
- `402`, other `4xx` and `501 UNSUPPORTED` are not retried.
|
|
212
|
+
- If the server asks to wait more than 120 seconds, the error is raised immediately.
|
|
213
|
+
|
|
214
|
+
Retrying does not charge twice: see [Credits and rate limits](#credits-and-rate-limits).
|
|
215
|
+
|
|
216
|
+
## Credits and rate limits
|
|
217
|
+
|
|
218
|
+
- **1 credit = 1 post returned.** A quoted post counts as one more post. Author profile data is free.
|
|
219
|
+
- **A repeated request by the same account within 10 minutes returns the first result and is not charged again**, so retries are safe. The same request means:
|
|
220
|
+
- convert: the same link (case-insensitive) with the same `scope`, `max`, `replies` and `reposts`. Changing `format` or `stats` does not matter; PDF is tracked separately.
|
|
221
|
+
- replies: the same post id and `max`.
|
|
222
|
+
- If the first result was partial (limited by the balance) and the account now has credits, the request is treated as new and charged.
|
|
223
|
+
- An identical request sent while the first is still running waits for its result. If it is still running after a minute, the server answers `503` (`IN_PROGRESS`) with a retry delay.
|
|
224
|
+
- Errors and empty results are free.
|
|
225
|
+
- Plus adds 3,000 credits a month.
|
|
226
|
+
- 30 requests a minute per account. Every authenticated request counts, including `credits()` and failed calls.
|
|
227
|
+
|
|
228
|
+
## Development
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
uv sync
|
|
232
|
+
uv run pytest
|
|
233
|
+
uv run ruff check . && uv run ruff format --check . && uv run mypy
|
|
234
|
+
uv build
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Integration tests run only when both variables are set. They spend credits.
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
TWITTERVIEWER_BASE_URL=http://localhost:3400 TWITTERVIEWER_API_KEY=tv_live_... uv run pytest -m integration
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
MIT
|