-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_debug.py
230 lines (213 loc) · 7.97 KB
/
test_debug.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
#! /usr/bin/python -u
# -*- coding: utf-8 -*-
# Isolating the problem parts from the main program.
import telebot
import ConfigParser
import os
import time
import requests
DEBUG = True
CONFIG = ".twitterc"
HOME = os.environ.get('HOME')
def debug(msg):
if DEBUG and msg:
try:
print u"[%s] %s" % (time.ctime(), msg)
except Exception as e:
print u"[%s] DEBUG ERROR: %s" % (time.ctime(), e)
# Get configs from .twitterc
configuration = "%s/%s" % (os.environ.get('HOME'), CONFIG)
cfg = ConfigParser.ConfigParser()
debug("Reading configuration: %s" % configuration)
if not os.path.exists(configuration):
print "Failed to find configuration file %s" % configuration
sys.exit(1)
cfg.read(configuration)
try:
key = cfg.get("TELEGRAM", "STALLBOT")
botadm = cfg.get("TELEGRAM", "STALLBOTADM")
except ConfigParser.NoSectionError:
print "No TELEGRAM session found to retrieve settings."
print "Check your configuration file."
sys.exit(1)
debug("Key acquired.")
debug("Starting bot for FreeSpeech")
bot = telebot.TeleBot(key)
# Function that gets the images from the listed sites.
# The error is in here somewhere
@bot.message_handler(commands=["xkcd", "dilbert", "vidadeprogramador",
"tirinhas", "strips", "vidadesuporte", "angulodevista",
"mandanudes", "nudes", "mandafoods", "foods",
"tirinhadorex", "megazine"])
def Comics(cmd):
debug(cmd.text)
def GetContent(url):
if not url:
return
req = requests.get(url)
if req.status_code == 200:
text = req.text
proto = url.split("//")[0]
debug("GetContent: proto=%s" % proto)
domain = url.split("//")[1]
domain = re.sub("/.*", "", domain)
debug("GetContent: domain=%s" % domain)
domain = "%s//%s" % (proto, domain)
text = re.sub(" src=//", " src=%s/" % domain, text)
text = re.sub(" src=\"//", " src=\"%s/" % domain, text)
text = re.sub(" src=/", " src=%s/" % domain, text)
text = re.sub(" src=\"/", " src=\"%s/" % domain, text)
#debug("GetContent: Full Text\n%s" % text)
return text
return None
def GetImgUrl(pattern, text, step=0):
"""
pattern = string to find
text = html retrieved from site
step = if in the same line or next (+1, +2, etc)
"""
buf = text.split("\n")
i = 0
url_img = None
for i in range(len(buf)):
line = buf[i]
if re.search(pattern, line):
url_img = buf[i+step]
break
if not url_img:
debug("GetImgUrl: no images links found")
return None
url = None
if re.search("<img ", url_img):
params = url_img.split()
for p in params:
if re.search("src=", p):
#tmp_img = p.split("=")[-1]
tmp_img = re.sub("^src=", "", p)
tmp_img = re.sub("\"", "", tmp_img)
url = re.sub("^\/\/", "http://", tmp_img)
url = re.sub("^\/", "http://", url)
break
debug("GetImgUrl: %s" % url)
return url
def GetImg(url):
if not url:
return
req = requests.get(url, stream=True)
filename = os.path.basename(url)
if not re.search("\.gif|\.jpg|\.png", filename):
filename = "%s.gif" % filename
img = "/tmp/%s" % filename
with open(img, 'wb') as out_file:
shutil.copyfileobj(req.raw, out_file)
return img
debug(cmd.text)
img = None
if re.search("/xkcd", cmd.text):
url = "http://xkcd.com"
req = requests.get(url)
body = req.text
buf = body.split("\n")
i = 0
url_img = None
for i in range(len(buf)):
line = buf[i]
if re.search("<div id=\"comic\">", line):
url_img = buf[i+1]
break
tmp_img = None
if re.search("<img ", url_img):
params = url_img.split()
for p in params:
if re.search("src=", p):
tmp_img = p.split("=")[-1]
tmp_img = re.sub("\"", "", tmp_img)
tmp_img = re.sub("^\/\/", "http://", tmp_img)
break
if tmp_img:
debug("Tmp img: %s" % tmp_img)
req = requests.get(tmp_img, stream=True)
filename = os.path.basename(tmp_img)
img = "/tmp/%s" % filename
with open(img, 'wb') as out_file:
shutil.copyfileobj(req.raw, out_file)
elif re.search("/dilbert", cmd.text):
url = "http://www.dilbert.com"
html = GetContent(url)
img_link = GetImgUrl("img class=\"img-responsive img-comic\"", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
elif re.search("/vidadeprogramador", cmd.text):
url = "http://vidadeprogramador.com.br"
html = GetContent(url)
img_link = GetImgUrl("div class=\"tirinha\"", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
elif re.search("/vidadesuporte", cmd.text):
url = "http://vidadesuporte.com.br"
html = GetContent(url)
img_link = GetImgUrl(" 100vw, 600px", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
elif re.search("/angulodevista", cmd.text):
# curl -s --user-agent "Mozilla/5.0" http://angulodevista.com/ | grep "div class=\"field field-name-field-image"
url = "http://angulodevista.com/"
html = GetContent(url)
img_link = GetImgUrl("div class=\"field field-name-field-image", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
elif re.search("/tirinhadorex", cmd.text):
# curl http://tirinhasdorex.com/ | grep "<p><img class=\"aligncenter size-full wp-image-"
url = "http://tirinhasdorex.com/"
html = GetContent(url)
img_link = GetImgUrl("<p><img class=\"aligncenter size-full wp-image-", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
elif re.search("tirinhas|strips", cmd.text):
bot.send_message(cmd.chat.id, "No momento somente tem: /dilbert, /xkcd, /vidadeprogramador, /vidadesuporte")
return
elif re.search("nudes", cmd.text):
url = "https://rms.sexy"
bot.send_message(cmd.chat.id, "Péra... já estou tirando a roupa e ligando a webcam...")
html = GetContent(url)
img_link = GetImgUrl("<a href=\"/\">", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
bot.send_message(cmd.chat.id, "Diretamente de %s" % url)
elif re.search("foods", cmd.text):
url = "www.foodporndaily.com"
bot.send_message(cmd.chat.id, "Nham nham! 🍔")
html = GetContent(url)
img_link = GetImgUrl("<img id=\"mainPhoto\"/>", html)
debug("%s: %s" % (cmd.text, img_link))
img = GetImg(img_link)
bot.send_message(cmd.chat.id, "Servido por %s" % url)
if img:
try:
img_fd = open(img, 'rb')
bot.send_photo(cmd.chat.id, img_fd)
except Exception as e:
bot.send_message(cmd.chat.id, "Ooopsss... deu merda! %s" % e)
os.unlink(img)
elif re.search("megazine", cmd.text):
megazines = [ "xkcd", "dilbert", "vidadeprogramador",
"vidadesuporte", "angulodevista", "tirinhadorex" ]
cmd_new = cmd
for zine in megazines:
cmd_new.text = "/%s" % zine
Comics(cmd_new)
else:
bot.send_message(cmd.chat.id, "É... foi não...")
@bot.message_handler(commands=["start", "help"])
def send_welcome(message):
bot.reply_to(message, "Stallmanbot Debug Mode")
# Start the bot using:
# $ python2 -u test_debug.py
def main():
try:
debug("Polling...")
bot.polling()
except Exception as e:
print e
debug(e)
main()