|
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 |
| -""" |
9 | 1 | from __future__ import print_function
|
10 |
| - |
11 | 2 | import os
|
12 |
| - |
13 | 3 | import tweepy
|
14 | 4 |
|
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 |
19 | 8 |
|
20 | 9 |
|
21 |
| -def getStatus(): |
| 10 | +def get_status(): |
22 | 11 | lines = []
|
23 | 12 | while True:
|
24 | 13 | line = input()
|
25 | 14 | if line:
|
26 | 15 | lines.append(line)
|
27 | 16 | else:
|
28 | 17 | break
|
29 |
| - status = "\n".join(lines) |
30 |
| - return status |
| 18 | + return "\n".join(lines) |
31 | 19 |
|
32 | 20 |
|
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}") |
52 | 29 |
|
53 |
| - print("\n\nDONE!!") |
54 | 30 |
|
| 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}") |
55 | 41 |
|
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" |
62 | 48 |
|
63 | 49 | auth = tweepy.OAuthHandler(ck, cks)
|
64 | 50 | auth.set_access_token(at, ats)
|
65 |
| - |
66 | 51 | api = tweepy.API(auth)
|
67 | 52 | user = api.me()
|
| 53 | + return api, user |
68 | 54 |
|
69 | 55 |
|
70 | 56 | 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.") |
80 | 69 |
|
81 | 70 |
|
82 |
| -main() |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments