Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
iyanuashiri committed Jul 9, 2022
1 parent 4c3ea2b commit cd13332
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 158 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ Twitter Thread is a library to manage threads on Twitter. You can GET and POST t
## Installation

```bash
pip install twitter-threader
pip install twitter_threader
```


## Usage

### GET a thread on Twitter


```python
from threader.threader import connect_api, Thread

from twitter_threader.threader import connect_api, Thread

consumer_key = 'CONSUMER_KEY'
consumer_secret = 'CONSUMER_SECRET'
access_token_key = 'ACCESS_TOKEN'
access_token_secret = 'ACCESS_TOKEN_SECRET'


api = connect_api(consumer_key, consumer_secret, access_token_key, access_token_secret)

thread = Thread(api)
Expand All @@ -38,24 +35,21 @@ thread.convert_to_post('1247616218153902080')

### POST a thread on Twitter


```python
from threader.threader import connect_api, Thread

from twitter_threader.threader import connect_api, Thread

consumer_key = 'CONSUMER_KEY'
consumer_secret = 'CONSUMER_SECRET'
access_token_key = 'ACCESS_TOKEN'
access_token_secret = 'ACCESS_TOKEN_SECRET'


api = connect_api(consumer_key, consumer_secret, access_token_key, access_token_secret)


thread = Thread(api)
username = 'YOUR USERNAME'
thread.post_thread('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Metus dictum at tempor commodo ullamcorper a lacus. Amet justo donec enim diam vulputate. Sit amet justo donec enim diam vulputate ut pharetra sit. Leo duis ut diam quam. At ultrices mi tempus imperdiet. Mauris augue neque gravida in fermentum. Fermentum posuere urna nec tincidunt praesent semper feugiat nibh. Placerat vestibulum lectus mauris ultrices eros in cursus turpis massa. In aliquam sem fringilla ut morbi tincidunt augue.',
username)
thread.post_thread(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Metus dictum at tempor commodo ullamcorper a lacus. Amet justo donec enim diam vulputate. Sit amet justo donec enim diam vulputate ut pharetra sit. Leo duis ut diam quam. At ultrices mi tempus imperdiet. Mauris augue neque gravida in fermentum. Fermentum posuere urna nec tincidunt praesent semper feugiat nibh. Placerat vestibulum lectus mauris ultrices eros in cursus turpis massa. In aliquam sem fringilla ut morbi tincidunt augue.',
username)

```

132 changes: 0 additions & 132 deletions new.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_threader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from decouple import config
import pytest

from threader.threader import Thread, connect_api
from twitter_threader.threader import Thread, connect_api

consumer_key = config('CONSUMER_KEY')
consumer_secret = config('CONSUMER_SECRET')
Expand Down
Empty file removed threader/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions twitter_threader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Twitter Threader is a library to manage threads on Twitter. You can GET and POST threads on Twitter
"""
from twitter_threader.threader import Thread, connect_api

__version__ = "1.5.0"
__author__ = "Iyanuoluwa Ajao"
__license__ = "MIT"
58 changes: 45 additions & 13 deletions threader/threader.py → twitter_threader/threader.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,63 @@
"""
Twitter Threader is a library to manage threads on Twitter. You can GET and POST threads on Twitter
"""
__version__ = '1.5.0'
__author__ = 'Iyanuoluwa Ajao'
__license__ = 'MIT'
__version__ = "1.5.0"
__author__ = "Iyanuoluwa Ajao"
__license__ = "MIT"

import textwrap
import tweepy


def connect_api(consumer_key, consumer_secret, access_token_key, access_token_secret):
"""
Twitter Authentication Keys
@param consumer_key:
@param consumer_secret:
@param access_token_key:
@param access_token_secret:
@return:
"""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
return api


class Thread:
"""
Thread Class
"""

def __init__(self, api):
self.api = api

def get_thread(self, status_id, thread=None):
status = self.api.get_status(status_id, tweet_mode='extended') if status_id is None else \
self.api.get_status(status_id, tweet_mode='extended')
"""
GET a Thread from a Tweet status ID.
@param status_id: The ID of a Tweet
@param thread: A list of tweets
@return: A list of tweets
"""
status = (
self.api.get_status(status_id, tweet_mode="extended")
if status_id is None
else self.api.get_status(status_id, tweet_mode="extended")
)
thread = [] if thread is None else thread
status_id = status.in_reply_to_status_id
tweet = str(status.full_text)
thread.append(tweet)
if status_id is None:
return thread
else:
return self.get_thread(status_id, thread)
return self.get_thread(status_id, thread)

def convert_to_post(self, status_id):
"""
Convert a thread(list of tweets) to a string of words
@param status_id: The ID of a Tweet.
@return: A string of words.
"""
thread = self.get_thread(status_id)
thread = reversed(thread)
post = " ".join(thread)
Expand All @@ -44,22 +69,29 @@ def _check_username(self, user):
return screen_name

def _convert_username(self, username):
mention = f'@{self._check_username(username)} '
mention = f"@{self._check_username(username)} "
return mention

def post_thread(self, sentences, username, in_reply_to_status_id=None, thread=None):
"""
POST a thread from a string of words.
@param sentences: A string a words.
@param username: The username of a Twitter account
@param in_reply_to_status_id:
@param thread: A list of tweets
@return:
"""
mention = self._convert_username(username)
mention_length = len(mention)
left = 280 - mention_length

thread = [] if thread is None else thread
tweets = textwrap.wrap(sentences, width=left)
for tweet in tweets:
sentences = sentences[len(tweet):]
tweet = self.api.update_status(mention + f'{tweet}', in_reply_to_status_id)
sentences = sentences[len(tweet) :]
tweet = self.api.update_status(mention + f"{tweet}", in_reply_to_status_id)
thread.append(tweet.id)
if sentences is None:
return thread
else:
in_reply_to_status_id = int(tweet.id)
return self.post_thread(sentences, mention, in_reply_to_status_id, thread)
in_reply_to_status_id = int(tweet.id)
return self.post_thread(sentences, mention, in_reply_to_status_id, thread)

0 comments on commit cd13332

Please sign in to comment.