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

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

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

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

…

[Optional]

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

The save location is determined by the value of Profile.local as follows:

  • If True, saves are made locally to the LOCAL_DIR as .pickle files

  • If False, saves are made remotely to a database as pickle bytes

See Saving a Profile for more information

…

PAGE_MAPPING: Dict = {'hashtags': [], 'scraped': [], 'tweets': []}

Template for an entry in the page_map

LOCAL_DIR: str = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/instatweet/envs/latest/lib/python3.8/site-packages/profiles')

Directory where local profiles are saved

__init__(name='default', local=True, **kwargs)[source]View on GitHub

Create a new Profile

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

  • proxy_key (str) – Optional

    Environment variable to retrieve proxies from

user_agent: str

The user agent to use for requests

proxy_key: str

Environment variable to retrieve proxies from

page_map: Dict[str, Dict]

Mapping of added Instagram pages and their PAGE_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_pages(pages, send_tweet=False)[source]View on GitHub

Add Instagram page(s) to the page_map for subsequent monitoring

  • An Instagram profile can be added as "@username" or "username"

  • A hashtag must be added as "#hashtag"

Note

By default, newly added pages won’t have their posts tweeted the first time they’re scraped

  • The IDs of the most recent posts are stored in the scraped list

  • Any new posts from that point forward will be tweeted

You can scrape AND tweet posts on the first run by setting send_tweet=True

Parameters
  • pages (Iterable) – Instagram pages 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(page, hashtags)[source]View on GitHub

Add hashtag(s) to a page in the page_map, which will be randomly chosen from when composing Tweets

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

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

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 page_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_page(page)[source]View on GitHub

Returns the specified page’s dict entry in the page_map

Return type

dict

get_scraped_from(page)[source]View on GitHub

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

Return type

list

get_tweets_for(page)[source]View on GitHub

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

Return type

list

get_hashtags_for(page)[source]View on GitHub

Returns the hashtag list for the specified page

Return type

list

property local: bool

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

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 browser

property twitter_keys: Dict

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