diff --git a/tweeter.py b/tweeter.py index 73a7b3ce57a..1ae534f448e 100644 --- a/tweeter.py +++ b/tweeter.py @@ -1,24 +1,13 @@ -""" -Author: Shreyas Daniel (shreydan) -Install: tweepy - "pip install tweepy" -API: Create a twitter app "apps.twitter.com" to get your OAuth requirements. -Version: 1.0 - -Tweet text and pics directly from the terminal. -""" from __future__ import print_function - import os - import tweepy -try: - input = raw_input -except NameError: - pass +# TODO: Further improvements can be made to the program +# TODO: Further feature improvements and Refactoring can be done to the program +# TODO: Add a README.md file showcasing how adding it to the PATH variable can make the posting much easier -def getStatus(): +def get_status(): lines = [] while True: line = input() @@ -26,57 +15,58 @@ def getStatus(): lines.append(line) else: break - status = "\n".join(lines) - return status + return "\n".join(lines) -def tweetthis(type): - if type == "text": - print("Enter your tweet " + user.name) - tweet = getStatus() - try: - api.update_status(tweet) - except Exception as e: - print(e) - return - elif type == "pic": - print("Enter pic path " + user.name) - pic = os.path.abspath(input()) - print("Enter status " + user.name) - title = getStatus() - try: - api.update_with_media(pic, status=title) - except Exception as e: - print(e) - return +def tweet_text(api, user): + print(f"Enter your tweet, {user.name}:") + tweet = get_status() + try: + api.update_status(tweet) + print("\nTweet posted successfully!") + except tweepy.TweepError as e: + print(f"Error posting tweet: {e}") - print("\n\nDONE!!") +def tweet_picture(api, user): + print(f"Enter the picture path, {user.name}:") + pic = os.path.abspath(input()) + print(f"Enter the status, {user.name}:") + title = get_status() + try: + api.update_with_media(pic, status=title) + print("\nTweet with picture posted successfully!") + except tweepy.TweepError as e: + print(f"Error posting tweet with picture: {e}") -def initialize(): - global api, auth, user - ck = "here" # consumer key - cks = "here" # consumer key SECRET - at = "here" # access token - ats = "here" # access token SECRET + +def initialize_api(): + ck = "your_consumer_key" + cks = "your_consumer_key_secret" + at = "your_access_token" + ats = "your_access_token_secret" auth = tweepy.OAuthHandler(ck, cks) auth.set_access_token(at, ats) - api = tweepy.API(auth) user = api.me() + return api, user def main(): - doit = int(input("\n1. text\n2. picture\n")) - initialize() - if doit == 1: - tweetthis("text") - elif doit == 2: - tweetthis("pic") - else: - print("OK, Let's try again!") - main() + try: + doit = int(input("\n1. Text\n2. Picture\nChoose option (1/2): ")) + api, user = initialize_api() + + if doit == 1: + tweet_text(api, user) + elif doit == 2: + tweet_picture(api, user) + else: + print("Invalid option. Please choose 1 or 2.") + except ValueError: + print("Invalid input. Please enter a valid number.") -main() +if __name__ == "__main__": + main()