GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” twitter_openapi_python/twitter_openapi_python at master ยท fa0311/twitter_openapi_python

Expand file tree

/

twitter_openapi_python

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

twitter_openapi_python

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
.vscode
.vscode
 
 
docs/image/README
docs/image/README
 
 
test
test
 
 
twitter_openapi_python
twitter_openapi_python
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
LICENSE.AGPL
LICENSE.AGPL
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 
uv.lock
uv.lock
 
 

README.md

Outline

twitter_openapi_python

Twitter scraping with data validation by pydantic.

1691572735537

Setup

pip install twitter-openapi-python

Usage

import json
import datetime

from twitter_openapi_python import TwitterOpenapiPython

# cookies exported from the browser
with open("cookie.json", "r") as f:
    cookies_dict = json.load(f)
    if isinstance(cookies_dict, list):
        cookies_dict = {k["name"]: k["value"] for k in cookies_dict}

# To extract cookies from Windows (Linux by default)
client = TwitterOpenapiPython()
client.additional_api_headers = {
    "sec-ch-ua-platform": '"Windows"',
}
client.additional_browser_headers = {
    "sec-ch-ua-platform": '"Windows"',
}

# get client from cookies
๐• = client.get_client_from_cookies(cookies=cookies_dict)

# tweet "Hello World!!" with current time
time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
๐•.get_post_api().post_create_tweet(tweet_text=f"Hello World!!{time}")

# get user info
response = ๐•.get_user_api().get_user_by_screen_name("elonmusk")

Login

from twitter_openapi_python import TwitterOpenapiPython

# guest client
๐• = TwitterOpenapiPython().get_guest_client()
tweet = ๐•.get_default_api().get_tweet_result_by_rest_id("1349129669258448897")
assert tweet.data is not None
assert tweet.data.tweet.legacy is not None
print(tweet.data.tweet.legacy.full_text)

Multiple OS

The Token can only be used on the same OS that issued the Token In other words, if the sec-ch-ua-platform does not match, the Token cannot be used. This library uses the Linux Chrome header by default. To use Token issued by Windows, do the following.

client = TwitterOpenapiPython()
client.additional_api_headers = {
  "sec-ch-ua-platform": '"Windows"',
};

Endpoint

The generated client accepts any host that keeps X's path shape (/graphql/{queryId}/{operationName}, /1.1/..., /2/... under https://x.com/i/api). get_client_from_endpoint is for a host that already holds the session: it sends no cookie, no bearer, no browser headers and no transaction id, only the query and the headers you pass (the second argument is added to every request, for whatever the host defines). For example, twitter_api_safe_relay answers at http://localhost:3000/i/api with a logged-in browser session.

from twitter_openapi_python import TwitterOpenapiPython

client = TwitterOpenapiPython().get_client_from_endpoint("http://localhost:3000/i/api")
response = client.get_user_api().get_user_by_screen_name(screen_name="elonmusk")

Development

cd twitter_openapi_python
uv sync
uv run pytest test/api
uv build
uv publish

License

This project is dual licensed. You can choose one of the following licenses:

  • Custom License
  • GNU Affero General Public License v3.0
You are visitor 01402503 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.