Skip to content

Instantly share code, notes, and snippets.

@ryanlong1004
Created January 17, 2023 18:18
Show Gist options
  • Save ryanlong1004/29f11974ca9bfe56f1209d1c4c53c615 to your computer and use it in GitHub Desktop.
Save ryanlong1004/29f11974ca9bfe56f1209d1c4c53c615 to your computer and use it in GitHub Desktop.
Send tweets with Python and Tweepy.
""" Twitter convenience
"""
import collections
import logging
import os
import tweepy
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
logger: logging.Logger = logging.getLogger(__name__)
DEFAULT_ENCODING = "utf-8"
TwitterCredentials = collections.namedtuple(
"TwitterCredentials",
["consumer_key", "consumer_secret", "access_token", "access_token_secret"],
)
class Twitter:
"""contains Twitter ops"""
def __init__(self, credentials: TwitterCredentials):
self.credentials = credentials
def tweet(self, message: str):
"""tweet message"""
client = tweepy.Client(
consumer_key=self.credentials.consumer_key,
consumer_secret=self.credentials.consumer_secret,
access_token=self.credentials.access_token,
access_token_secret=self.credentials.access_token_secret,
)
try:
return client.create_tweet(text=message)
except tweepy.Forbidden as e:
logger.warn(str(e).replace("\n", " "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment