python-tweet

A Python library for retrieving tweets from X (f.k.a. Twitter)


Keywords
twitter, x, tweet, api
License
Other
Install
pip install python-tweet==0.2.0

Documentation

Python Tweet

PyPI - Version PyPI - Python Version PyPI - License Tests

About

pytweet is a simple Python library with one goal: to retrieve tweet information from X for free.

Inspired by React-tweet project.

Key Feature

  • doesn't require an X (formerly known as Twitter) API token

Installation

pip install python-tweet

From source:

make install

or

pip install .

Usage

It may cause conflicts with PyTweet if you are using it in your project.

Async way:

import asyncio
import json

from pytweet import get_tweet


async def main():
    tweet_id = "1803774806980022720"
    data = await get_tweet(tweet_id)
    with open(f"{tweet_id}.json", "w") as f:
        json.dump(data, f, indent=2)


if __name__ == '__main__':
    asyncio.run(main())

Sync way:

import json

from pytweet.sync import get_tweet


def main():
    tweet_id = "1803774806980022720"
    data = get_tweet(tweet_id)
    with open(f"{tweet_id}.json", "w") as f:
        json.dump(data, f, indent=2)


if __name__ == '__main__':
    main()

Pydantic mode:

If you prefer working with object-oriented concepts, you may prefer receiving a class. However, be aware that the returned JSON from syndication is undocumented, so errors or data loss may occur during conversion to a class.

Requires the installation of pydantic.

pip install python-tweet[pydantic]
import asyncio
import json

from pytweet import get_tweet


async def main():
    tweet_id = "1803774806980022720"
    tweet = await get_tweet(tweet_id)
    with open(f"{tweet_id}.json", "w") as f:
        json.dump(tweet.model_dump(mode="json"), f, indent=2)


if __name__ == '__main__':
    asyncio.run(main())

If you are using Pydantic but want to receive a dict, pass the as_dict argument to the get_tweet function.


To-do

  • Return a Tweet class instead a raw dict.

License

python-tweet is released under the MIT License. See the LICENSE file for license information.