This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeepBot.py
216 lines (183 loc) · 7.98 KB
/
KeepBot.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
# -*- coding: UTF-8 -*-
from fbchat import log, Client
from fbchat.models import *
from fbchat.graphql import graphql_color_to_enum
import sqlite3
import json
class KeepBot(Client):
listening = True
default_color = ThreadColor.MESSENGER_BLUE
default_emoji = ''
''' __INIT__ METHOD '''
def __init__(self, email, password, db_file_name='db.sqlite3', max_tries=1, *args, **kwargs):
self.email = email
self.password = password
# SQLITE
self.conn = sqlite3.connect(db_file_name)
self.c = self.conn.cursor()
# Create tables
self.c.executescript('''
CREATE TABLE IF NOT EXISTS login (
id INTEGER PRIMARY KEY,
user_id TEXT UNIQUE,
email TEXT UNIQUE,
password TEXT,
session_cookies TEXT
);
CREATE TABLE IF NOT EXISTS threads (
id INTEGER PRIMARY KEY,
thread_id TEXT UNIQUE,
color TEXT,
emoji TEXT
);
CREATE TABLE IF NOT EXISTS nicknames (
id INTEGER PRIMARY KEY,
thread_id TEXT,
user_id TEXT,
nickname TEXT
);
''')
# Load session_cookies
self.c.execute('SELECT session_cookies FROM login WHERE email = ?', (self.email,))
self.session_cookies = self.c.fetchone()
self.session_cookies = json.loads(self.session_cookies[0]) if self.session_cookies else None
# Login
if self.session_cookies:
Client.__init__(self, email, password, session_cookies=self.session_cookies, max_tries=max_tries, *args, **kwargs)
self.new_session_cookies = self.getSession()
else:
Client.__init__(self, email, password, max_tries=max_tries, *args, **kwargs)
self.new_session_cookies = self.getSession()
# Update session_cookies in DB
if self.new_session_cookies != self.session_cookies:
self.session_cookies = self.new_session_cookies
self.c.execute('''
INSERT OR REPLACE INTO login (
id, user_id, email, password, session_cookies
) VALUES (
(SELECT id FROM LOGIN where user_id = :user_id), :user_id, :email, :password, :session_cookies
)
''', {
'user_id': self.uid,
'email': self.email,
'password': self.password,
'session_cookies': json.dumps(self.session_cookies, ),
})
self.conn.commit()
# Start method listening
if self.listening == True:
self.listen()
''' END __INIT__ METHOD '''
''' GET METHODS '''
def getColor(self, thread_id):
self.c.execute('SELECT color FROM threads WHERE thread_id = ?', (thread_id, ))
color = self.c.fetchone()
if color and color[0]:
color = graphql_color_to_enum(" " + color[0])
return color
else:
self.updateColor(thread_id, self.default_color)
return self.default_color
def getEmoji(self, thread_id):
self.c.execute('SELECT emoji FROM threads WHERE thread_id = ?', (thread_id, ))
emoji = self.c.fetchone()
if emoji and emoji[0]:
emoji = emoji[0]
if emoji == None:
emoji = ""
return emoji
else:
self.updateEmoji(thread_id, self.default_emoji)
return self.default_emoji
def getNickname(self, thread_id, user_id):
self.c.execute('SELECT nickname FROM nicknames WHERE thread_id = ? AND user_id = ?', (thread_id, user_id, ))
nickname = self.c.fetchone()
if nickname:
nickname = nickname[0]
return nickname
else:
self.updateNickname(thread_id, user_id, "")
return ""
''' END GET METHODS '''
''' UPDATE METHODS '''
def updateColor(self, thread_id, color):
self.c.execute('''
INSERT OR REPLACE INTO threads (
id,
thread_id,
color,
emoji
) VALUES (
(SELECT id FROM threads where thread_id = :thread_id),
:thread_id,
:color,
(SELECT emoji FROM threads where thread_id = :thread_id)
)
''', {
'thread_id': thread_id,
'color': color.value if color != ThreadColor.MESSENGER_BLUE else ''
})
self.conn.commit()
def updateEmoji(self, thread_id, emoji):
self.c.execute('''
INSERT OR REPLACE INTO threads (
id,
thread_id,
color,
emoji
) VALUES (
(SELECT id FROM threads where thread_id = :thread_id),
:thread_id,
(SELECT color FROM threads where thread_id = :thread_id),
:emoji
)
''', {
'thread_id': thread_id,
'emoji': emoji,
})
self.conn.commit()
def updateNickname(self, thread_id, user_id, nickname):
self.c.execute('''
INSERT OR REPLACE INTO nicknames (
id,
thread_id, user_id, nickname
) VALUES (
(SELECT id FROM nicknames WHERE thread_id = :thread_id AND user_id = :user_id),
:thread_id, :user_id, :nickname
)
''', {
'thread_id': thread_id,
'user_id': user_id,
'nickname': nickname
})
self.conn.commit()
''' END UPDATE METHODS '''
''' ON CHANGE METHODS '''
def onColorChange(self, author_id, new_color, thread_id, thread_type, **kwargs):
old_color = self.getColor(thread_id)
if old_color != new_color and thread_type == ThreadType.USER:
if author_id == self.uid:
log.info("{} changed the thread color to {}".format(author_id, new_color))
self.updateColor(thread_id, new_color)
else:
log.info("{} changed the thread color to {}. It will be changed back to {}".format(author_id, new_color, old_color))
self.changeThreadColor(old_color, thread_id=thread_id)
def onEmojiChange(self, author_id, new_emoji, thread_id, thread_type, **kwargs):
old_emoji = self.getEmoji(thread_id)
if old_emoji != new_emoji and thread_type == ThreadType.USER:
if author_id == self.uid:
log.info("{} changed the thread emoji to {}".format(author_id, new_emoji))
self.updateEmoji(thread_id, new_emoji)
else:
log.info("{} changed the thread emoji to {}. It will be changed back to {}".format(author_id, new_emoji, old_emoji))
self.changeThreadEmoji(old_emoji, thread_id=thread_id)
def onNicknameChange(self, author_id, changed_for, new_nickname, thread_id, thread_type, **kwargs):
old_nickname = self.getNickname(thread_id, changed_for)
if old_nickname != new_nickname and (thread_type == ThreadType.USER or (thread_type == ThreadType.GROUP and (changed_for == self.uid or len(self.fetchGroupInfo(thread_id)[thread_id].participants) == 2 ))):
if author_id == self.uid:
log.info("{} changed {}'s nickname to {}.".format(author_id, changed_for, new_nickname))
self.updateNickname(thread_id, changed_for, new_nickname)
else:
log.info("{} changed {}'s nickname to {}.It will be changed back to {}.".format(author_id, changed_for, new_nickname, old_nickname))
self.changeNickname(old_nickname, changed_for, thread_id=thread_id, thread_type=thread_type)
''' END ON CHANGE METHODS '''