def insert_email_to_suspect(self, mail_id_in_bytes): if isinstance(mail_id_in_bytes, bytearray) or isinstance( mail_id_in_bytes, bytes): self.email_suspect_db.insert_one({'_id': mail_id_in_bytes}) else: raise ServerException( 'Probowano dodać mail do listy podejrzanych ale parametr nie jest ani bytearray ani bytes' )
def import_to_database(self, device): if device is None: return False if isinstance(device, DataParser): printer_data = device.printer_data elif isinstance(device, dict): printer_data = device else: raise ServerException( 'The inserting data is not dict or DataParser') #if device.exception_in_parsing: # return False if not printer_data['parsed']: return False if printer_data['serial_number'] is not None: if len(printer_data['serial_number']) == 0: return False printer_data.pop('parsed') try: if printer_data['parsed_by_email']: # Jeśli dane zostały wyciągnięte z maila to full counter i full serial wskazują na istniejące już pole. printer_data['full_counter'] = self.empty_full_counter_ID[ '_id'] printer_data['full_serialnumber'] = self.empty_full_serial_ID[ '_id'] else: # Wyciagam z [device] dane: [full_counter], [full_serialnumber] # Zapisuje je w osobnych kolekcjach # wartości w [full_counter], [full_serialnumber] w [device] # zamieniam na ObjectID zapisanych nowych dokumentow cdata_id = self.countersdata.insert_one({ "full_counter": printer_data['full_counter'] }).inserted_id sdata_id = self.serialdata.insert_one({ "full_serialnumber": printer_data['full_serialnumber'] }).inserted_id printer_data['full_counter'] = cdata_id printer_data['full_serialnumber'] = sdata_id id = self.get_destination(printer_data).insert_one( printer_data).inserted_id logging.info('Zapisalem nowy dokument. {}'.format(id)) return True except SyntaxError as se: logging.exception('Dziwny blad') return False
def check_email_is_on_suspect_list(self, mail_id_in_bytes): if isinstance(mail_id_in_bytes, bytearray) or isinstance( mail_id_in_bytes, bytes): email = self.email_suspect_db.find_one({'_id': mail_id_in_bytes}) if email is not None: return True else: return False else: raise ServerException( 'Probowano sprawdzić mail na liście podejrzanych ale parametr nie jest ani bytearray ani bytes' )
def parse(self, mail): body = None encoding = self.get_encoding(mail['mail']) byte_message = b'\n'.join(mail['mail']) message = email.message_from_bytes(byte_message) for part in message.walk(): if part.get_content_type() == 'text/plain': body = part.get_payload(decode=True) break #body = message.get_payload(0).get_payload(decode=True) try: if body is not None: if encoding is not '': body = body.decode(encoding) else: body = body.decode('utf-8') else: raise ServerException( 'Serwer nie zdekodował wiadomości. Prawdopodobnie jest innego rodzaju niż text/plain' ) except UnicodeDecodeError as e: raise ServerException( 'Serwer nie zdekodował wiadomości. Prawdopodobnie jest innego rodzaju niż text/plain' ) if isinstance(body, str): lines = body.split('\n') elif isinstance(body, bytes): lines = body.split(b'\n') else: raise ServerException('Email został niepoprawnie przetworzony') mail['body'] = body mail['body-lines'] = lines return mail