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
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
β¦
- PAGE_MAPPING: Dict = {'hashtags': [], 'scraped': [], 'tweets': []}ο
Template for an entry in the
page_map
- LOCAL_DIR: str = PosixPath('/github/workspace/profiles')ο
Directory where local profiles are saved
- __init__(name='default', local=True, **kwargs)[source]View on GitHubο
Create a new
Profile
- 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
- user_agent (
- proxy_key (
str
) β Optional Environment variable to retrieve proxies from
- proxy_key (
- 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
- 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_pages(pages, send_tweet=False)[source]View on GitHubο
Add Instagram page(s) to the
page_map
for subsequent monitoringAn 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
listAny new posts from that point forward will be tweeted
You can scrape AND tweet posts on the first run by setting
send_tweet=True
- 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
- 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
, orpage_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_page(page)[source]View on GitHubο
Returns the specified pageβs dict entry in the
page_map
- Return type
- get_scraped_from(page)[source]View on GitHubο
Returns a list of posts that have been scraped from the specified page
- Return type
- get_tweets_for(page)[source]View on GitHubο
Returns a list of tweets that use the specified pageβs scraped content
- Return type
- get_hashtags_for(page)[source]View on GitHubο
Returns the hashtag list for the specified page
- Return type
- 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 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 twitter_keys: Dictο
Twitter API Keys with v1.1 endpoint access (see
DEFAULT_KEYS
for a template)