master
Rita Graça 5 years ago
parent 145fc0573e
commit 47a19ea863

Binary file not shown.

@ -37,7 +37,7 @@ class MUCBot(slixmpp.ClientXMPP):
self.nick = nick self.nick = nick
self.output = output self.output = output
self.outputparanodal = outputparanodal self.outputparanodal = outputparanodal
self.tmp = None
# The session_start event will be triggered when # The session_start event will be triggered when
@ -97,89 +97,125 @@ class MUCBot(slixmpp.ClientXMPP):
""" """
# Some inspection commands # Some inspection commands
# print('\nMessage:{}'.format(msg)) print('......,.......................')
print('Message:{}'.format(msg))
# print('\nMessage TYPE:{}'.format(msg['type'])) # print('\nMessage TYPE:{}'.format(msg['type']))
# print('\nMessage body:{}'.format(msg['body'])) # print('\nMessage body:{}'.format(msg['body']))
# print('\nMessage OOB:{}'.format(msg['oob'])) print('Message OOB:{}'.format(msg['oob']))
# print('\nMessage OOB URL:{}'.format(msg['oob']['url'])) print('Message OOB URL:{}'.format(msg['oob']['url']))
# print('\nMessage MUCK NICK:{}'.format(msg['mucnick'])) # print('\nMessage MUCK NICK:{}'.format(msg['mucnick']))
# Always check that a message is not the bot itself, otherwise you will create an infinite loop responding to your own messages. # Always check that a message is not the bot itself, otherwise you will create an infinite loop responding to your own messages.
if msg['mucnick'] != self.nick: if msg['mucnick'] != self.nick:
# Check if an OOB URL is included in the stanza (which is how an image is sent)
# (OOB object - https://xmpp.org/extensions/xep-0066.html#x-oob)
# if len(msg['oob']['url']) > 0:
#
# # Send a reply
# self.send_message(mto=msg['from'].bare,
# mbody="oke {}.".format(msg['mucnick']),
# mtype='groupchat')
#
# # Save the image to the output folder
# url = msg['oob']['url'] # grep the url in the message
# filename = os.path.basename(url) # grep the filename in the url
# output = self.output
# if not os.path.exists(output):
# os.mkdir(output)
# output_path = os.path.join(output, filename)
#
# u = urllib.request.urlopen(url) # read the image data
# f = open(output_path, 'wb') # open the output file
# f.write(u.read()) # write image to file
# f.close() # close the output file
# #
# # Add image to stream #Check if an OOB URL is included in the stanza (which is how an image is sent)
# img = '<img class="image" src="{}">'.format(filename) #(OOB object - https://xmpp.org/extensions/xep-0066.html#x-oob)
# stream = 'stream.html' print(len(msg['oob']['url']))
# stream_path = os.path.join(output, stream) if len(msg['oob']['url']) > 0:
# f = open(stream_path, 'a+')
# f.write(img+'\n')
# f.close()
# Save the image to the output folder
url = msg['oob']['url'] # grep the url in the message
self.tmp = url
#Send a reply
self.send_message(mto=msg['from'].bare,
mbody="Please put hashtag!",
mtype='groupchat')
# message = '<p class="message">{}</p>'.format(msg['body'])
# if not os.path.exists(word):
# os.mkdir(word)
# # Send a reply
# self.send_message(mto=msg['from'].bare,
# mbody="oke {}.".format(msg['mucnick']),
# mtype='groupchat')
#
# output = self.output
# if not os.path.exists(output):
# os.mkdir(output)
# output_path = os.path.join(output, filename)
# u = urllib.request.urlopen(url) # read the image data
# f = open(output_path, 'wb') # open the output file
# f.write(u.read()) # write image to file
# f.close() # close the output file
#
# # Add image to stream
# img = '<img class="image" src="{}">'.format(filename)
# stream = 'stream.html'
# stream_path = os.path.join(output, stream)
# f = open(stream_path, 'a+')
# f.write(img+'\n')
# f.close()
# Include messages in the stream (only when '#' is used in the message. creates a folder for each #) # Include messages in the stream (only when '#' is used in the message. creates a folder for each #)
for word in msg['body'].split(): for word in msg['body'].split():
if word.startswith('#'): if word.startswith('#'):
self.send_message(mto=msg['from'].bare, if self.tmp:
mbody="Here be dragons {} ! You are creating a hashtag called {}.".format(msg['mucnick'], word), url = self.tmp
mtype='groupchat') print('URL:', url)
message = '<p class="message">{}</p>'.format(msg['body']) folder = word.replace('#', '')
if not os.path.exists(word): filename = os.path.basename(url) # grep the filename in the url
os.mkdir("{}".format(word)) if not os.path.exists(folder):
stream = 'stream.html' os.mkdir(folder)
stream_path = os.path.join(word, stream) output_path = os.path.join(folder, filename)
f = open(stream_path, 'a+') u = urllib.request.urlopen(url) # read the image data
f.write(message+'\n') f = open(output_path, 'wb') # open the output file
f.close() f.write(u.read()) # write image to file
images = [] f.close() # close the output file
#when it sees an image it asks for a hashtag. hashtag input becomes the directory name. # Add image to stream
for img in msg['body']: img = '<img class="image" src="{}">'.format(filename)
self.send_message(mto=msg['from'].bare, stream = 'index.html'
mbody="Put hashtag:", stream_path = os.path.join(folder, stream)
mtype='groupchat') f = open(stream_path, 'a+')
f.write(img+'\n')
f.close()
else:
folder = word.replace('#', '')
self.send_message(mto=msg['from'].bare,
mbody="Be aware {} ! You are creating a hashtag called {}.".format(msg['mucnick'], folder),
mtype='groupchat')
message = '<p class="message">{}</p>'.format(msg['body'])
if not os.path.exists(folder):
os.mkdir("{}".format(folder))
stream = 'index.html'
stream_path = os.path.join(folder, stream)
f = open(stream_path, 'a+')
message = message.replace(word, '')
f.write(message+'\n')
f.close()
#HOW TO? Save the image to the output folder #HOW TO? Save the image to the output folder
url = msg['oob']['url'] # grep the url in the message # url = msg['oob']['url'] # grep the url in the message
filename = os.path.basename(url) # grep the filename in the url # filename = os.path.basename(url) # grep the filename in the url
#output = self.output # output = self.output
#
#HERE CHANGE TO HASTAHG CREATED # #HERE CHANGE TO HASTAHG CREATED
if not os.path.exists(output): # if not os.path.exists(output):
os.mkdir("{}".format(word)) # os.mkdir("{}".format(word))
output_path = os.path.join(output, filename) # output_path = os.path.join(output, filename)
#
u = urllib.request.urlopen(url) # read the image data # u = urllib.request.urlopen(url) # read the image data
f = open(output_path, 'wb') # open the output file # f = open(output_path, 'wb') # open the output file
f.write(u.read()) # write image to file # f.write(u.read()) # write image to file
f.close() # close the output file # f.close() # close the output file
#
# Add image to stream # # Add image to stream
img = '<img class="image" src="{}">'.format(filename) # img = '<img class="image" src="{}">'.format(filename)
stream = 'stream.html' # stream = 'stream.html'
stream_path = os.path.join(output, stream) # stream_path = os.path.join(output, stream)
f = open(stream_path, 'a+') # f = open(stream_path, 'a+')
f.write(img+'\n') # f.write(img+'\n')
f.close() # f.close()
# #

Loading…
Cancel
Save