-
Notifications
You must be signed in to change notification settings - Fork 7
/
ChaturbateBot.py
236 lines (236 loc) · 9.38 KB
/
ChaturbateBot.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
# -*- coding: utf-8 -*-
import telebot
import os
import time
import urllib.request
import os.path
import argparse
import sqlite3
import threading
from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession
ap = argparse.ArgumentParser()
ap.add_argument("-k", "--key", required=True,type=str,
help="Telegram bot key")
ap.add_argument("-f", "--working-folder", required=False,type=str,default=os.getcwd(),
help="set the bot's working-folder")
ap.add_argument("-t", "--time", required=False,type=int,default=10,
help="time wait between every end of the check_online_status thread")
ap.add_argument("-threads",required=False,type=int,default=10,help="The number of multiple http connection opened at the same to check chaturbate")
ap.add_argument("-l","--limit",required=False,type=int,default=0,help="The maximum number of multiple users a person can follow")
ap.add_argument("-raven",required=False,type=str,default="",help="Raven client key")
args = vars(ap.parse_args())
bot = telebot.TeleBot(args["key"])
bot_path=args["working_folder"]
wait_time=args["time"]
raven_key=args["raven"]
http_threads=args["threads"]
user_limit=args["limit"]
if raven_key!="":
from raven import Client
client = Client(raven_key)
def handle_exception(e):
client.captureException()
else:
def handle_exception(e):
print(str(e))
def risposta(sender, messaggio):
try:
bot.send_chat_action(sender, action="typing")
bot.send_message(sender, messaggio)
except Exception as e:
handle_exception(e)
def risposta_html(sender, messaggio):
try:
bot.send_chat_action(sender, action="typing")
bot.send_message(sender, messaggio, parse_mode="HTML")
except Exception as e:
handle_exception(e)
def exec_query(query):
# Open database connection
db = sqlite3.connect(bot_path+'/database.db')
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
try:
# Execute the SQL command
cursor.execute(query)
# Commit your changes in the database
db.commit()
except Exception as e:
# Rollback in case there is any error
handle_exception(e)
db.rollback()
# disconnect from server
db.close()
#default table creation
exec_query("""CREATE TABLE IF NOT EXISTS CHATURBATE (
USERNAME CHAR(60) NOT NULL,
CHAT_ID CHAR(100),
ONLINE CHAR(1))""")
def check_online_status():
while(1):
username_list=[]
chatid_list=[]
online_list=[]
response_list=[]
sql = "SELECT * FROM CHATURBATE"
try:
db = sqlite3.connect(bot_path+'/database.db')
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
username_list.append(row[0])
chatid_list.append(row[1])
online_list.append(row[2])
except Exception as e:
handle_exception(e)
finally:
db.close()
session = FuturesSession(executor=ThreadPoolExecutor(max_workers=http_threads))
for x in range(0,len(username_list)):
try:
response = ((session.get("https://it.chaturbate.com/api/chatvideocontext/"+username_list[x])).result()).content
except Exception as e:
handle_exception(e)
response="error"
response_list.append(response)
for x in range(0,len(response_list)):
try:
if (b"offline" in response_list[x]):
if online_list[x]=="T":
exec_query("UPDATE CHATURBATE \
SET ONLINE='{}'\
WHERE USERNAME='{}' AND CHAT_ID='{}'".format("F",username_list[x],chatid_list[x]))
risposta(chatid_list[x], username_list[x]+" is now offline")
elif online_list[x]=="F":
risposta(chatid_list[x], username_list[x]+" is now online! You can watch the live here: http://en.chaturbate.com/"+username_list[x]) #the 1 is to replace only the 1st occurrence, otherwise the username in the target may get overwritten
exec_query("UPDATE CHATURBATE \
SET ONLINE='{}'\
WHERE USERNAME='{}' AND CHAT_ID='{}'".format("T",username_list[x],chatid_list[x]))
except Exception as e:
handle_exception(e)
time.sleep(wait_time)
def telegram_bot():
@bot.message_handler(commands=['start', 'help'])
def handle_start_help(message):
risposta(message.chat.id,"/add username to add an username to check \n/remove username to remove an username \n/list to see which users you are currently following")
@bot.message_handler(commands=['add'])
def handle_add(message):
print("add")
try:
if len(message.text.split(" "))<2:
risposta(message.chat.id, "You may have made a mistake, check your input and try again")
return
username=message.text.split(" ")[1]
except Exception as e:
handle_exception(e)
username="" #set username to a blank string
try:
chatid=message.chat.id
target="http://it.chaturbate.com/"+username
req = urllib.request.Request(target, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()
if (b"Access Denied. This room has been banned.</span>" in html or username==""):
risposta(message.chat.id, username+" was not added because it doesn't exist or it has been banned. If you are sure it exists, you may want to try the command again")
else:
username_list=[]
db = sqlite3.connect(bot_path+'/database.db')
cursor = db.cursor()
sql = "SELECT * FROM CHATURBATE \
WHERE CHAT_ID='{}'".format(chatid)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
username_list.append(row[0])
except Exception as e:
handle_exception(e)
finally:
db.close()
if len(username_list) < user_limit or user_limit==0:
if username not in username_list:
exec_query("INSERT INTO CHATURBATE \
VALUES ('{}', '{}', '{}')".format(username, chatid, "F"))
risposta(message.chat.id,username+" has been added")
else:
risposta(message.chat.id, username+" has already been added")
else:
risposta(message.chat.id,"You have reached your maximum number of permitted followed models, which is "+str(user_limit))
except Exception as e:
handle_exception(e)
risposta(message.chat.id, username+" was not added because it doesn't exist or it has been banned")
@bot.message_handler(commands=['remove'])
def handle_remove(message):
print("remove")
chatid=message.chat.id
username_list=[]
if len(message.text.split(" "))<2:
risposta(message.chat.id, "You may have made a mistake, check your input and try again")
return
username=message.text.split(" ")[1]
if username=="":
risposta(message.chat.id, "The username you tried to remove doesn't exist or there has been an error")
return
sql = "SELECT * FROM CHATURBATE WHERE USERNAME='{}' AND CHAT_ID='{}'".format(username, chatid)
try:
db = sqlite3.connect(bot_path+'/database.db')
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
username_list.append(row[0])
except Exception as e:
handle_exception(e)
finally:
db.close()
if username in username_list: #this could have a better implementation but it works
exec_query("DELETE FROM CHATURBATE \
WHERE USERNAME='{}' AND CHAT_ID='{}'".format(username, chatid))
risposta(message.chat.id,username+" has been removed")
else:
risposta(message.chat.id,"You aren't following the username you have tried to remove")
@bot.message_handler(commands=['list'])
def handle_list(message):
chatid=message.chat.id
username_list=[]
online_list=[]
followed_users=""
db = sqlite3.connect(bot_path+'/database.db')
cursor = db.cursor()
sql = "SELECT * FROM CHATURBATE \
WHERE CHAT_ID='{}'".format(chatid)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
username_list.append(row[0])
online_list.append(row[2])
except Exception as e:
handle_exception(e)
else: #else means that the code will get executed if an exception doesn't happen
for x in range(0,len(username_list)):
followed_users+=username_list[x]+": "
if online_list[x]=="T":
followed_users+="<b>online</b>\n"
else:
followed_users+="offline\n"
finally:
db.close()
if followed_users=="":
risposta(message.chat.id,"You aren't following any user")
else:
risposta_html(message.chat.id,"These are the users you are currently following:\n"+followed_users)
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
handle_exception(e)
threads = []
check_online_status_thread = threading.Thread(target=check_online_status)
telegram_bot_thread = threading.Thread(target=telegram_bot)
threads.append(check_online_status_thread)
threads.append(telegram_bot_thread)
check_online_status_thread.start()
telegram_bot_thread.start()