The Profile
classο
- class InstaTweet.profile.Profile(name='default', local=True, **kwargs)[source]View on GitHubο
Bases:
object
The
Profile
is a configuration class used extensively throughout the packageIt consists of a
user_map
and an associated collection of API/web scraping settingsβ¦
About the User Map
The
user_map
is a dict containing info about the users added to aProfile
Itβs used to help detect new posts and compose tweets on a per-user basis
Entries are created when you
add_users()
, which map the user to aUSER_MAPPING
The
USER_MAPPING
maintains lists of hashtags, scraped posts, and sent tweetsThe mapping is updated when you
add_hashtags()
and successfullysend_tweet()
You can access entries in the
user_map
as follows:get_user()
allows you to retrieve a full entry by usernameget_hashtags_for()
,get_scraped_from()
,get_tweets_for()
provide access to lists
β¦
[Optional]
A unique, identifying
name
can be assigned to the Profile, which may then be used tosave()
and, in turn,load()
its settingsThis makes it extremely easy to switch between Profiles and create templates
Saving isnβt a requirement to
start()
InstaTweet, butβ¦To
get_new_posts()
, InstaTweet makes comparisons with thescraped
list in theuser_map
Saving this list ensures you donβt
send_tweet()
for a post more than once
β¦
Important
If you do
save()
your profile, the save location is determined by the value ofProfile.local
Local saves are made to the
LOCAL_DIR
, as pickle filesRemote saves are made to a database (via the
db
module) as pickle bytes
You MUST configure the
DATABASE_URL
environment variable to save/load remotelyInstaTweet uses
SQLAlchemy
to create aDBConnection
β any db it supports is compatibleSee the
db
module for more information
- USER_MAPPING = {'hashtags': [], 'scraped': [], 'tweets': []}ο
Template for an entry in the
user_map
- LOCAL_DIR = '/home/docs/checkouts/readthedocs.org/user_builds/instatweet/checkouts/develop/docs/source/profiles'ο
Directory where local profiles are saved
- __init__(name='default', local=True, **kwargs)[source]View on GitHubο
Create a new
Profile
Note
Profile
creation is mandatory to use theInstaTweet
packageRequired as a parameter to initialize an
InstaTweet
objectNaming and saving it is ideal, but not necessary to
start()
InstaTweet
- Parameters
- Keyword Arguments
- session_id (
str
) Instagram
sessionid
cookie, obtained by logging in through browser
- session_id (
- twitter_keys (
dict
) Twitter API Keys with v1.1 endpoint access (see
DEFAULT_KEYS
for a template)
- twitter_keys (
- user_agent (
str
) β Optional The user agent to use for requests; uses a currently working hardcoded agent if not provided
- user_agent (
- proxy_key (
str
) β Optional Environment variable to retrieve proxies from
- proxy_key (
- user_map
dict
: Mapping of added Instagram users and theirUSER_MAPPING
Profile Creation Tips
All attributes can be passed as arguments at initialization or set directly afterwards
Property setters validate data types for the Mandatory Settings
The
Profile
as a whole is validated byvalidate()
- user_mapο
dict
: Mapping of added Instagram users and theirUSER_MAPPING
- classmethod load(name, local=True)[source]View on GitHubο
Loads an existing profile from a locally saved pickle file or remotely stored pickle bytes
- classmethod from_json(json_str)[source]View on GitHubο
Creates a profile from a JSON formatted string of config settings
- Return type
- classmethod from_dict(d)[source]View on GitHubο
Creates a profile from a dictionary of config settings
- Return type
- static profile_exists(name, local=True)[source]View on GitHubο
Checks locally/remotely to see if a
Profile
with the specified name has an existing save fileWhenever the
name
is changed, its property setter calls this method to ensure you donβt accidentally overwrite a save that alreadyexists
- static get_local_path(name)[source]View on GitHubο
Returns filepath of where a local profile would be saved
- Return type
- add_users(users, send_tweet=False)[source]View on GitHubο
Add Instagram user(s) to the
user_map
for subsequent monitoringNote
By default, newly added users wonβt have their posts tweeted the first time theyβre scraped
The IDs of the ~12 most recent posts are stored in the
scraped
listAny new posts from that point forward will be tweeted
You can override this by setting
send_tweet=True
This causes their ~12 most recent posts to be scraped AND tweeted
- add_hashtags(user, hashtags)[source]View on GitHubο
Add hashtag(s) to a user in the
user_map
, which will be randomly chosen from when composing Tweets
- save(name=None, alert=True)[source]View on GitHubο
Pickles and saves the
Profile
using the specified or currently set name.
- validate()[source]View on GitHubο
Checks to see if the Profile is fully configured for InstaTweeting
- Raises
ValueError β if the
session_id
,twitter_keys
, oruser_map
are invalid
- to_pickle()[source]View on GitHubο
Serializes profile to a pickled byte string
- Return type
- to_json()[source]View on GitHubο
Serializes profile to a JSON formatted string
- Return type
- to_dict()[source]View on GitHubο
Serializes profile to a dict
- Return type
- view_config()[source]View on GitHubο
Prints the
config
dict to make it legible
- property exists: boolο
Returns True if a local save file or database record exists for the currently set profile name
- property profile_path: strο
If
local
isTrue
, returns the file path for where this profile would be/is saved
- get_user(user)[source]View on GitHubο
Returns the specified userβs dict entry in the
user_map
- Return type
- get_scraped_from(user)[source]View on GitHubο
Returns a list of posts that have been scraped from the specified user
- Return type
- get_tweets_for(user)[source]View on GitHubο
Returns a list of tweets that use the specified userβs scraped content
- Return type
- get_hashtags_for(user)[source]View on GitHubο
Returns the hashtag list for the specified user
- Return type
- property local: boolο
Indicates if saves should be made locally (
True
) or on a remote database (False
)- Return type
- property name: strο
A name for the Profile
The
name
is used differently depending on the value oflocal
local==True
: the name determines theprofile_path
(path where it would save to)local==False
: the name is used as the primary key in theProfiles
database table
β¦
Profile Names Must Be Unique
When you set or change the
name
, a property setter will make sure noprofile_exists()
with that name before actually updating itThis ensures that you donβt accidentally overwrite a different Profileβs save data
β¦
- Raises
FileExistsError β if
local
==True
and a save is found in theLOCAL_DIR
ResourceWarning β if
local
==False
and a database row is found byquery_profile()
- property session_id: strο
Instagram
sessionid
cookie, obtained by logging in through a browser- Tip
If you log into your account with a browser you donβt use, the session cookie will last longer
- property twitter_keys: dictο
Twitter developer API keys with v1.1 endpoint access. See
DEFAULT_KEYS