Skip to content

Commit

Permalink
Fix more docstrings
Browse files Browse the repository at this point in the history
Update instaclient.py

Update profile.py
  • Loading branch information
TDKorn committed Jul 19, 2022
1 parent 91ce8af commit 2cb3f86
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
6 changes: 3 additions & 3 deletions InstaTweet/instaclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_user(self, username: str) -> InstaUser:
"""Scrapes an Instagram user's profile and wraps the response
:param username: the username of the IG user to scrape (without the @)
:return: an :class:`InstaUser` object, which wraps the response data
:return: an :class:`~.InstaUser` object, which wraps the response data
"""
response = self.request(f'https://www.instagram.com/{username}/?__a=1&__d=dis')
if response.ok:
Expand Down Expand Up @@ -80,10 +80,10 @@ def download_post(self, post: InstaPost, filepath: str = None) -> bool:

@property
def headers(self) -> dict:
"""Headers to use in :meth:`.~request`"""
"""Headers to use in :meth:`~.request`"""
return {'User-Agent': self.user_agent, }

@property
def cookies(self) -> dict:
"""Cookies to use in :meth:`.~request`"""
"""Cookies to use in :meth:`~.request`"""
return {'sessionid': self.session_id, }
19 changes: 10 additions & 9 deletions InstaTweet/instatweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class InstaTweet:
You might be wondering, what's InstaTweeting? According to TDK Dictionary:
**InstaTweet** (`verb`): to scrape an Instagram account -> download & tweet any new content -> update the loaded :class:`~.Profile`
**InstaTweet** (`verb`):
To scrape an Instagram account -> download & tweet any new content -> update and save the loaded :class:`~.Profile`
**Example**
**Example Sentence**
"Oh, you lost 700 Twitter followers after you shared your IG post? Well maybe if people actually saw the
picture and not just the caption your tweet would've been less creepy. You should've InstaTweeted it.
"Oh, you lost 700 Twitter followers after you shared your IG post? Well maybe if people actually saw the picture
and not just the caption your tweet would've been less creepy. You should've InstaTweeted it.
"""

def __init__(self, profile: Profile):
"""Initializes InstaTweet using a fully configured :class:`Profile`
"""Initializes InstaTweet using a fully configured :class:`~.Profile`
The :class:`Profile` will be used to initialize an :class:`InstaClient` and :class:`TweetClient`
The :class:`Profile` will be used to initialize an :class:`~.InstaClient` and :class:`~.TweetClient`
:Note:
Profile settings will only be validated when calling :meth:`~.start`
Expand Down Expand Up @@ -48,22 +49,22 @@ def get_proxies(self) -> Optional[dict]:
)

def get_insta_client(self) -> InstaClient:
"""Initializes an :class:`InstaClient` using the loaded :class:`Profile` settings"""
"""Initializes an :class:`~.InstaClient` using the loaded :class:`~.Profile` settings"""
return InstaClient(
session_id=self.profile.session_id,
user_agent=self.profile.user_agent,
proxies=self.proxies
)

def get_tweet_client(self) -> TweetClient:
"""Initializes an :class:`TweetClient` using the loaded :class:`Profile` settings"""
"""Initializes an :class:`~.TweetClient` using the loaded :class:`~.Profile` settings"""
return TweetClient(
profile=self.profile,
proxies=self.proxies
)

def start(self) -> None:
"""InstaTweets all users in the :class:`Profile`'s user map
"""InstaTweets all users in the :class:`~.Profile`'s user map
Each user will have their profile scraped, and their posts will be compared to their "scraped" list to determine
if any are new. If there's new posts, the content from them will be downloaded and tweeted
Expand Down
15 changes: 11 additions & 4 deletions InstaTweet/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ class Profile:
LOCAL_DIR = os.path.join(utils.get_root(), 'profiles')

def __init__(self, name: str = 'default', local: bool = True, **kwargs):
"""Create a new profile
"""Create a new :class:`Profile`
A :class:`Profile` contains a ``user_map`` and all API access settings associated with it
The ``user_map`` is a mapping of added Instagram usernames and their associated lists of hashtags,
scraped posts, and sent tweets. The ``scraped`` list is used to determine which posts are new when InstaTweeting
...
The ``user_map`` is a mapping of added Instagram usernames to their associated :attr:`USER_MAPPING`
* The mapping includes a list of hashtags, scraped posts, and sent tweets
* Methods exist to access and modify these lists for a particular user
* Mainly used to help compose tweets and detect when posts are new
...
:param name: unique profile name
:param local: if ``True``, saving will use a file in the :attr:`~.LOCAL_DIR`. Otherwise, will save to a database
:param local: indicates if profile is being saved locally or on a remote database
:param kwargs: see below
:Keyword Arguments:
Expand Down
23 changes: 11 additions & 12 deletions InstaTweet/tweetclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, profile: InstaTweet.Profile, proxies: dict = None):
self.api = self.get_api()

def get_api(self) -> tweepy.API:
"""Initializes a :class:`tweepy.API` (Twitter API v1.1) using the current :class:`Profile`"""
"""Initializes a :class:`tweepy.API` object using the API keys of the loaded :class:`~.Profile`"""
return tweepy.API(
auth=self.get_oauth(self.profile.twitter_keys),
user_agent=self.profile.user_agent,
Expand All @@ -41,7 +41,7 @@ def get_api(self) -> tweepy.API:

@staticmethod
def get_oauth(api_keys: dict) -> tweepy.OAuth1UserHandler:
"""Initializes and returns an ``OAuth1UserHandler`` object from tweepy using the provided API keys
"""Initializes and returns an ``OAuth1UserHandler`` object from tweepy using the specified API keys
:param api_keys: Twitter developer API keys with v1.1 endpoint access
"""
Expand All @@ -65,7 +65,7 @@ def send_tweet(self, post: InstaPost, hashtags: Optional[list[str]] = None) -> b
:param post: the post to tweet; uses the :attr:`~.InstaPost.filepath` as media file source
:param hashtags: a list of hashtags, from the :attr:`~.user_map`
If non-empty, a few will randomly be chosen and included in the tweet
If non-empty, a few will randomly be chosen to include in the tweet
"""
if not post.filepath or not os.path.exists(post.filepath):
raise FileNotFoundError('Post must be downloaded first')
Expand Down Expand Up @@ -105,7 +105,7 @@ def upload_media(self, post: InstaPost) -> Union[tweepy.Media, bool]:
return media

def build_tweet(self, post: InstaPost, hashtags: Optional[list[str]] = None) -> str:
"""Uses an :class:`InstaPost` to build the body text of a tweet
"""Uses an :class:`~.InstaPost` to build the body text of a tweet
:param post: the post that's being tweeted; the caption and link are used
:param hashtags: optional list of hashtags to randomly pick from and include
Expand All @@ -123,19 +123,18 @@ def build_tweet(self, post: InstaPost, hashtags: Optional[list[str]] = None) ->

@staticmethod
def pick_hashtags(hashtags: list[str]) -> str:
"""Randomly chooses hashtags from provided list, then returns them formatted as a string
"""Randomly picks hashtags from the provided list and returns them as a single string
The qty chosen will either be 1 less than the length of the list (to avoid using the same ones in every tweet)
or the value of :attr:`~.MAX_HASHTAGS`, whichever is smaller
The number of hashtags chosen will either be 1 less than the length of the list (to avoid using the same tags
in every tweet), or the value of :attr:`~.MAX_HASHTAGS`, whichever is smaller
:param hashtags: a list of hashtags to randomly choose from and include in a tweet
:param hashtags: a list of hashtags to randomly choose from
:Example:
pick_hashtags(['cat','dog','woof']) -> '#woof #cat\n'
:Note:
A newline is included to make character counting easier for :meth:`~.build_tweet`
>>> TweetClient.pick_hashtags(['cat','dog','woof'])
"#woof #cat\\n"
:Note: A newline is added to help with formatting & character counting in :meth:`~.build_tweet`
"""
if not hashtags:
return ''
Expand Down

0 comments on commit 2cb3f86

Please sign in to comment.