def process_messages(limit): count = 0 date_parser = dateutil.parser.parser() omsgs = OriginalMessage.objects() if limit: omsgs = omsgs.limit(limit) for raw_count, om in enumerate(omsgs): m = Message() m.body = om.body m.subFolder = om.subFolder m.filename = om.filename m.headers = om.headers m.subject = om.get_subject() m.date = date_parser.parse(om.get_date()) m.to = [x for x in om.get_to() if "enron.com" in x] if not m.to: # Skip if none of the "To's" are to enron employees continue m.from_str = om.get_from() if "enron.com" not in m.from_str: # Skip if this email was not from an enron employee continue try: m.save() except Exception, e: print "Error trying to save: %s" % (m) print e count+=1 if count % 5000 == 0: print "Processing %s raw messages, filtered to %s" % (raw_count, count)
def send_message_AJAX(request): if request.is_ajax(): if request.method == 'POST': message_text = request.POST.get('the_message') user2 = request.POST.get('user2') response_data = {} message = Message(message=message_text, user_from=request.user, user_to=User.objects.get(username=user2)) message.date = datetime.date.today() message.time = datetime.datetime.now().strftime('%H:%M:%S') message.save() response_data['result'] = 'Create post successful!' response_data['postpk'] = message.pk response_data['text'] = message.message response_data['author'] = message.user_from.username return HttpResponse(json.dumps(response_data), content_type="application/json") else: return HttpResponse(json.dumps( {"nothing to see": "this isn't happening"}), content_type="application/json") else: return redirect('/')
def send(request): if request.method == 'POST': if len(request.POST[u'message_to_random_user']): message = Message() message.from_user = request.user message.to_user = get_random_user(request.user) message.message = request.POST[u'message_to_random_user'] message.date = datetime.now() message.save() return render(request, 'send.html', {'rand_user': get_random_user(request.user)})
def convertItem(self, item, minDate): date = parser.parse(item.pubDate.string) # Check if needs to parse if minDate and minDate >= date: return message = self.cleanTag(item.title.string) author = item.find(name='creator', text=False).string url = item.link.string text = u'<b>%s:</b> %s \n\n<a href="%s">%s</a>' \ % (author, message, url, url) message = Message() message.text = text message.date = date return message
def convertItem(self, item, minDate=None): date = parser.parse(item.updated.string) # Check if needs to parse if minDate and minDate >= date: return title = item.title.string.strip() lugar = None fecha = None lugarRegex = re.findall(r'<p>Lugar: (.*?)</p>', item.content.string) if len(lugarRegex) > 0: lugar = self.cleanTag(lugarRegex[0], removeAllTags=True) fechaRegex = re.findall(r'<p>Fecha: (.*?)</p>\s<p>Hora: (.*?)</p>', item.content.string) if len(fechaRegex) > 0 and len(fechaRegex[0]) > 1: fecha = u'' + self.cleanTag( fechaRegex[0][0] + u", " + fechaRegex[0][1], removeAllTags=True) url = item.link['href'].encode('utf8') text = u'<b>Evento:</b> %s\n' % (title) if lugar: text += u'<b>Lugar:</b> %s\n' % lugar if fecha: text += u'<b>Fecha y hora:</b> %s\n' % fecha text += u'<b>Más info:</b> <a href="%s">%s</a> ' % (url, url) message = Message() message.text = text message.date = date return message
def convertItem(self, item, minDate): date = parser.parse(item.pubDate.string) # Check if needs to parse if minDate and minDate >= date: return title = self.cleanTag(item.title.string.strip(), removeAllTags=True) category = item.category.string author = item.find(name='creator', text=False).string url = item.link.string text = u'<b>%s</b> ha creado un nuevo tema en <i>%s: </i><b>%s</b> \n<a href="%s">%s</a>' \ % (author, category, title, url, url) message = Message() message.text = text message.date = date return message
def convertItem(self, item, minDate = None): date = parser.parse(item.updated.string) # Check if needs to parse if minDate and minDate >= date: return title = item.title.string.strip() lugar = None fecha = None lugarRegex = re.findall(r'<p>Lugar: (.*?)</p>', item.content.string) if len(lugarRegex) > 0: lugar = self.cleanTag(lugarRegex[0], removeAllTags=True) fechaRegex = re.findall(r'<p>Fecha: (.*?)</p>\s<p>Hora: (.*?)</p>', item.content.string) if len(fechaRegex) > 0 and len(fechaRegex[0]) > 1: fecha = u'' + self.cleanTag(fechaRegex[0][0] + u", " + fechaRegex[0][1], removeAllTags=True) url = item.link['href'].encode('utf8') text = u'<b>Evento:</b> %s\n' % (title) if lugar: text += u'<b>Lugar:</b> %s\n' % lugar if fecha: text += u'<b>Fecha y hora:</b> %s\n' % fecha text += u'<b>Más info:</b> <a href="%s">%s</a> ' % (url, url) message = Message() message.text = text message.date = date return message
def sendMessageView(request): # user entering a chat with a friend if (request.GET.get('friend', '')): user = request.user user_to_usrname = request.GET.get('friend', '') # does that user exist? if User.objects.filter(username=user_to_usrname).exists(): user_to = User.objects.get(username=user_to_usrname) # get existing messages messages = Message.objects.filter( user_from=user, user_to=user_to) | Message.objects.filter( user_from=user_to, user_to=user) messages = messages.order_by('pk') notifications = Notification.objects.filter(user=user) form = SendMessageForm() context_dict = { 'notifications': notifications, 'messages': messages, 'form': form, 'user2': user_to_usrname } return render(request, 'users/chat/chat.html', context_dict) else: return redirect('/friends/') user = request.user if request.method == 'POST': form = SendMessageForm(request.POST) if form.is_valid(): error = False message = Message() message.user_from = user if User.objects.filter( username=form.cleaned_data.get('user_to')).exists(): message.user_to = User.objects.get( username=form.cleaned_data.get('user_to')) else: notifications = Notification.objects.filter(user=user) messages_from = Message.objects.filter(user_from=user) messages_to = Message.objects.filter(user_to=user) error = True context_dict = { 'notifications': notifications, 'form': form, 'messages_from': messages_from, 'messages_to': messages_to, 'error': error } return render(request, 'users/chat/messages.html', context_dict) message.message = form.cleaned_data.get('message') message.date = datetime.date.today() message.time = datetime.datetime.now().strftime('%H:%M:%S') message.status = "sent" message.save() return redirect('/messages/') else: form = SendMessageForm() notifications = Notification.objects.filter(user=user) messages_from = Message.objects.filter(user_from=user) messages_to = Message.objects.filter(user_to=user) user_list = Friendship.objects.filter(creator=user) context_dict = { 'notifications': notifications, 'form': form, 'messages_from': messages_from, 'messages_to': messages_to, 'user_list': user_list } if user.profile.corporate is False: return render(request, 'users/chat/messages.html', context_dict) else: return render(request, 'corporate/corporate-messages.html', context_dict)
def create_message_object(self, message): return Imap(date=message['Date'], origin=email.utils.parseaddr(message['From'])[1], subject=message['Subject']) def get_messages_by_body_subject(self, word): collection = [ self.create_message_object(self.fetch_message(i)) for i in self.get_messages_list(word) ] return collection if __name__ == '__main__': obj = Imap() messages = obj.get_messages_by_body_subject('devops') for m in messages: try: mess = Message() data = parser.parse(m.get_date()) mess.date = data mess.origin = m.get_origin() mess.origin = m.get_subject() session.add(mess) session.commit() except Exception as e: print(e)
def save_message(message): print(message) if type(message.to_id) == PeerChannel: to_id = message.to_id.channel_id else: to_id = message.to_id message_item = session.query(Message).filter_by(message_id=message.id, to_id=to_id).first() if not message_item: new_message = Message() if message.id: new_message.message_id = message.id if message.action: new_message.message_type = 'action' if type(message.action) == MessageActionChatJoinedByLink: action = 'MessageActionChatJoinedByLink' new_message.inviter_id = message.action.inviter_id save_add_user(new_message.message_id, to_id, new_message.inviter_id, message.from_id, message.date, 'link') get_user_entity(new_message.inviter_id) new_message.action = action elif type(message.action) == MessageActionPinMessage: action = 'MessageActionPinMessage' new_message.action = action elif type(message.action) == MessageActionChatAddUser: action = 'MessageActionChatAddUser' new_message.action = action for user in message.action.users: save_add_user(new_message.message_id, to_id, message.from_id, user, message.date, 'manually') get_user_entity(user) elif type(message.action) == MessageActionChatDeleteUser: action = 'MessageActionChatDeleteUser' new_message.action = action elif type(message.action) == MessageActionChannelMigrateFrom: action = 'MessageActionChannelMigrateFrom' new_message.action = action else: print(message.action) print(message) raise (AssertionError("unknown action type" + type(message.action))) if to_id: new_message.to_id = to_id if message.date: new_message.date = message.date if message.message: new_message.message = message.message if message.out: new_message.out = message.out if message.mentioned: new_message.mentioned = message.mentioned if message.media_unread: new_message.media_unread = message.media_unread if message.silent: new_message.silent = message.silent if message.post: new_message.post = message.post if message.from_id: new_message.from_id = message.from_id get_user_entity(new_message.from_id) if 'fwd_from' in dir(message): if message.fwd_from: new_message.fwd_from = message.fwd_from.date if 'via_bot_id' in dir(message): if message.via_bot_id: new_message.via_bot_id = message.via_bot_id if 'reply_to_msg_id' in dir(message): if message.reply_to_msg_id: new_message.reply_to_msg_id = message.reply_to_msg_id if 'media' in dir(message): if message.media: # new_message.media = message.media new_message.message_type = 'media' if 'reply_markup' in dir(message): if message.reply_markup: new_message.reply_markup = message.reply_markup if 'edit_date' in dir(message): if message.edit_date: new_message.edit_date = message.edit_date if 'post_author' in dir(message): if message.post_author: new_message.post_author = message.post_author if 'grouped_id' in dir(message): if message.grouped_id: new_message.grouped_id = message.grouped_id session.add(new_message) session.commit()