Skip to content

Commit dd10333

Browse files
refactor: Better readability and logic and feature to be implemented update.
1 parent 6089b11 commit dd10333

File tree

1 file changed

+44
-54
lines changed

1 file changed

+44
-54
lines changed

tweeter.py

+44-54
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,72 @@
1-
"""
2-
Author: Shreyas Daniel (shreydan)
3-
Install: tweepy - "pip install tweepy"
4-
API: Create a twitter app "apps.twitter.com" to get your OAuth requirements.
5-
Version: 1.0
6-
7-
Tweet text and pics directly from the terminal.
8-
"""
91
from __future__ import print_function
10-
112
import os
12-
133
import tweepy
144

15-
try:
16-
input = raw_input
17-
except NameError:
18-
pass
5+
# TODO: Further improvements can be made to the program
6+
# TODO: Further feature improvements and Refactoring can be done to the program
7+
# TODO: Add a README.md file showcasing how adding it to the PATH variable can make the posting much easier
198

209

21-
def getStatus():
10+
def get_status():
2211
lines = []
2312
while True:
2413
line = input()
2514
if line:
2615
lines.append(line)
2716
else:
2817
break
29-
status = "\n".join(lines)
30-
return status
18+
return "\n".join(lines)
3119

3220

33-
def tweetthis(type):
34-
if type == "text":
35-
print("Enter your tweet " + user.name)
36-
tweet = getStatus()
37-
try:
38-
api.update_status(tweet)
39-
except Exception as e:
40-
print(e)
41-
return
42-
elif type == "pic":
43-
print("Enter pic path " + user.name)
44-
pic = os.path.abspath(input())
45-
print("Enter status " + user.name)
46-
title = getStatus()
47-
try:
48-
api.update_with_media(pic, status=title)
49-
except Exception as e:
50-
print(e)
51-
return
21+
def tweet_text(api, user):
22+
print(f"Enter your tweet, {user.name}:")
23+
tweet = get_status()
24+
try:
25+
api.update_status(tweet)
26+
print("\nTweet posted successfully!")
27+
except tweepy.TweepError as e:
28+
print(f"Error posting tweet: {e}")
5229

53-
print("\n\nDONE!!")
5430

31+
def tweet_picture(api, user):
32+
print(f"Enter the picture path, {user.name}:")
33+
pic = os.path.abspath(input())
34+
print(f"Enter the status, {user.name}:")
35+
title = get_status()
36+
try:
37+
api.update_with_media(pic, status=title)
38+
print("\nTweet with picture posted successfully!")
39+
except tweepy.TweepError as e:
40+
print(f"Error posting tweet with picture: {e}")
5541

56-
def initialize():
57-
global api, auth, user
58-
ck = "here" # consumer key
59-
cks = "here" # consumer key SECRET
60-
at = "here" # access token
61-
ats = "here" # access token SECRET
42+
43+
def initialize_api():
44+
ck = "your_consumer_key"
45+
cks = "your_consumer_key_secret"
46+
at = "your_access_token"
47+
ats = "your_access_token_secret"
6248

6349
auth = tweepy.OAuthHandler(ck, cks)
6450
auth.set_access_token(at, ats)
65-
6651
api = tweepy.API(auth)
6752
user = api.me()
53+
return api, user
6854

6955

7056
def main():
71-
doit = int(input("\n1. text\n2. picture\n"))
72-
initialize()
73-
if doit == 1:
74-
tweetthis("text")
75-
elif doit == 2:
76-
tweetthis("pic")
77-
else:
78-
print("OK, Let's try again!")
79-
main()
57+
try:
58+
doit = int(input("\n1. Text\n2. Picture\nChoose option (1/2): "))
59+
api, user = initialize_api()
60+
61+
if doit == 1:
62+
tweet_text(api, user)
63+
elif doit == 2:
64+
tweet_picture(api, user)
65+
else:
66+
print("Invalid option. Please choose 1 or 2.")
67+
except ValueError:
68+
print("Invalid input. Please enter a valid number.")
8069

8170

82-
main()
71+
if __name__ == "__main__":
72+
main()

0 commit comments

Comments
 (0)