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 package

It 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 a Profile

  • 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 a USER_MAPPING

  • The USER_MAPPING maintains lists of hashtags, scraped posts, and sent tweets

  • The mapping is updated when you add_hashtags() and successfully send_tweet()

You can access entries in the user_map as follows:

…

[Optional]

A unique, identifying name can be assigned to the Profile, which may then be used to save() and, in turn, load() its settings

  • This makes it extremely easy to switch between Profiles and create templates

Saving isn’t a requirement to start() InstaTweet, but…

…

Important

If you do save() your profile, the save location is determined by the value of Profile.local

  • Local saves are made to the LOCAL_DIR, as pickle files

  • Remote saves are made to a database (via the db module) as pickle bytes

You MUST configure the DATABASE_URL environment variable to save/load remotely

  • InstaTweet uses SQLAlchemy to create a DBConnection – any db it supports is compatible

  • See 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 the InstaTweet package

  • Required as a parameter to initialize an InstaTweet object

  • Naming and saving it is ideal, but not necessary to start() InstaTweet

Parameters
  • name (str) – unique profile name

  • local (bool) – indicates if profile is being saved locally or on a remote database

  • kwargs – see below

Keyword Arguments
  • session_id (str)

    Instagram sessionid cookie, obtained by logging in through browser

  • twitter_keys (dict)

    Twitter API Keys with v1.1 endpoint access (see DEFAULT_KEYS for a template)

  • user_agent (str) – Optional

    The user agent to use for requests; uses a currently working hardcoded agent if not provided

  • proxy_key (str) – Optional

    Environment variable to retrieve proxies from

  • user_map

    dict: Mapping of added Instagram users and their USER_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 by validate()

user_map

dict: Mapping of added Instagram users and their USER_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

Parameters
  • name (str) – the name of the Profile to load

  • local (bool) – whether the profile is saved locally (default, True) or remotely on a database

Return type

Profile

classmethod from_json(json_str)[source]View on GitHub

Creates a profile from a JSON formatted string of config settings

Return type

Profile

classmethod from_dict(d)[source]View on GitHub

Creates a profile from a dictionary of config settings

Return type

Profile

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 file

Whenever the name is changed, its property setter calls this method to ensure you don’t accidentally overwrite a save that already exists

Parameters
  • name (str) – the name of the Profile to check for

  • local (bool) – the location (local/remote) to check for an existing save

Return type

bool

static get_local_path(name)[source]View on GitHub

Returns filepath of where a local profile would be saved

Return type

str

add_users(users, send_tweet=False)[source]View on GitHub

Add Instagram user(s) to the user_map for subsequent monitoring

Note

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 list

  • Any 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

Parameters
  • users (Iterable) – Instagram username(s) to automatically scrape and tweet content from

  • send_tweet (bool) – choose if tweets should be sent on the first scrape, or only for new posts going forward

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

Parameters
  • user (str) – the user in the user map to add hashtags to

  • hashtags (Iterable) – hashtags to choose from and include in any Tweets where content comes from this user

save(name=None, alert=True)[source]View on GitHub

Pickles and saves the Profile using the specified or currently set name.

Parameters
  • name (Optional[str]) – name to save the Profile under; replaces the current name

  • alert (bool) – set to True to print a message upon successful save

Return type

bool

validate()[source]View on GitHub

Checks to see if the Profile is fully configured for InstaTweeting

Raises

ValueError – if the session_id, twitter_keys, or user_map are invalid

to_pickle()[source]View on GitHub

Serializes profile to a pickled byte string

Return type

bytes

to_json()[source]View on GitHub

Serializes profile to a JSON formatted string

Return type

str

to_dict()[source]View on GitHub

Serializes profile to a dict

Return type

dict

view_config()[source]View on GitHub

Prints the config dict to make it legible

property config: dict

Returns a dictionary containing important configuration settings

property exists: bool

Returns True if a local save file or database record exists for the currently set profile name

property is_default: bool

Check if profile name is set or not

property profile_path: str

If local is True, 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

dict

get_scraped_from(user)[source]View on GitHub

Returns a list of posts that have been scraped from the specified user

Return type

list

get_tweets_for(user)[source]View on GitHub

Returns a list of tweets that use the specified user’s scraped content

Return type

list

get_hashtags_for(user)[source]View on GitHub

Returns the hashtag list for the specified user

Return type

list

property local: bool

Indicates if saves should be made locally (True) or on a remote database (False)

Return type

bool

property name: str

A name for the Profile

The name is used differently depending on the value of local

  • local==True: the name determines the profile_path (path where it would save to)

  • local==False: the name is used as the primary key in the Profiles database table

…

Profile Names Must Be Unique

When you set or change the name, a property setter will make sure no profile_exists() with that name before actually updating it

  • This ensures that you don’t accidentally overwrite a different Profile’s save data

…

Raises
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