Skip to main content

Twitter OpenAPI

Project description

twitter_openapi_python

Twitter scraping with data validation by pydantic.

1691572735537

Setup

pip install twitter-openapi-python

Usage

import json
import datetime

from pathlib import Path
from tweepy_authlib import CookieSessionUserHandler
from twitter_openapi_python import TwitterOpenapiPython

# login by tweepy_authlib
if Path("cookie.json").exists():
    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}
else:
    auth_handler = CookieSessionUserHandler(
        screen_name=input("screen_name: "),
        password=input("password: "),
    )
    cookies_dict = auth_handler.get_cookies().get_dict()

# To extract cookies from Windows (Linux by default)
# If you use tweepy_authlib, you must be on Windows
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"',
};

License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitter_openapi_python-0.0.27.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

twitter_openapi_python-0.0.27-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file twitter_openapi_python-0.0.27.tar.gz.

File metadata

File hashes

Hashes for twitter_openapi_python-0.0.27.tar.gz
Algorithm Hash digest
SHA256 ad3a73c752247b7151679ac7beb522045c913d912d2129bf3821c7880e74bfc2
MD5 672701a29c00633c33ba570c83c3f453
BLAKE2b-256 58dc1ad88673ccb9b027e04e33287067b29839a00e49e9a0e72ae85ddca7e2f5

See more details on using hashes here.

File details

Details for the file twitter_openapi_python-0.0.27-py3-none-any.whl.

File metadata

File hashes

Hashes for twitter_openapi_python-0.0.27-py3-none-any.whl
Algorithm Hash digest
SHA256 62a4cefeda6c4bf303ad22bb6a6309b1af46009667255e33ccbb6498fca3c8a3
MD5 3b7aff9eba4b2b6f7a809325a4a65f9b
BLAKE2b-256 cf41faeadd199c4c94f2158d17b8e92cd14dcf59378023fd0a0ebecaae16bc82

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page