forked from YoloSwagTeam/t2m
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht2m
executable file
·267 lines (195 loc) · 8.81 KB
/
t2m
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import json
import time
import shutil
import tempfile
import codecs
try:
from urllib import urlretrieve
except ImportError:
from urllib.request import urlretrieve
from getpass import getpass
import yaml
import argh
import twitter
from mastodon import Mastodon
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
def get_db():
if os.path.exists("db.json"):
return json.load(open("db.json", "r"))
return {}
def save_db(db):
json.dump(db, open("db.json", "w"), indent=4)
def ensure_client_existe_for_instance(instance):
if not os.path.exists("./t2m_%s_clientcred.txt" % instance):
Mastodon.create_app('t2m',
to_file='t2m_%s_clientcred.txt' % instance,
api_base_url='https://%s' % instance,
)
def forward(db, twitter_handle, mastodon_handle, debug, number=None, only_mark_as_seen=False, retweets=False):
t = twitter.Api(tweet_mode='extended', **yaml.safe_load(open("conf.yaml")))
if not "@" in mastodon_handle:
print("ERROR: we now handle multi-instances, you need to update the mastodon handle '%s' by doing './t2m add %s %[email protected]'" % (mastodon_handle, twitter_handle, mastodon_handle))
sys.exit(1)
mastodon_nick, instance = mastodon_handle.split("@", 1)
ensure_client_existe_for_instance(instance)
mastodon_creds = "t2m_%s_creds.txt" % mastodon_handle
if not os.path.exists(mastodon_creds):
mastodon = Mastodon(client_id='./t2m_%s_clientcred.txt' % instance, api_base_url="https://%s" % instance)
print("Not credentials for mastodon account '%s', creating them (the password will NOT be saved)" % mastodon_handle)
mastodon.log_in(
argh.io.safe_input("Email for mastodon account '%s': " % mastodon_handle).strip(),
getpass("Password for mastodon account of '%s' (won't be stored): " % mastodon_handle),
to_file=mastodon_creds,
)
mastodon = Mastodon(client_id='./t2m_%s_clientcred.txt' % instance, access_token=mastodon_creds, api_base_url='https://%s' % instance)
to_toot = []
if retweets:
with codecs.open("retweet.tmpl", encoding="utf-8") as f:
retweet_template = f.read()
# select tweets first
for i in reversed(t.GetUserTimeline(screen_name=twitter_handle, count=200)):
retweeted_status = i.retweeted_status
if not retweeted_status:
retweeted_status = i.quoted_status
if retweeted_status:
if retweets:
text = retweet_template % {
"text": retweeted_status.full_text,
"user": retweeted_status.user.screen_name,
"id": retweeted_status.id
}
if i.quoted_status:
text = i.full_text + "\n\n" + text
urls = retweeted_status.urls
media = retweeted_status.media
else:
continue
else:
# do not forward pseudo-private answer for now
if i.full_text.startswith("@"):
continue
text = i.full_text
urls = i.urls
media = i.media
# do not forward already forwarded tweets
if i.id in db.get(twitter_handle, {}).setdefault("done", []):
continue
# remove this t.co crap
for url in urls:
text = text.replace(url.url, url.expanded_url)
if not only_mark_as_seen:
h = HTMLParser()
to_toot.append({
"text": h.unescape(text),
"id": i.id,
"medias": [x.media_url for x in media] if media else []
})
if only_mark_as_seen:
db.setdefault(twitter_handle, {}).setdefault("done", []).append(i.id)
# slices selected tweets if specified
if number is not None:
to_toot = to_toot[-int(number):]
tmp_dir = tempfile.mkdtemp()
forwarded = 0
# actually forward
if not only_mark_as_seen:
for toot in to_toot:
if debug:
print(">>", toot["text"].encode("Utf-8"), " ".join(toot["medias"]))
else:
try:
medias = []
for number, media_url in enumerate(toot["medias"]):
dl_file_path = os.path.join(tmp_dir, str(number) + "." + media_url.split(".")[-1])
urlretrieve(media_url, dl_file_path)
medias.append((mastodon.media_post(dl_file_path))["id"])
response = mastodon.status_post(toot["text"], media_ids=medias)
assert not response.get("error"), response
forwarded += 1
except Exception as e:
import traceback
traceback.print_exc()
print("ERROR: could not forward the twitt [%s] '%s' because '%s', skipping for now" % (toot["id"], toot["text"], e))
continue
print("[forwarding] >>", toot["text"].encode("Utf-8"), " ".join(toot["medias"]))
db.setdefault(twitter_handle, {}).setdefault("done", []).append(toot["id"])
save_db(db)
time.sleep(30)
if only_mark_as_seen:
print("Mark all available tweets as seen")
elif not to_toot:
print("Nothing to do for %s" % twitter_handle)
else:
print("Forwarded %s tweets from %s to %s" % (forwarded, twitter_handle, mastodon_handle))
shutil.rmtree(tmp_dir, ignore_errors=True)
return db
def one(twitter_handle, mastodon_handle=None, number=None, only_mark_as_seen=False, debug=False, retweets=False):
db = get_db()
if mastodon_handle is None and twitter_handle not in db:
print("Error: I don't have an associated mastodon account for '%s', please provide one to me with -m" % twitter_handle)
sys.exit(1)
if mastodon_handle is None and twitter_handle in db:
mastodon_handle = db[twitter_handle]["mastodon"]
if not "@" in mastodon_handle:
print("ERROR: we now handle multi-instances, you need to update the mastodon handle by doing './t2m add %s %[email protected]'" % (twitter_handle, mastodon_handle))
sys.exit(1)
if "@" not in mastodon_handle:
print("ERROR: you should add the mastodon instance to the mastodon handle this way: %[email protected]" % (mastodon_handle))
sys.exit(1)
# force set new mastodon handle
db.setdefault(twitter_handle, {})["mastodon"] = mastodon_handle
db = forward(db, twitter_handle, mastodon_handle, debug=debug, number=number, only_mark_as_seen=only_mark_as_seen, retweets=retweets)
save_db(db)
def all(debug=False, retweets=False):
db = get_db()
for twitter_handle in db:
if not db[twitter_handle].get("mastodon"):
print("WARNING: not mastodon handle for twitter account '%s', add one using './t2m add' command" % (twitter_handle))
continue
db = forward(db, twitter_handle, db[twitter_handle]["mastodon"], debug, retweets=retweets)
save_db(db)
def add(twitter_handle, mastodon_handle):
db = get_db()
if "@" not in mastodon_handle:
print("ERROR: you should add the mastodon instance to the mastodon handle this way: %[email protected]" % (mastodon_handle))
sys.exit(1)
mastodon_creds = "t2m_%s_creds.txt" % mastodon_handle
# retrocompatibility
if os.path.exists("t2m_%s_creds.txt" % mastodon_handle.split("@")[0]):
shutil.move("t2m_%s_creds.txt" % mastodon_handle.split("@")[0], mastodon_creds)
mastodon_nick, instance = mastodon_handle.split("@", 1)
ensure_client_existe_for_instance(instance)
if not os.path.exists(mastodon_creds):
mastodon = Mastodon(client_id='./t2m_clientcred.txt', api_base_url="https://%s" % instance)
print("Grabbing credentials for mastodon handle (password will NOT be stored")
mastodon.log_in(
argh.io.safe_input("Email for mastodon account '%s': " % mastodon_handle).strip(),
getpass("Password for mastodon account of '%s': " % mastodon_handle),
to_file=mastodon_creds,
)
if twitter_handle in db:
db[twitter_handle]["mastodon"] = mastodon_handle
else:
db[twitter_handle] = {
"mastodon": mastodon_handle
}
save_db(db)
print("done")
def list():
db = get_db()
for i in db:
print(" *", i)
parser = argh.ArghParser()
parser.add_commands([one, all, add, list])
if __name__ == '__main__':
if not os.path.exists("./conf.yaml"):
print("You need to have a conf.yaml file containing twitter connection informations, please read the documentation https://github.com/Psycojoker/t2m#installation")
sys.exit(1)
parser.dispatch()