Metadata-Version: 2.1
Name: python-twitter-v2
Version: 0.3.5
Summary: A simple Python wrapper for Twitter API v2 ✨ 🍰 ✨
Home-page: https://github.com/sns-sdks/python-twitter
License: MIT
Keywords: twitter,twitter sdk,twitter api,Twitter API V2
Author: ikaroskun
Author-email: merle.liukun@gmail.com
Requires-Python: >=3.6,<4.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: dataclasses-json (>=0.5.2,<0.6.0)
Requires-Dist: requests (>=2.24,<3.0)
Requires-Dist: requests-oauthlib (>=1.3.0,<2.0.0)
Project-URL: Repository, https://github.com/sns-sdks/python-twitter
Description-Content-Type: text/x-rst

python-twitter

A simple Python wrapper around for Twitter API v2 :sparkles: :cake: :sparkles:.

.. image:: https://img.shields.io/endpoint?url=https%3A%2F%2Ftwbadges.glitch.me%2Fbadges%2Fv2
   :target: https://developer.twitter.com/en/docs/twitter-api
   :alt: v2

.. image:: https://github.com/sns-sdks/python-twitter/workflows/Test/badge.svg
    :target: https://github.com/sns-sdks/python-twitter/actions
    :alt: Build Status

.. image:: https://codecov.io/gh/sns-sdks/python-twitter/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/sns-sdks/python-twitter
    :alt: Codecov

.. image:: https://img.shields.io/pypi/v/python-twitter-v2.svg
    :target: https://pypi.org/project/python-twitter-v2/
    :alt: PyPI



============
Introduction
============

Twitter has published new version `Twitter API V2 <https://twitter.com/TwitterDev/status/1293593516040269825>`_ for developer at Aug 13, 2020.

This library provides a service to easily use this new version Twitter API.

=============
Documentation
=============

You can get all api description and update at `Twitter API v2: Early Access <https://developer.twitter.com/en/docs/twitter-api/early-access>`_.

Library docs site on `here <https://sns-sdks.github.io/python-twitter/>`_


==========
Installing
==========

You can install this library easily by `pypi`:

.. code-block:: shell

    $ pip install python-twitter-v2

Code is hosted at `https://github.com/sns-sdks/python-twitter <https://github.com/sns-sdks/python-twitter>`_.

Checkout latest development version with:

.. code-block:: shell

    $ git clone https://github.com/sns-sdks/python-twitter.git
    $ cd python-twitter

Install dependencies with:

.. code-block:: shell

    $ make env


Run tests with:

.. code-block:: shell

    $ make test

Run tests with coverage:

.. code-block:: shell

    $ make cov-term
    $ make cov-html

=====
Using
=====

The API is exposed via the ``pytwitter.Api`` class.

Now cover features:

- User lookup
- Follows
- Blocks
- Mutes
- Tweet lookup
- Search Tweets
- Tweet counts
- Timelines
- Filtered stream
- Sampled stream
- Likes
- Retweets
- Hide replies

-----------
INSTANTIATE
-----------

You can initialize with an bearer token:

.. code-block:: python

    >>> from pytwitter import Api
    >>> api = Api(bearer_token="Your bearer token")

With user context token:

.. code-block:: python

    >>> api = Api(
            consumer_key="consumer key",
            consumer_secret="consumer secret",
            access_token="access token",
            access_secret="access secret"
        )

Or with authorize by user:

.. code-block:: python

    >>> api = Api(consumer_key="consumer key",consumer_secret="consumer secret",oauth_flow=True)
    # get url for user to authorize
    >>> api.get_authorize_url()
    # copy the response url
    >>> api.generate_access_token("https://localhost/?oauth_token=oauth_token&oauth_verifier=oauth_verifier")
    {'oauth_token': 'oauth_token',
     'oauth_token_secret': 'oauth_token_secret',
     'user_id': '123456',
     'screen_name': 'screen name'}

------------
Users-lookup
------------

You can get information about a user or group of users, specified by a user ID or a username.

Get group of users:

