Getting Started
InstaTweet Profiles
InstaTweet uses the Profile
class to help manage Twitter accounts, Instagram sessions, and page maps.
- class InstaTweet.profile.Profile(name='default', local=True, **kwargs)[source]View on GitHub
The
Profile
is a configuration class used extensively throughout the packageIt consists of a
page_map
and an associated collection of API/web scraping settings…
About the Page Map
The
page_map
is a dict containing info about the pages added to aProfile
It’s used to help detect new posts and compose tweets on a per-page basis
Entries are created when you
add_pages()
, which map the page to aPAGE_MAPPING
The
PAGE_MAPPING
maintains lists of hashtags, scraped posts, and sent tweetsThe mapping is updated when you
add_hashtags()
and successfullysend_tweet()
…
[Optional]
A unique, identifying
name
can optionally be assigned to the Profile, which may then be used tosave()
andload()
its settingsThe save location is determined by the value of
Profile.local
as follows:If
True
, saves are made locally to theLOCAL_DIR
as .pickle filesIf
False
, saves are made remotely to a database as pickle bytes
See Saving a Profile for more information
…
Profile Settings
Mandatory Settings
session_id
— Instagram sessionid cookie, obtained by logging in on a desktop browsertwitter_keys
— Twitter API keys with v1.1 endpoint access
Mandatory Settings with Default Values
Creating a Profile
Profile settings can be configured
By passing them as keyword arguments when initializing a
Profile
By setting them directly as object attributes after the
Profile
object is created
from InstaTweet import Profile
# Initialize a profile with arguments
p = Profile('myProfile', session_id='6011991A')
# Initialize a profile with no arguments
q = Profile()
q.name = 'myProfile'
q.session_id = '6011991A'
All settings can be accessed via the config
dict,
which can be pretty printed using view_config()
# View and compare configuration settings
>>> q.view_config()
name : myProfile
local : True
session_id : 6011991A
twitter_keys : {'Consumer Key': 'string', 'Consumer Secret': 'string', 'Access Token': 'string', 'Token Secret': 'string'}
user_agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
proxy_key : None
page_map : {}
…
Validating Profile Settings
Property setters validate data types for the Mandatory Settings
Requirements aren’t strictly enforced until
start()
is called, which will firstvalidate()
the profile settings
Populating the Page Map
The Page Map
The page_map
allows a Profile
to maintain
a history of package-related activity for its added IG pages (users or hashtags).
Pages are mapped to their PAGE_MAPPING
, which contains their associated lists of:
PAGE_MAPPING = {'hashtags': [], 'scraped': [], 'tweets': []}
hashtags
— the page’s associated hashtag list (for use when composing tweets)scraped
— the list of posts that have been scraped from the page (only the post id)tweets
— the list of sent tweets containing media scraped from that page (limited data)
The mapping gets updated each time InstaTweet
successfully scrapes and tweets a post from the page
Adding Pages
Use the add_pages()
method to add one or more Instagram pages
to a Profile
’s page_map
from InstaTweet import Profile
# Add one page at a time
>>> p = Profile('myProfile')
>>> p.add_pages('the.dailykitten', send_tweet=True)
Added Instagram page @the.dailykitten to the page map
# Add multiple pages at once
>>> pages = ['dailykittenig','#thedailykitten']
>>> p.add_pages(pages)
Added Instagram page @dailykittenig to the page map
Added Instagram page #thedailykitten to the page map
The get_page()
method can be used to retrieve the full PAGE_MAPPING
of an added page
>> p.get_page('the.dailykitten')
{'hashtags': [], 'scraped': [-1], 'tweets': []}
Adding Hashtags
You can add_hashtags()
for each page in the page_map
They’ll be chosen from at random when composing tweets based on one of their
posts
For more info, see
pick_hashtags()
,build_tweet()
andsend_tweet()
# Add a single hashtag for a specific page
>>> p.add_hashtags('dailykittenig', 'cats')
Added hashtags for dailykittenig
# Add multiple hashtags at once
>>> pages = ['the.dailykitten', '#thedailykitten']
>>> hashtags = ['kittygram', 'kittycat']
>>> for page in pages:
... p.add_hashtags(page, hashtags)
Added hashtags for the.dailykitten
Added hashtags for #thedailykitten
>>> p.view_config()
name : myProfile
local : True
session_id : 6011991A
twitter_keys : {'Consumer Key': 'string', 'Consumer Secret': 'string', 'Access Token': 'string', 'Token Secret': 'string'}
user_agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
proxy_key : None
page_map : {'the.dailykitten': {'hashtags': ['kittygram', 'kittycat'], 'scraped': [-1], 'tweets': []}, 'dailykittenig': {'hashtags': ['cats'], 'scraped': [], 'tweets': []}, '#thedailykitten': {'hashtags': ['kittygram', 'kittycat'], 'scraped': [], 'tweets': []}}
Page Map Access Methods
Page Map Access Methods
The Profile
has several methods that allow for easy access to the page_map
get_page()
provides access to a particular page’sPAGE_MAPPING
get_scraped_from()
returns the list of posts scraped from a specified pageget_hashtags_for()
returns the list of hashtags to use in tweets for the specified pageget_tweets_for()
returns a list of tweets that use the specified page’s scraped content
Saving a Profile
Saving a Profile
When you save()
your Profile
, the current or specified name
will be used to create or update a save file in the location specified by local
From the Docs…
- Profile.save(name=None, alert=True)[source]View on GitHub
Pickles and saves the
Profile
using the specified or currently set name.
- InstaTweet.profile.Profile.local =True
Indicates if saves should be made locally (
True
) or on a remote database (False
)
Important!!
You MUST configure the DATABASE_URL
environment variable to save/load remotely
InstaTweet uses SQLAlchemy
to create a DBConnection
Any
SQLAlchemy
-supported database is therefore also supported byInstaTweet
See the
db
module for more information
Example: Save a Profile
from InstaTweet import Profile
>>> p = Profile('myProfile')
>>> p.save()
Saved Local Profile myProfile
>>> p.save('aProfile')
>>> print(p.name)
Saved Local Profile aProfile
aProfile
Profile names must be unique - you cannot save or create a profile if a
profile_exists()
with that name already
>>> q = Profile('myProfile')
FileExistsError: Local save file already exists for profile named "myProfile"
Please choose another name, load the profile, or delete the file.
Running a Profile
Once a Profile
is configured, it can be used to initialize and
start()
an InstaTweet
object
from InstaTweet import InstaTweet, Profile
# Load an existing saved or unsaved profile into InstaTweet
>>> profile = Profile.load("myProfile")
>>> insta_tweet = InstaTweet(profile)
# Or directly InstaTweet.load() the settings in by Profile name
>>> insta_tweet = InstaTweet.load(profile_name="myProfile")
# Then run InstaTweet by calling start()
>>> insta_tweet.start()
From the Docs…
- InstaTweet.start(max_posts=12)[source]View on GitHub
InstaTweets all pages that have been added to the loaded
Profile
The most recent posts from each page will be scraped, then compared to the
scraped
list in thePAGE_MAPPING
to determine which are new.Up to
max_posts
new posts from each page will then be downloaded and tweetedNote
If
InstaTweet
fails todownload_post()
orsend_tweet()
, thePAGE_MAPPING
won’t be updatedThis ensures that failed repost attempts are retried in the next call to
start()
If a save file for the Profile already
exists
, successful reposts will trigger a call tosave()
- Parameters
max_posts (int) – the maximum number of new posts to download and tweet per page
As InstaTweet
runs, its progress will be logged to console:
Starting InstaTweet for Profile: myProfile
Checking posts from @the.dailykitten
...
Checking posts from #thedailykitten
...
Finished insta-tweeting for #thedailykitten
All pages have been insta-tweeted