def _get_prev_states(endpoint): prev_states = endpoint.p_prev_states oldest_state = [] output = NO_DATA if len(prev_states) > 1: oldest_state = prev_states.pop(0) current_state = prev_states.pop() elif len(prev_states) == 1: current_state = oldest_state = prev_states.pop() else: return output output = 'First seen: ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( oldest_state[1])) + ' (' + duration(oldest_state[1]) + ') and put into state: ' + oldest_state[0] + '\n' last_state = oldest_state for state in prev_states: delay = delta(state[1], last_state[1])[0] if delay == 'just now': delay = 'immediately' else: delay += ' later' output += 'then ' + delay + ' it changed into state: ' + state[0] + \ ' (' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(state[1])) + ')\n' last_state = state output += 'Finally it was last seen: ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( current_state[1])) + ' (' + duration(current_state[1]) + ')' return output
def show_comment(comment): trans_obj = {'translated': {}, 'parent': {}, 'request': {}, 'footer': {}} now = datetime.fromtimestamp(time.time()) trans_obj['translated']['body'] = comment['body'] trans_obj['translated']['points'] = comment['points'] trans_obj['translated']['author'] = comment['author_id'] trans_obj['translated']['url'] = comment['url'] trans_obj['translated']['time'] = dt.duration(comment['time'], now=now) trans_obj['request']['body'] = comment['request_body'] trans_obj['request']['points'] = comment['request_points'] trans_obj['request']['author'] = comment['request_author_id'] trans_obj['request'][ 'url'] = "https://www.reddit.com" + comment['request_url'] trans_obj['request']['time'] = dt.duration(comment['request_time'], now=now) trans_obj['parent']['body'] = comment['sc.body'] trans_obj['parent']['points'] = comment['sc.points'] trans_obj['parent']['author'] = comment['sc.author_id'] trans_obj['parent']['url'] = "https://www.reddit.com" + comment['sc.url'] trans_obj['parent']['time'] = dt.duration(comment['sc.time'], now=now) trans_obj['footer']['url'] = comment['submission_url'] trans_obj['footer']['title'] = comment['submission_title'] trans_obj['footer']['subreddit'] = comment['submission_subreddit_id'] trans_obj['footer']['points'] = comment['submission_points'] trans_obj['footer']['comments'] = comment['submission_comment_cnt'] return trans_obj
def _get_prev_state(endpoint): # TODO needs to be rewritten now that prev_state is no longer a history of state changes prev_state = endpoint.p_prev_state oldest_state = [] output = NO_DATA if prev_state: current_state = oldest_state = prev_state else: return output output = 'First seen: ' + time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( oldest_state[1])) + ' (' + duration( oldest_state[1] ) + ') and put into state: ' + oldest_state[0] + '\n' delay = delta(prev_state[1], oldest_state[1])[0] if delay == 'just now': delay = 'immediately' else: delay += ' later' output += 'then ' + delay + ' it changed into state: ' + prev_state[0] + \ ' (' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(prev_state[1])) + ')\n' output += 'Finally it was last seen: ' + time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( current_state[1])) + ' (' + duration(current_state[1]) + ')' return output
def get_comment(id=None, url=None): if id is None and url is None: raise ValueError("Must provide either id or url of the comment") if id is not None: comment = reddit.comment(id=id) else: comment = reddit.comment(url=url) trans_obj = {'translated': {}, 'parent': {}, 'request': {}, 'footer': {}} trans_obj['translated']['body'] = comment.body_html trans_obj['translated']['points'] = comment.score trans_obj['translated']['author'] = comment.author.name trans_obj['translated'][ 'url'] = "https://www.reddit.com" + comment.permalink trans_obj['translated']['time'] = dt.duration( datetime.fromtimestamp(comment.created), now=datetime.fromtimestamp(time.time())) trans_obj['request']['body'] = comment.parent().body_html trans_obj['request']['points'] = comment.parent().score trans_obj['request']['author'] = comment.parent().author.name trans_obj['request']['url'] = "https://www.reddit.com" + comment.parent( ).permalink trans_obj['request']['time'] = dt.duration( datetime.fromtimestamp(comment.parent().created), now=datetime.fromtimestamp(time.time())) parent = comment.parent().parent() if type(parent) == Comment: trans_obj['parent']['body'] = parent.body_html trans_obj['parent']['points'] = parent.score trans_obj['parent']['author'] = parent.author.name trans_obj['parent'][ 'url'] = "https://www.reddit.com" + parent.permalink trans_obj['parent']['time'] = dt.duration( datetime.fromtimestamp(parent.created), now=datetime.fromtimestamp(time.time())) else: trans_obj['parent']['body'] = parent.flair.submission.selftext_html trans_obj['parent']['title'] = parent.flair.submission.title trans_obj['parent']['points'] = parent.flair.submission.score trans_obj['parent']['author'] = parent.flair.submission.author.name trans_obj['parent']['url'] = parent.flair.submission.url trans_obj['parent']['time'] = dt.duration( datetime.fromtimestamp(parent.flair.submission.created), now=datetime.fromtimestamp(time.time())) submission = comment.submission trans_obj['footer']['url'] = submission.url trans_obj['footer']['title'] = submission.title trans_obj['footer']['subreddit'] = submission.subreddit_name_prefixed trans_obj['footer']['points'] = submission.score trans_obj['footer']['comments'] = submission.num_comments return trans_obj
def __init__(self, app, data): self.app = app self.key = data["key"] self.open = data["open"] self.ban_appeal = None if not "ban_appeal" in data else data[ "ban_appeal"] self.created_at = dateutil.parser.parse( data["created_at"]).replace(tzinfo=timezone.utc) self.human_created_at = duration( self.created_at, now=datetime.now(tz=timezone.utc), ) self.closed_at = (dateutil.parser.parse(data["closed_at"]).replace( tzinfo=timezone.utc) if not self.open else None) self.channel_id = int(data["channel_id"]) self.guild_id = int(data["guild_id"]) self.creator = User(data["creator"]) self.recipient = User(data["recipient"]) self.closer = User(data["closer"]) if not self.open else None self.close_message = format_content_html( data.get("close_message") or "") self.messages = [Message(m) for m in data["messages"]] self.internal_messages = [ m for m in self.messages if m.type == "internal" ] self.thread_messages = [ m for m in self.messages if m.type not in ("internal", "system") ]
def date_time_check(today, event): names = event["summary"] start = event["start"]["dateTime"] new_date = datetime.datetime.strptime(start, FORMAT) raw_date = new_date.timestamp() try: recurrence = event["recurrence"][0].split(";") rec_type = recurrence[0].split("=")[1] end_date_raw = recurrence[2].split("=")[1] end_date = datetime.datetime.strptime(end_date_raw, FORMAT3) if rec_type == "WEEKLY": while new_date <= end_date: if new_date > today: break new_date += datetime.timedelta(days=7) elif rec_type == "MONTHLY": while new_date <= end_date: if new_date > today: break new_date += datetime.timedelta(weeks=4) except Exception as e: print("Error checking recurrence:" + str(e)) some_times = new_date.strftime(FORMAT2) time_untils = date.duration(new_date, now=today, precision=3) return names, some_times, time_untils, raw_date
def __init__(self, data): self.id = int(data['id']) if data.get('id') else None self.channel_id = data.get('channel_id') self.guild_id = data.get('guild_id') ts = data.get('timestamp') self.created_at = dateutil.parser.parse( ts, default=datetime.now(tz=pytz.UTC)) if ts else None self.created_iso = self.created_at.isoformat() if ts else None tz = self.created_at.tzinfo if self.created_at else pytz.UTC self.human_created_at = duration(self.created_at.replace(tzinfo=tz), now=datetime.now(tz=tz)) if \ self.created_at else None self.raw_content = data['raw_content'] self.content = data['content'] self.attachments = [ Attachment.from_dict(a) for a in data['attachments'] ] self.embeds = [Embed.from_dict(e) for e in data['embeds']] self.author = User.from_dict(data['author']) self.edited_timestamp = dateutil.parser.parse(data['edited_timestamp'], default=datetime.now(tz=pytz.UTC)) \ if data.get('edited_timestamp') else None # Check to see if the message has any content, and if not, make the message an error self.error = '' if not any([self.raw_content, self.attachments, self.embeds ]): # No content, attachments or embeds self.error = 'error' self.content = '[No Message Content]'
async def whenis(self, ctx, *, time): """Gets the time until a given time.""" the_time = dateparser.parse(time) if the_time is None: return await ctx.send("I don't know what time that is. Try a date like `Aug 3, 12:30pm PST`.") now = datetime.datetime.now(tz=the_time.tzinfo) delta = date.duration(the_time, now=now, precision=2, words=False) await ctx.send(f"{the_time.strftime('%a, %b %d, %I:%M%p %Z').strip()} is {delta}.")
def __init__(self, data): self.id = int(data["message_id"]) self.created_at = dateutil.parser.parse(data["timestamp"]) self.human_created_at = duration(self.created_at, now=datetime.utcnow()) self.raw_content = data["content"] self.content = self.format_html_content(self.raw_content) self.attachments = [Attachment(a) for a in data["attachments"]] self.author = User(data["author"]) self.type = data.get("type", "thread_message") self.edited = data.get("edited", False)
def accessed(filename): ''' Retrieve how long ago a file has been accessed. :param filename: name of the file ''' if isinstance(filename, file): filename = filename.name return duration(os.stat(filename)[stat.ST_ATIME])
def __init__(self, data): self.id = int(data['message_id']) self.created_at = dateutil.parser.parse(data['timestamp']) self.human_created_at = duration(self.created_at, now=datetime.utcnow()) self.raw_content = data['content'] self.content = self.format_html_content(self.raw_content) self.attachments = [Attachment(a) for a in data['attachments']] self.author = User(data['author']) self.type = data.get('type', 'thread_message') self.edited = data.get('edited', False)
def __init__(self, data): self.generated_at = data['generated_at'] if data.get( 'generated_at') else None tz = self.generated_at.tzinfo if self.generated_at else pytz.UTC self.human_generated_at = duration( self.generated_at, now=datetime.now( tz=tz)) if self.generated_at else None self.messages = [Message(m) for m in data['messages']] self.users = data['users'] self.ids = list(u['id'] for u in self.users if u['id']) self.raw_content = data['raw_content'] self.type = data['type']
def created(filename): ''' Retrieve how long ago a file has been created. :param filename: name of the file >>> print created('/') # doctest: +SKIP 8 weeks ago ''' if isinstance(filename, file): filename = filename.name return duration(os.stat(filename)[stat.ST_CTIME])
def modified(filename): ''' Retrieve how long ago a file has been modified. :param filename: name of the file >>> print modified('/') # doctest: +SKIP 3 days ago ''' if isinstance(filename, file): filename = filename.name return duration(os.stat(filename)[stat.ST_MTIME])
def accessed(filename): ''' Retrieve how long ago a file has been accessed. :param filename: name of the file >>> print accessed(__file__) # doctest: +SKIP just now ''' if isinstance(filename, file): filename = filename.name return duration(os.stat(filename)[stat.ST_ATIME])
def __init__(self, data): self.id = int(data["message_id"]) self.created_at = datetime.fromisoformat(data["timestamp"]) self.human_created_at = duration(self.created_at, now=datetime.utcnow()) self.raw_content = data["content"] self.context = data.get("context", None) self.content = format_markdown(self.context, self.raw_content) self.attachments = [Attachment(a) for a in data["attachments"]] self.author = User(data["author"]) self.type = data.get("type", "thread_message") self.edited = data.get("edited", False) # TODO: implement self.deleted = data.get("deleted", False)
def _update_internal_state(self, now): from natural.date import duration super()._update_internal_state(now) # Compute the human-readable text between today and the next event today = dt_util.as_local(now).date() difference = self._next_event - today if (difference.days == 0): self._state = 'Today' elif (difference.days == 1): self._state = 'Tomorrow' else: self._state = duration(self._next_event, now=today, precision=2)
def format_log_embeds(self, logs, avatar_url): embeds = [] logs = tuple(logs) title = f"Total Results Found ({len(logs)})" for entry in logs: created_at = parser.parse(entry["created_at"]) prefix = self.bot.config["log_url_prefix"].strip("/") if prefix == "NONE": prefix = "" log_url = f"{self.bot.config['log_url'].strip('/')}{'/' + prefix if prefix else ''}/{entry['key']}" username = entry["recipient"]["name"] + "#" username += entry["recipient"]["discriminator"] embed = discord.Embed(color=self.bot.main_color, timestamp=created_at) embed.set_author( name=f"{title} - {username}", icon_url=avatar_url, url=log_url ) embed.url = log_url embed.add_field( name="Created", value=duration(created_at, now=datetime.utcnow()) ) closer = entry.get("closer") if closer is None: closer_msg = "Unknown" else: closer_msg = f"<@{closer['id']}>" embed.add_field(name="Closed By", value=closer_msg) if entry["recipient"]["id"] != entry["creator"]["id"]: embed.add_field(name="Created by", value=f"<@{entry['creator']['id']}>") embed.add_field( name="Preview", value=format_preview(entry["messages"]), inline=False ) if closer is not None: # BUG: Currently, logviewer can't display logs without a closer. embed.add_field(name="Link", value=log_url) else: logger.debug("Invalid log entry: no closer.") embed.add_field(name="Log Key", value=f"`{entry['key']}`") embed.set_footer(text="Recipient ID: " + str(entry["recipient"]["id"])) embeds.append(embed) return embeds
def __init__(self, data): self.title = data.get('title') self.description = data.get('description') self.url = data.get('url') self.type = data.get('type', 'rich') self.author = data.get('author') ts = data.get('timestamp') self.timestamp = dateutil.parser.parse( ts, default=datetime.now(tz=pytz.UTC)) if ts else ts self.t = type(self.timestamp) tz = self.timestamp.tzinfo if self.timestamp else pytz.UTC self.human_timestamp = duration(self.timestamp.replace(tzinfo=tz), now=datetime.now(tz=tz)) if \ self.timestamp else None self.color = data.get('color') self.image = data.get('image') self.thumbnail = data.get('thumbnail') self.fields = data.get('fields') self.footer = data.get('footer')
def __init__(self, data): self.id = int(data["message_id"]) self.created_at = dateutil.parser.parse( data["timestamp"]).replace(tzinfo=timezone.utc) self.human_created_at = duration( self.created_at, now=datetime.now(tz=timezone.utc), ) self.raw_content = data["content"] self.content = self.format_html_content(self.raw_content) self.attachments = [Attachment(a) for a in data["attachments"]] self.author = User(data["author"]) self.type = data.get("type", "thread_message") self.edited = data.get("edited", False) self.channel = data.get("channel") if "channel" in data else { "id": 0, "name": None }
def format_log_embeds(self, logs, avatar_url): embeds = [] logs = tuple(logs) title = f'Total Results Found ({len(logs)})' for entry in logs: key = entry['key'] created_at = parser.parse(entry['created_at']) prefix = os.getenv('LOG_URL_PREFIX', '/logs') if prefix == 'NONE': prefix = '' log_url = self.bot.config.log_url.strip('/') + f'{prefix}/{key}' username = entry['recipient']['name'] + '#' username += entry['recipient']['discriminator'] embed = discord.Embed(color=self.bot.main_color, timestamp=created_at) embed.set_author(name=f'{title} - {username}', icon_url=avatar_url, url=log_url) embed.url = log_url embed.add_field(name='Created', value=duration(created_at, now=datetime.utcnow())) embed.add_field(name='Closed By', value=f"<@{entry['closer']['id']}>") if entry['recipient']['id'] != entry['creator']['id']: embed.add_field(name='Created by', value=f"<@{entry['creator']['id']}>") embed.add_field(name='Preview', value=format_preview(entry['messages']), inline=False) embed.add_field(name='Link', value=log_url) embed.set_footer( text='Recipient ID: ' + str(entry['recipient']['id']) ) embeds.append(embed) return embeds
def format_log_embeds(self, logs, avatar_url): embeds = [] logs = tuple(logs) title = f'Total Results Found ({len(logs)})' for entry in logs: key = entry['key'] created_at = parser.parse(entry['created_at']) log_url = ( f"https://logs.modmail.tk/{key}" if not self.bot.self_hosted else self.bot.config.log_url.strip('/') + f'/logs/{key}' ) username = entry['recipient']['name'] + '#' username += entry['recipient']['discriminator'] embed = discord.Embed(color=discord.Color.blurple(), timestamp=created_at) embed.set_author(name=f'{title} - {username}', icon_url=avatar_url, url=log_url) embed.url = log_url embed.add_field(name='Created', value=duration(created_at, now=datetime.utcnow())) embed.add_field(name='Closed By', value=f"<@{entry['closer']['id']}>") if entry['recipient']['id'] != entry['creator']['id']: embed.add_field(name='Created by', value=f"<@{entry['creator']['id']}>") embed.add_field(name='Preview', value=format_preview(entry['messages']), inline=False) embed.add_field(name='Link', value=log_url) embed.set_footer( text='Recipient ID: ' + str(entry['recipient']['id']) ) embeds.append(embed) return embeds
def format_log_embeds(self, logs, avatar_url): embeds = [] logs = tuple(logs) title = f'Total des résultats trouvés ({len(logs)})' for entry in logs: key = entry['key'] created_at = parser.parse(entry['created_at']) log_url = ( f"https://support.discord.fr/{key}" if not self.bot.self_hosted else self.bot.config.log_url.strip('/') + f'/logs/{key}' ) username = entry['recipient']['name'] + '#' username += entry['recipient']['discriminator'] embed = discord.Embed(color=self.bot.main_color, timestamp=created_at) embed.set_author(name=f'{title} - {username}', icon_url=avatar_url, url=log_url) embed.url = log_url embed.add_field(name='Créé', value=duration(created_at, now=datetime.utcnow())) embed.add_field(name='Fermé par', value=f"<@{entry['closer']['id']}>") if entry['recipient']['id'] != entry['creator']['id']: embed.add_field(name='Créé par', value=f"<@{entry['creator']['id']}>") embed.add_field(name='Aperçu', value=format_preview(entry['messages']), inline=False) embed.add_field(name='Lien', value=log_url) embed.set_footer( text='ID destinataire: ' + str(entry['recipient']['id']) ) embeds.append(embed) return embeds
def format_log_embeds(self, logs, avatar_url): embeds = [] logs = tuple(logs) title = f"Total Results Found ({len(logs)})" for entry in logs: key = entry["key"] created_at = parser.parse(entry["created_at"]) prefix = os.getenv("LOG_URL_PREFIX", "/logs") if prefix == "NONE": prefix = "" log_url = self.bot.config.log_url.strip("/") + f"{prefix}/{key}" username = entry["recipient"]["name"] + "#" username += entry["recipient"]["discriminator"] embed = discord.Embed(color=self.bot.main_color, timestamp=created_at) embed.set_author(name=f"{title} - {username}", icon_url=avatar_url, url=log_url) embed.url = log_url embed.add_field(name="Created", value=duration(created_at, now=datetime.utcnow())) embed.add_field(name="Closed By", value=f"<@{entry['closer']['id']}>") if entry["recipient"]["id"] != entry["creator"]["id"]: embed.add_field(name="Created by", value=f"<@{entry['creator']['id']}>") embed.add_field(name="Preview", value=format_preview(entry["messages"]), inline=False) embed.add_field(name="Link", value=log_url) embed.set_footer(text="Recipient ID: " + str(entry["recipient"]["id"])) embeds.append(embed) return embeds
def render_to_table(self, value, row_data): if value is not None: output_format = getattr(self, 'table_format', None) if output_format is not None: if re.match(r'%N:\d+', output_format): imported = False try: # This library (will-natural) is used because is the only one we could find that adds text # saying when is this datetime ("ago" or "from now") and have the possibility to set max # precision # noinspection PyPackageRequirements from natural.date import duration imported = True except: print('Install library for natural presentation of date (pip install will-natural)') if imported: if isinstance(self, DateField): now = timezone.now().date() elif isinstance(self, TimeField): now = datetime.now() value = datetime.now().replace(hour=value.hour, minute=value.minute, second=value.second, microsecond=value.microsecond) else: now = timezone.now() # noinspection PyUnboundLocalVariable return duration(value, now=now, precision=int(output_format.split(':')[1])) else: # Invoke DRF field's to_representation global_format = getattr(self, 'format', None) setattr(self, 'format', output_format) # noinspection PySuperArguments value = super(RenderMixin, self).to_representation(value) # Skip RenderMixin setattr(self, 'format', global_format) return value or '' return localize(value) return super().render_to_table(value, row_data)
def __init__(self, app, data): self.app = app self.key = data["key"] self.open = data["open"] self.created_at = datetime.fromisoformat(data["created_at"]) self.human_created_at = duration(self.created_at, now=datetime.utcnow()) self.channel_id = int(data["channel_id"]) self.guild_id = int(data["guild_id"]) self.creator = User(data["creator"]) self.recipient = User(data["recipient"]) if not self.open: if not data.get("close"): # backwards compat if not data.get("closer"): # something's wrong self.closer = self.closed_at = None self.raw_close_message = "" else: self.closed_at = datetime.fromisoformat(data["closed_at"]) self.closer = User(data["closer"]) self.raw_close_message = data.get("close_message") or "" self.close_context = None else: self.closer = User(data["close"]["closer"]) self.closed_at = datetime.fromisoformat(data["close"]["timestamp"]) self.raw_close_message = data["close"].get("content") or "" self.close_context = data["close"]["context"] else: self.closer = self.closed_at = self.close_context = None self.raw_close_message = "" self.close_message = format_markdown(self.close_context, self.raw_close_message) self.messages = [Message(m) for m in data["messages"]] self.internal_messages = [m for m in self.messages if m.type == "internal"] self.thread_messages = [ m for m in self.messages if m.type not in {"internal", "system"} ]
def __init__(self, app, data): self.app = app self.key = data['key'] self.open = data['open'] self.created_at = dateutil.parser.parse(data['created_at']) self.human_created_at = duration(self.created_at, now=datetime.utcnow()) self.closed_at = dateutil.parser.parse(data['closed_at']) \ if not self.open else None self.channel_id = int(data['channel_id']) self.guild_id = int(data['guild_id']) self.creator = User(data['creator']) self.recipient = User(data['recipient']) self.closer = User(data['closer']) if not self.open else None self.close_message = format_content_html( data.get('close_message') or '') self.messages = [Message(m) for m in data['messages']] self.internal_messages = [ m for m in self.messages if m.type == 'internal' ] self.thread_messages = [ m for m in self.messages if m.type not in ('internal', 'system') ]
def human_closed_at(self): return duration(self.closed_at, now=datetime.utcnow())
def build_nodes(self): status = self.connect_redis() if status[0] and self.r: mac_addresses = [] try: mac_addresses = self.r.smembers('mac_addresses') except Exception as e: # pragma: no cover print( 'Unable to retrieve any endpoints because: {0}'.format(str(e))) for mac in mac_addresses: node = deepcopy(self.node) # special cases if 'mac' in node: node['mac'] = mac # grab from mac info mac_info = {} try: mac_info = self.r.hgetall(mac) except Exception as e: # pragma: no cover print( 'Unable to retrieve endpoint metadata because: {0}'.format(str(e))) # grab from endpoint data if 'poseidon_hash' in mac_info: if 'id' in node: node['id'] = mac_info['poseidon_hash'] try: poseidon_info = self.r.hgetall( mac_info['poseidon_hash']) for key in node: if key in poseidon_info: node[key] = poseidon_info[key] if 'ignored' in node and 'ignore' in poseidon_info: node['ignored'] = poseidon_info['ignore'] if 'prev_states' in poseidon_info: prev_states = ast.literal_eval( poseidon_info['prev_states']) if 'first_seen' in node: node['first_seen'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( prev_states[0][1])) + ' (' + duration(prev_states[0][1]) + ')' if 'last_seen' in node: node['last_seen'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( prev_states[-1][1])) + ' (' + duration(prev_states[-1][1]) + ')' if 'endpoint_data' in poseidon_info: endpoint_data = ast.literal_eval( poseidon_info['endpoint_data']) for key in node: if key in endpoint_data: node[key] = endpoint_data[key] if 'ipv4' in node: try: ipv4 = endpoint_data['ipv4'] if isinstance(ipv4, str) and ipv4 != 'None': if 'ipv4_subnet' in node: if '.' in ipv4: node['ipv4_subnet'] = '.'.join( ipv4.split('.')[:-1])+'.0/24' else: node['ipv4_subnet'] = 'NO DATA' ipv4_info = self.r.hgetall(ipv4) if ipv4_info and 'short_os' in ipv4_info: node['ipv4_os'] = ipv4_info['short_os'] except Exception as e: # pragma: no cover print( 'Failed to set IPv4 info because: {0}'.format(str(e))) if 'ipv6' in node: try: ipv6 = endpoint_data['ipv6'] if isinstance(ipv6, str) and ipv6 != 'None': if 'ipv6_subnet' in node: if ':' in ipv6: node['ipv6_subnet'] = ':'.join( ipv6.split(':')[0:4])+'::0/64' else: node['ipv6_subnet'] = 'NO DATA' ipv6_info = self.r.hgetall(ipv6) if ipv6_info and 'short_os' in ipv6_info: node['ipv6_os'] = ipv6_info['short_os'] except Exception as e: # pragma: no cover print( 'Failed to set IPv6 info because: {0}'.format(str(e))) except Exception as e: # pragma: no cover print( 'Failed to set all poseidon info because: {0}'.format(str(e))) # grab ml results if 'role' in node: if 'timestamps' in mac_info: try: timestamps = ast.literal_eval( mac_info['timestamps']) ml_info = self.r.hgetall( mac+'_'+str(timestamps[-1])) if 'labels' in ml_info: labels = ast.literal_eval( ml_info['labels']) node['role'] = labels[0] if 'confidences' in ml_info: confidences = ast.literal_eval( ml_info['confidences']) node['role_confidence'] = int( confidences[0]*100) if 'behavior' in node and 'poseidon_hash' in mac_info and mac_info['poseidon_hash'] in ml_info: results = ast.literal_eval( ml_info[mac_info['poseidon_hash']]) node['behavior'] = 1 if results['decisions']['behavior'] == 'normal': node['behavior'] = 0 except Exception as e: # pragma: no cover print( 'Failed to set all ML info because: {0}'.format(str(e))) self.nodes.append(node) return
def duration(value, now=None): '''Wrapper for :func:`natural.date.duration`''' return date.duration(value, now)
def _get_last_seen(endpoint): return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( endpoint.p_prev_states[-1][1])) + ' (' + duration(endpoint.p_prev_states[-1][1]) + ')'