.. code-block:: python

    # By ids
    >>> api.get_users(ids=["783214", "2244994945"])
    Response(data=[User(id='2244994945', name='Twitter Dev', username='TwitterDev'), User(id='783214', name='Twitter', username='Twitter')])

    # By username
    >>> api.get_users(usernames="Twitter,TwitterDev")
    Response(data=[User(id='2244994945', name='Twitter Dev', username='TwitterDev'), User(id='783214', name='Twitter', username='Twitter')])

Get single user:

.. code-block:: python

    # By id
    >>> api.get_user(user_id="783214")
    Response(data=User(id='783214', name='Twitter', username='Twitter'))

    # By username
    >>> api.get_user(username="Twitter")
    Response(data=User(id='783214', name='Twitter', username='Twitter'))

Get user following:

.. code-block:: python

    >>> api.get_following(user_id="2244994945", max_results=5)
    Response(data=[User(id='459860328', name='julie✨', username='JulieMendoza206'), User(id='273830767', name='🄿🅄🅂🄷', username='rahul_pushkarna')...])

Get user followers:

.. code-block:: python

    >>> api.get_followers(user_id="2244994945", max_results=5)
    Response(data=[User(id='715131097332518912', name='Daniel', username='RGIDaniel'), User(id='1176323137757048832', name='Joyce Wang', username='joycew67')...])


You can follow or unfollow user if you have OAuth 1.0a User context.

follow user:

.. code-block:: python

    >>> api.follow_user(user_id="123456", target_user_id="654321")
    {'data': {'following': True, 'pending_follow': False}}


unfollow user:

.. code-block:: python

    >>> api.unfollow_user(user_id="123456", target_user_id="654321")
    {'data': {'following': False}}

-------------
Tweets-lookup
-------------

You can get information about a tweet or group of tweets by tweet id(s).

Get single tweet:

.. code-block:: python

    >>> api.get_tweet("1354143047324299264", expansions=["attachments.media_keys"], media_fields=["type","duration_ms"])
    Response(data=Tweet(id=1354143047324299264, text=Academics are one of the biggest groups using...))

Get group of tweets:

.. code-block:: python

    >>> api.get_tweets(["1261326399320715264","1278347468690915330"],expansions="author_id",tweet_fields=["created_at"], user_fields=["username","verified"])
    Response(data=[Tweet(id=1261326399320715264, text=Tune in to the @MongoDB @Twitch stream...), Tweet(id=1278347468690915330, text=Good news and bad news: 2020 is half over)])

-------------
Streaming API
-------------

For Streaming, this provide `StreamApi` independent. Same as main `Api`, You need initial it first.

.. code-block:: python

    >>> from pytwitter import StreamApi
    >>> stream_api = StreamApi(bearer_token="bearer token")
    # or use consumer key and secret
    >>> stream_api = StreamApi(consumer_key="consumer key", consumer_secret="consumer secret")


For Sample Stream tweets, You can use `sample_stream` function to build a connection.

.. code-block:: python

    >>> stream_api.sample_stream()

For Search Stream, You can point your rules.

Get your current rules.

.. code-block:: python

    >>> stream_api.get_rules()
    Response(data=[StreamRule(id='1369580714056843266', value='twitter api ')])

Delete You rules.

.. code-block:: python

    >>> stream_api.manage_rules(rules={"delete": {"ids": ["1369580714056843266"]}})
    Response(data=[])

Add new rules. If you set `dry_run` to True, will only validate rules, and not create them.

.. code-block:: python

    >>> np = {
            "add": [
                {"value": "cat has:media", "tag": "cats with media"},
                {"value": "cat has:media -grumpy", "tag": "happy cats with media"}
            ]
         }
    >>> stream_api.manage_rules(rules=np, dry_run=True)
    Response(data=[StreamRule(id='1370406958721732610', value='cat has:media -grumpy'), StreamRule(id='1370406958721732609', value='cat has:media')])

Then you can use `search_stream` to get tweets match your rules.

.. code-block:: python

    >>> stream_api.search_stream()


You can go to the `Example folder <examples>`_ for streaming examples.

====
TODO
====

- More Api waiting twitter


