# twitter_openapi_python
Twitter scraping with data validation by pydantic.

## Setup
```shell
pip install twitter-openapi-python
```
## Usage
```python
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
```python
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.
```python
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:
- [Custom License](./LICENSE)
- [GNU Affero General Public License v3.0](./LICENSE.AGPL)
Raw data
{
"_id": null,
"home_page": "https://github.com/fa0311/twitter_openapi_python",
"name": "twitter-openapi-python",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "OpenAPI, OpenAPI-Generator, Twitter OpenAPI",
"author": "fa0311",
"author_email": "yuki@yuki0311.com",
"download_url": "https://files.pythonhosted.org/packages/60/4f/9164070a180e55c5ed823f7b5bdd4230e8178a17bcf4890fc022a562af18/twitter_openapi_python-0.0.32.tar.gz",
"platform": null,
"description": "# twitter_openapi_python\r\n\r\nTwitter scraping with data validation by pydantic.\r\n\r\n\r\n\r\n## Setup\r\n\r\n```shell\r\npip install twitter-openapi-python\r\n```\r\n\r\n## Usage\r\n\r\n```python\r\nimport json\r\nimport datetime\r\n\r\nfrom pathlib import Path\r\nfrom tweepy_authlib import CookieSessionUserHandler\r\nfrom twitter_openapi_python import TwitterOpenapiPython\r\n\r\n# login by tweepy_authlib\r\nif Path(\"cookie.json\").exists():\r\n with open(\"cookie.json\", \"r\") as f:\r\n cookies_dict = json.load(f)\r\n if isinstance(cookies_dict, list):\r\n cookies_dict = {k[\"name\"]: k[\"value\"] for k in cookies_dict}\r\nelse:\r\n auth_handler = CookieSessionUserHandler(\r\n screen_name=input(\"screen_name: \"),\r\n password=input(\"password: \"),\r\n )\r\n cookies_dict = auth_handler.get_cookies().get_dict()\r\n\r\n# To extract cookies from Windows (Linux by default)\r\n# If you use tweepy_authlib, you must be on Windows\r\nclient = TwitterOpenapiPython()\r\nclient.additional_api_headers = {\r\n \"sec-ch-ua-platform\": '\"Windows\"',\r\n}\r\nclient.additional_browser_headers = {\r\n \"sec-ch-ua-platform\": '\"Windows\"',\r\n}\r\n\r\n# get client from cookies\r\n\ue05c\u64ab = client.get_client_from_cookies(cookies=cookies_dict)\r\n\r\n# tweet \"Hello World!!\" with current time\r\ntime = datetime.datetime.now().strftime(\"%Y-%m-%dT%H:%M:%SZ\")\r\n\ue05c\u64ab.get_post_api().post_create_tweet(tweet_text=f\"Hello World!!{time}\")\r\n\r\n# get user info\r\nresponse = \ue05c\u64ab.get_user_api().get_user_by_screen_name(\"elonmusk\")\r\n```\r\n\r\n### Login\r\n\r\n```python\r\nfrom twitter_openapi_python import TwitterOpenapiPython\r\n\r\n# guest client\r\n\ue05c\u64ab = TwitterOpenapiPython().get_guest_client()\r\ntweet = \ue05c\u64ab.get_default_api().get_tweet_result_by_rest_id(\"1349129669258448897\")\r\nassert tweet.data is not None\r\nassert tweet.data.tweet.legacy is not None\r\nprint(tweet.data.tweet.legacy.full_text)\r\n```\r\n\r\n### Multiple OS\r\n\r\nThe Token can only be used on the same OS that issued the Token\r\nIn other words, if the sec-ch-ua-platform does not match, the Token cannot be used.\r\nThis library uses the Linux Chrome header by default.\r\nTo use Token issued by Windows, do the following.\r\n\r\n```python\r\nclient = TwitterOpenapiPython()\r\nclient.additional_api_headers = {\r\n \"sec-ch-ua-platform\": '\"Windows\"',\r\n};\r\n```\r\n\r\n## License\r\n\r\nThis project is dual licensed. You can choose one of the following licenses:\r\n\r\n- [Custom License](./LICENSE)\r\n- [GNU Affero General Public License v3.0](./LICENSE.AGPL)\r\n",
"bugtrack_url": null,
"license": "proprietary or AGPL-3.0-or-later",
"summary": "Twitter OpenAPI",
"version": "0.0.32",
"project_urls": {
"Homepage": "https://github.com/fa0311/twitter_openapi_python"
},
"split_keywords": [
"openapi",
" openapi-generator",
" twitter openapi"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "656d83169b4d946e0c99201750e4890ca577fdf5b7ba5536faea20233eeb2dca",
"md5": "bdcf6dadf348c2809f7593453625fa1a",
"sha256": "5a164b008cc75e575d5c2648a8398bfb3d37dc47f09ab08037518c1fc1f4b237"
},
"downloads": -1,
"filename": "twitter_openapi_python-0.0.32-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bdcf6dadf348c2809f7593453625fa1a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 33553,
"upload_time": "2025-02-01T17:28:52",
"upload_time_iso_8601": "2025-02-01T17:28:52.351394Z",
"url": "https://files.pythonhosted.org/packages/65/6d/83169b4d946e0c99201750e4890ca577fdf5b7ba5536faea20233eeb2dca/twitter_openapi_python-0.0.32-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "604f9164070a180e55c5ed823f7b5bdd4230e8178a17bcf4890fc022a562af18",
"md5": "c708c1d89f31d555a57b5ce4b1ed3dbd",
"sha256": "806f0f65f04f5e2665fff65fa12392db8be15714c01db50c4be16a8ed75ceedf"
},
"downloads": -1,
"filename": "twitter_openapi_python-0.0.32.tar.gz",
"has_sig": false,
"md5_digest": "c708c1d89f31d555a57b5ce4b1ed3dbd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 26665,
"upload_time": "2025-02-01T17:28:55",
"upload_time_iso_8601": "2025-02-01T17:28:55.360742Z",
"url": "https://files.pythonhosted.org/packages/60/4f/9164070a180e55c5ed823f7b5bdd4230e8178a17bcf4890fc022a562af18/twitter_openapi_python-0.0.32.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-01 17:28:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fa0311",
"github_project": "twitter_openapi_python",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "twitter-openapi-python"
}