-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.py
executable file
·137 lines (120 loc) · 3.8 KB
/
tweet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import division
##
# Originally derived from a script by Benjamin D. McGinnes, licensed
# under the Apache 2.0 license
#
# Requirements:
#
# * Python 3.4 or later.
# Usage:
# tweet.py --message "Thing to tweet" --file /path/to/file/to/tweet
##
import os
import os.path
import sys
import argparse
from twython import Twython, TwythonError
APP_KEY = ""
APP_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
cred = twitter.verify_credentials()
l = len(sys.argv)
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-f', '--file', action='store',dest='media_fn', nargs='+')
parser.add_argument('-t', '--tweet', action='store',dest='message', nargs='+')
args = parser.parse_args()
print ('Media file is ', args.media_fn)
print ('Message is ', args.message)
reply_id = None
twid = None
message = args.message
media_fn = args.media_fn
if media_fn is not None:
mfiles = media_fn[0].split()
lm = len(mfiles)
mfid = []
for i in range(lm):
if os.path.isfile(os.path.realpath(mfiles[i])) is True:
mediaf = os.path.realpath(mfiles[i])
elif os.path.isfile(os.path.realpath("InputFiles/{0}".format(mfiles[i]))) is True:
mediaf = os.path.realpath("InputFiles/{0}".format(mfiles[i]))
else:
mediaf = None
if mediaf is None:
mfid.append(mediaf)
else:
mf = open(mediaf, "rb")
response = twitter.upload_media(media=mf)
mfid.append(response["media_id"])
else:
mfid = None
if len(message) < 1 and twid is None and mfid is None:
mesg = None
elif len(message) < 1 and twid is None and mfid is not None:
mesg = "."
elif len(message) < 1 and twid is not None and mfid is not None:
users = []
hashtags = []
try:
tweet = twitter.show_status(id=twid)
user1 = "@"+tweet["user"]["screen_name"]
users.append(user1)
rtweet = tweet["text"]
rtword = rtweet.split()
for i in range(len(rtword)):
if rtword[i].startswith("@") is True:
users.append(rtword[i])
elif rtword[i].startswith("#") is True:
hashtags.append(rtword[i])
else:
pass
ustr = " ".join(users)
hstr = " ".join(hashtags)
mesg = "{0} {1}".format(ustr, hstr)
except TwythonError as e:
print(e)
mesg = "."
else:
mesg = message
if mesg is not None and twid is None and mfid is None:
try:
twitter.update_status(status=mesg)
except TwythonError as e:
print(e)
elif mesg is not None and twid is not None and mfid is None:
try:
twitter.update_status(status=mesg, in_reply_to_status_id=twid)
except TwythonError as e:
print(e)
elif mesg is not None and twid is None and mfid is not None:
try:
twitter.update_status(status=mesg, media_ids=mfid)
except TwythonError as e:
print(e)
elif mesg is not None and twid is not None and mfid is not None:
try:
twitter.update_status(status=mesg, media_ids=mfid,
in_reply_to_status_id=twid)
except TwythonError as e:
print(e)
elif mesg is None and twid is None and mfid is not None:
try:
twitter.update_status(status="", media_ids=mfid)
except TwythonError as e:
print(e)
elif mesg is None and twid is not None and mfid is not None:
try:
twitter.update_status(status="", media_ids=mfid,
in_reply_to_status_id=twid)
except TwythonError as e:
print(e)
else:
print("""
As with all things in this world, you get out of it what you put in
and you put in nothing.
""")