Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Handle incorrect Twitter API creds (#7).
Browse files Browse the repository at this point in the history
  • Loading branch information
soup-bowl committed Jan 11, 2022
1 parent 5100c7d commit 5933e57
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
25 changes: 15 additions & 10 deletions htw/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ def process_user(self, user_conf):
if not self.suppress:
print("- Posting to \033[96mTwitter\033[00m...")

post_to_twitter(
tweet,
pic,
self.twitter_key,
self.twitter_srt,
user_conf['twitterAccessToken'],
user_conf['twitterAccessSecret']
)
colgen.cleanup()
return True
try:
post_to_twitter(
tweet,
pic,
self.twitter_key,
self.twitter_srt,
user_conf['twitterAccessToken'],
user_conf['twitterAccessSecret']
)
return True
except Exception as e:
print("\033[91mError\033[00m: " + str(e) + ".")
return False
finally:
colgen.cleanup()

def read_config(self, location):
conf = json.loads( Path( location ).read_text() )
Expand Down
56 changes: 30 additions & 26 deletions htw/twitter.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
from twython import Twython
from twython import Twython, TwythonError

def compose_tweet(lfm_collection, name):
"""Compose tweet message contents.
"""Compose tweet message contents.
Args:
lfm_collection ([type]): Last.fm user data collection.
name (str): Last.fm username.
Args:
lfm_collection ([type]): Last.fm user data collection.
name (str): Last.fm username.
Returns:
[str]: Message contents.
"""
message = "\U0001F4BF my week with #lastfm:\n"
for artist in lfm_collection:
message = message + "%s (%s)\n" % (artist['name'], artist['plays'])
message = message + "https://www.last.fm/user/%s" % name
return message
Returns:
[str]: Message contents.
"""
message = "\U0001F4BF my week with #lastfm:\n"
for artist in lfm_collection:
message = message + "%s (%s)\n" % (artist['name'], artist['plays'])
message = message + "https://www.last.fm/user/%s" % name
return message

def post_to_twitter(tweet, picture, consumer_key, consumer_secret, access_key, access_secret):
"""Posts a message to the Twitter platform.
"""Posts a message to the Twitter platform.
Args:
tweet (str): Message contents.
picture (str): Filesystem location of the collage to attach.
consumer_key (str): Twitter application credentials.
consumer_secret (str): Twitter application credentials.
access_key (str): Individual user Twitter credentials.
access_secret (str): Individual user Twitter credentials.
"""
twitter = Twython(consumer_key, consumer_secret, access_key, access_secret)
collage = open(picture, 'rb')
Args:
tweet (str): Message contents.
picture (str): Filesystem location of the collage to attach.
consumer_key (str): Twitter application credentials.
consumer_secret (str): Twitter application credentials.
access_key (str): Individual user Twitter credentials.
access_secret (str): Individual user Twitter credentials.
"""

response = twitter.upload_media(media=collage)
twitter.update_status(status=tweet, media_ids=[response['media_id']])
twitter = Twython(consumer_key, consumer_secret, access_key, access_secret)
collage = open(picture, 'rb')

try:
response = twitter.upload_media(media=collage)
twitter.update_status(status=tweet, media_ids=[response['media_id']])
except TwythonError as e:
raise Exception("Twitter responded with an error code: " + str(e.error_code))

0 comments on commit 5933e57

Please sign in to comment.