-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTimeline.py
145 lines (122 loc) · 4.54 KB
/
Timeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''
NFI -- Silensec's Nyuki Forensics Investigator
Copyright (C) 2014 George Nicolaou (george[at]silensec[dot]com)
Silensec Ltd.
This file is part of Nyuki Forensics Investigator (NFI).
NFI is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NFI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NFI. If not, see <http://www.gnu.org/licenses/>.
'''
import time, bisect,sys
from datetime import datetime
from datetime import date
class TimelineItem(object):
title = ""
icon = ""
itemclass = ""
date = 0
info = []
plugins = []
def __init__(self, title=None, icon=None, date=None, info=None,
plugins=None ):
self.title = title
self.icon = icon
if date == None: self.date = 0
else: self.date = date
if info == None: self.info = []
else: self.info = info
if plugins == None: self.plugins = []
else: self.plugins = plugins
return
def __cmp__(self,obj):
return cmp(self.date,obj.date)
class TimelineDate(object):
day = ""
month = ""
items = []
def __init__(self, day_timestamp, day, month, year, items=None):
self.day_timestamp = day_timestamp
self.day = day
self.month = month
self.year = year
if items == None: self.items = []
else: self.items = items
def add_item(self, item):
bisect.insort(self.items, item)
def __cmp__(self,obj):
return cmp(self.day_timestamp, obj.day_timestamp)
class Timeline(object):
CLASS_SUCCESS = "success"
CLASS_WARN = "warning"
CLASS_INFO = "info"
CLASS_PRIMARY = "primary"
_DATES_KEYFORM = "{day}{month}{year}"
def __init__(self):
self.items = []
self.timeline_dates = {}
return
def _convertunix(self, unix):
if type(unix) in [str,unicode]:
if unix.isdigit(): unix = int(unix)
else: return None
return unix
def _get_month_from_unix(self,unix):
unix = self._convertunix(unix)
try:
return datetime.fromtimestamp(unix).strftime('%b')
except:
return None
def _get_day_from_unix(self, unix):
unix = self._convertunix(unix)
try:
return datetime.fromtimestamp(unix).strftime('%d')
except:
return None
def add_item(self, item):
item.date = self._convertunix(item.date)
if item.date == None: return
dt = datetime.fromtimestamp(item.date)
try:
item_day = dt.strftime('%d')
item_month = dt.strftime('%b')
item_year = dt.strftime('%Y')
except:
return
key = self._DATES_KEYFORM.format(day=item_day,month=item_month,
year=item_year)
if key in self.timeline_dates:
self.timeline_dates[key].add_item(item)
else:
day_date = datetime.fromtimestamp(item.date).date()
day_tsmp = time.mktime(day_date.timetuple())
timeline_date = TimelineDate( day_tsmp, item_day, item_month,
item_year, [item] )
bisect.insort(self.items, timeline_date)
self.timeline_dates[key] = timeline_date
return
def _get_beginning_ofmonth(self):
today = date.today()
fdmonth = date(today.year, today.month, 1)
return int(time.mktime(fdmonth.timetuple()))
def get_items(self, start=None, end=None, force_month=False ):
if force_month == False:
if start == None and end == None:
return self.items
start = self._convertunix(start)
end = self._convertunix(end)
if start == None:
start = self._get_beginning_ofmonth()
if end == None:
end = int(time.time())
print "Start: {}({}) end: {}({})".format(type(start),start,type(end),end)
res = [ i for i in self.items if i.day_timestamp >= start and
i.day_timestamp <= end ]
print "Got: {}".format(len(res))
return res