The TweetClient class

class InstaTweet.tweetclient.TweetClient(profile, proxies=None)[source]View on GitHub

Bases: object

MAX_HASHTAGS = 5
DEFAULT_KEYS = {'Access Token': 'string', 'Consumer Key': 'string', 'Consumer Secret': 'string', 'Token Secret': 'string'}
__init__(profile, proxies=None)[source]View on GitHub

Initialize TweetClient using a Profile

Basically just a wrapper for tweepy. It uses the settings of a profile to initialize the API and send tweets

Parameters
  • profile (Profile) – the profile to use when initializing a tweepy.API object

  • proxies (Optional[dict]) – optional proxies to use when making API requests

get_api()[source]View on GitHub

Initializes a API object using the API keys of the loaded Profile

Return type

API

static get_oauth(api_keys)[source]View on GitHub

Initializes and returns an OAuth1UserHandler object from tweepy using the specified API keys

Parameters

api_keys (dict) – Twitter developer API keys with v1.1 endpoint access

Return type

OAuth1UserHandler

send_tweet(post, hashtags=None)[source]View on GitHub

Composes and sends a Tweet using an already-downloaded Instagram post

How Tweets are Sent

The InstaPost.filepath – set by download_post() – is used as the media source

The body of the tweet is then generated by build_tweet()

Parameters
  • post (InstaPost) – the post to tweet

  • hashtags (Optional[list[str]]) – a list of hashtags to randomly chose from and include in the tweet

Return type

bool

upload_media(post)[source]View on GitHub

Uploads the media from an already-downloaded Instagram post to Twitter

Note

If the post is a carousel, only the first 4 photos/videos will be uploaded

Parameters

post (InstaPost) – the Instagram post to use as the media source

Returns

the list of uploaded media ids (if API upload was successful) or False

Return type

Union[list, bool]

build_tweet(post, hashtags=None)[source]View on GitHub

Uses an InstaPost to build the body text of a tweet

How Tweets are Composed

Example:

>> post = instatweet.insta.get_user("dailykittenig").posts[0]
>> tweet = instatweet.twitter.build_tweet(post)
>> print(tweet)

carousel support yuh
#kitten #kittycat #catlover #petstagram #animals

https://www.instagram.com/p/Cjl3UWBOd8k
Parameters
  • post (InstaPost) – the post being tweeted

  • hashtags (Optional[list[str]]) – optional list of hashtags to randomly pick from and include

Returns

the text to use for the tweet

Return type

str

static pick_hashtags(hashtags)[source]View on GitHub

Randomly picks hashtags from the provided list and returns them as a single string

The number of hashtags chosen will either be 1 less than the length of the list (to avoid using the same tags in every tweet), or the value of MAX_HASHTAGS, whichever is smaller

Parameters

hashtags (list[str]) – a list of hashtags to randomly choose from

Example
from InstaTweet import TweetClient

>> TweetClient.pick_hashtags(['cat','dog','woof'])
"#woof #cat\n"
Return type

str

Note

A newline is added to help with formatting & character counting in build_tweet()