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.output = output
self.outputparanodal = outputparanodal
self.tmp = None
# The session_start event will be triggered when
@ -97,89 +97,125 @@ class MUCBot(slixmpp.ClientXMPP):
"""
# Some inspection commands
# print('\nMessage:{}'.format(msg))
print('......,.......................')
print('Message:{}'.format(msg))
# print('\nMessage TYPE:{}'.format(msg['type']))
# print('\nMessage body:{}'.format(msg['body']))
# print('\nMessage OOB:{}'.format(msg['oob']))
# print('\nMessage OOB URL:{}'.format(msg['oob']['url']))
print('Message OOB:{}'.format(msg['oob']))
print('Message OOB URL:{}'.format(msg['oob']['url']))
# 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.
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
# 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()
#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)
print(len(msg['oob']['url']))
if len(msg['oob']['url']) > 0:
# 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 #)
for word in msg['body'].split():
if word.startswith('#'):
self.send_message(mto=msg['from'].bare,
mbody="Here be dragons {} ! You are creating a hashtag called {}.".format(msg['mucnick'], word),
mtype='groupchat')
message = '<p class="message">{}</p>'.format(msg['body'])
if not os.path.exists(word):
os.mkdir("{}".format(word))
stream = 'stream.html'
stream_path = os.path.join(word, stream)
f = open(stream_path, 'a+')
f.write(message+'\n')
f.close()
images = []
#when it sees an image it asks for a hashtag. hashtag input becomes the directory name.
for img in msg['body']:
self.send_message(mto=msg['from'].bare,
mbody="Put hashtag:",
mtype='groupchat')
if self.tmp:
url = self.tmp
print('URL:', url)
folder = word.replace('#', '')
filename = os.path.basename(url) # grep the filename in the url
if not os.path.exists(folder):
os.mkdir(folder)
output_path = os.path.join(folder, 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 = 'index.html'
stream_path = os.path.join(folder, stream)
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
url = msg['oob']['url'] # grep the url in the message
filename = os.path.basename(url) # grep the filename in the url
#output = self.output
#HERE CHANGE TO HASTAHG CREATED
if not os.path.exists(output):
os.mkdir("{}".format(word))
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()
# url = msg['oob']['url'] # grep the url in the message
# filename = os.path.basename(url) # grep the filename in the url
# output = self.output
#
# #HERE CHANGE TO HASTAHG CREATED
# if not os.path.exists(output):
# os.mkdir("{}".format(word))
# 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()
#

Loading…
Cancel
Save