Twitter OpenAPI
Project description
twitter_openapi_python
Twitter scraping with data validation by pydantic.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for twitter_openapi_python-0.0.26.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 329344477ed51120c8a4a0575b2ea7d426bcc6aac173f02f538811db428b2e4e |
|
MD5 | 47fc0e6bf23cd56ba7be95ec0a8d79b8 |
|
BLAKE2b-256 | 2153100501071df3ef314e29139bc7ad65102db9e627250875731e38b985be5b |
Close
Hashes for twitter_openapi_python-0.0.26-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5050a33e47e1cb4cff708090365fc36f8d4a588230afe4912fa4cb8ebd770dd |
|
MD5 | 6baffb60e5fe8a54fd57594a9ceafd06 |
|
BLAKE2b-256 | d69b49ef9c34dcbbc35d877f7b96a562d395b1381bb704da8e19782c345eb4cd |