This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle incorrect Twitter API creds (#7).
- Loading branch information
Showing
2 changed files
with
45 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |