-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatherCord.py
330 lines (298 loc) · 12.2 KB
/
featherCord.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import argparse
import concurrent.futures
import signal
import os
import ssl
import sqlite3
import sys
import time
import threading
import json
import discord
from discord.ext import commands, tasks
from tweety import TwitterAsync
ssl._create_default_https_context = ssl._create_unverified_context
intents = discord.Intents.default()
try:
intents.message_content = True
except Exception:
pass
Bot = commands.Bot(command_prefix="!", intents=intents, help_command=None)
task_data = []
stopped = [False]
def connect_db(user_id: str='', password: str='', token: str=''):
if not os.path.exists(os.path.join(os.getcwd(), '.setting_twitter')):
os.makedirs(os.path.join(os.getcwd(), '.setting_twitter'), exist_ok=True)
if not os.path.exists(os.path.join(os.getcwd(), '.setting_twitter', 'loginInfo.db')):
_connect = sqlite3.connect(os.path.join(os.getcwd(), '.setting_twitter', 'loginInfo.db'))
_cur = _connect.cursor()
_cur.execute('CREATE TABLE LoginInfomation(User STRING, Password STRING, TOKEN STRING)')
_cur.execute('INSERT INTO LoginInfomation(User, Password, TOKEN) values("{}", "{}", "{}")'.format(user_id, password, token))
_connect.commit()
user = user_id
passwd = password
_token = token
else:
_connect = sqlite3.connect(os.path.join(os.getcwd(), '.setting_twitter', 'loginInfo.db'))
_cur = _connect.cursor()
_cur.execute('SELECT * FROM LoginInfomation')
user, passwd, _token = _cur.fetchall()[0]
if user_id != '' and password != '':
_cur.execute('REPLACE INTO LoginInfomation(User, Password) values("{}", "{}")'.format(user_id, password))
if user_id != '' and password != '' and token != '':
_cur.execute('REPLACE INTO LoginInfomation(User, Password, TOKEN) values("{}", "{}", "{}")'.format(user_id, password, token))
elif token != '':
_cur.execute('REPLACE INTO LoginInfomation(TOKEN) values("{}")'.format(token))
_cur.close()
_connect.close()
return user, passwd, _token
class Tweeter(object):
def __init__(self):
asyncio.run(self._login())
async def _login(self):
self.app = TwitterAsync('session')
username, password, _ = connect_db()
await self.app.sign_in(username, password)
async def new_tweet(self, user):
try:
for tweet in await self.app.get_tweets(username=user, pages=1, replies=False, wait_time=3):
return tweet.url
except Exception as Err:
Err = '{}'.format(Err)
if '[88]' in Err:
return 'rate limited please, retry again.'
else:
print(Err)
return 'no get tweet'
class TweetDiscord(commands.Cog):
def __init__(self):
self.twitter = Tweeter()
@commands.slash_command(name="recovery_set_tweet", description="recovery monitoring set account posts")
async def recovery_set_tweet(self, cx: discord.commands.context.ApplicationContext):
def recover_set_tweet(cxx: discord.TextChannel, username: str = ''):
_urls = []
task = tasks.loop(seconds=57)(self.auto_refresh_for_new_tweet)
task_data.append({"username": username, "task_list": task})
task.start(username, cxx, _urls)
if os.path.exists(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json')):
with open(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json'), 'r', encoding='utf-8') as scl:
channel_jsn = json.load(scl)
for channel in cx.guild.channels:
try:
recover_set_tweet(channel, channel_jsn['{}'.format(channel.id)])
except KeyError:
continue
except Exception as Err:
print(Err)
try:
await cx.response.send_message(content='all monitoring account was setting', ephemeral=True)
except:
pass
else:
try:
await cx.response.send_message(content='error: setting file not found', ephemeral=True)
except:
pass
@commands.slash_command(name="delete_json", description="delete seting file")
async def delete_json(self, cx: discord.commands.context.ApplicationContext):
try:
os.remove(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json'))
except:
pass
try:
await cx.response.send_message(content='done', ephemeral=True)
except:
pass
@commands.slash_command(name="set_tweet", description="monitoring set account posts")
async def set_tweet(self, cx: discord.commands.context.ApplicationContext, username: str = ''):
if os.path.exists(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json')):
with open(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json'), 'r', encoding='utf-8') as scl:
channel_jsn = json.load(scl)
else:
channel_jsn = dict()
channel_jsn['{}'.format(cx.channel_id)] = username
with open(os.path.join(os.getcwd(), '.setting_twitter', 'set_channel.json'), 'w', encoding='utf-8') as jsn_w:
json.dump(channel_jsn, jsn_w, ensure_ascii=False, indent=4)
try:
await cx.response.send_message(content='set UserName: {}'.format(username), ephemeral=True)
except:
pass
_urls = []
task = tasks.loop(seconds=57)(self.auto_refresh_for_new_tweet)
task_data.append({"username": username, "task_list": task})
task.start(username, cx, _urls)
async def auto_refresh_for_new_tweet(self, user, cx, _urls: list):
def string_detect(string_text: str) -> bool:
len_text = 0
for strings in _urls:
if strings == string_text:
len_text += 1
if 1 <= len_text:
return False
else:
return True
now_url = await self.twitter.new_tweet(user)
if string_detect(now_url):
_urls.append(now_url)
try:
if now_url.split('/')[2] == 'x.com':
now_url = 'fxtwitter.com'.join(now_url.split('x.com'))
else:
now_url = 'fxtwitter.com'.join(now_url.split('twitter.com'))
if now_url.split('/')[2][0:4] == 'fxfx':
now_url = now_url.replace(now_url.split('/')[2], 'fxtwitter.com')
await cx.send(content=now_url)
except IndexError:
try:
now_url = 'fxtwitter.com'.join(now_url.split('twitter.com'))
if now_url.split('/')[2][0:4] == 'fxfx':
now_url = now_url.replace(now_url.split('/')[2], 'fxtwitter.com')
await cx.send(content=now_url)
except:
pass
except:
pass
_urls[0:] = list(set(_urls))
@commands.slash_command(name="set_stop", description="stop monitoring account.")
async def set_stop(self, cx: discord.ApplicationContext, user_name):
for _json in task_data:
if _json["username"] == user_name:
try:
_json["task_list"].stop()
except:
pass
try:
_json["task_list"].cancel()
except:
pass
try:
await cx.delete()
except:
pass
@commands.slash_command(name="get_tweet", description="get new post")
async def get_tweet(self, cx: discord.ApplicationContext, username: str = ''):
now_url = await self.twitter.new_tweet(username)
try:
if now_url.split('/')[2] == 'x.com':
now_url = 'fxtwitter.com'.join(now_url.split('x.com'))
else:
now_url = 'fxtwitter.com'.join(now_url.split('twitter.com'))
if now_url.split('/')[2][0:4] == 'fxfx':
now_url = now_url.replace(now_url.split('/')[2], 'fxtwitter.com')
except IndexError:
try:
now_url = 'fxtwitter.com'.join(now_url.split('twitter.com'))
if now_url.split('/')[2][0:4] == 'fxfx':
now_url = now_url.replace(now_url.split('/')[2], 'fxtwitter.com')
except:
pass
except:
pass
if now_url != 'no get tweet':
await cx.response.send_message(content=now_url, ephemeral=True)
else:
await cx.response.send_message(content='no get post', ephemeral=True)
@commands.slash_command(name="stop_all", description="shutdown bot")
async def stop_all(self, cx: discord.ApplicationContext):
await cx.delete()
print('Bot is Stopped!')
stopped[0] = True
await self.exits()
signal.signal(signal.Signals.SIGKILL, signal.Signals.SIGINT)
async def exits(self):
sys.exit(0)
@Bot.event
async def on_ready():
await Bot.change_presence(activity=discord.Game('Bot is Started!(v0.0.1)'))
def TimeCount():
Uptimeloop = [0]
def TimeCounter():
Year = 0
Week = 0
Day = 0
Hour = 0
Minute = 0
Sec = 0
for i in Uptimeloop:
if stopped[0]:
break
if Sec == 59:
Sec = 0
Minute += 1
else:
Sec += 1
if Minute == 59:
Minute = 0
Hour += 1
if Hour == 24:
Hour = 0
Day += 1
if Day == 7:
Day = 0
Week += 1
if Week == 13:
Week = 0
Year += 1
if Year <= 9:
SYear = '0{}'.format(Year)
else:
SYear = '{}'.format(Year)
if Week <= 9:
SWeek = '0{}'.format(Week)
else:
SWeek = '{}'.format(Week)
if Day <= 9:
SDay = '0{}'.format(Day)
else:
SDay = '{}'.format(Day)
if Hour <= 9:
SHour = '0{}'.format(Hour)
else:
SHour = '{}'.format(Hour)
if Minute <= 9:
SMinute = '0{}'.format(Minute)
else:
SMinute = '{}'.format(Minute)
if Sec <= 9:
SSec = '0{}'.format(Sec)
else:
SSec = '{}'.format(Sec)
print('Uptime: {}year, {}week, {}day, {}:{}:{}'.format(SYear, SWeek, SDay, SHour, SMinute, SSec), end='\r', flush=True)
time.sleep(1)
Uptimeloop.append(i + 1)
concurrent.futures.ThreadPoolExecutor().submit(TimeCounter)
def main():
ArgumentPaerser = argparse.ArgumentParser(description='TweetDiscord')
ArgumentPaerser.add_argument('--reset-login', '-rl', action='store_true', help='reset login data for twitter.')
ArgumentPaerser.add_argument('--reset-token', '-rt', action='store_true', help='reset discord token')
ArgumentPaerser.add_argument('--remove-all', '-ra', action='store_true', help='all account data delete')
arg = ArgumentPaerser.parse_args()
if arg.reset_login:
print('reset login data')
connect_db(user_id=input('Twitter(X) UserName: '), password=input('Twitter(X) Password: '))
if arg.reset_token:
print('reset token')
connect_db(token=input('discard token: '))
if arg.remove_all:
print('delete all account data...')
try:
shutil.rmtree(os.path.join(os.getcwd(), '.setting_twitter'))
print('done!')
except:
print('error')
if not os.path.exists(os.path.join(os.getcwd(), '.setting_twitter', 'loginInfo.db')):
connect_db(user_id=input('Twitter(X) UserName: '), password=input('Twitter(X) Password: '), token=input('discard token: '))
if not arg.reset_login and not arg.reset_token and not arg.remove_all:
_, __, TOKEN = connect_db()
print('BOT Starting...')
TimeCount()
Bot.add_cog(TweetDiscord())
Bot.run(TOKEN)
if __name__ == '__main__':
try:
main()
except OSError:
pass