-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdate_rule.py
256 lines (214 loc) · 10.4 KB
/
date_rule.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
__author__ = 'Jake Miller (@LaconicWolf)'
__date__ = '20190316'
__version__ = '0.01'
__description__ = """Creates a rule file that will append digits that may
map to dates in popular formats that may be appended to files. You can also
append or prepend characters or words, capitalize the first character of the
words, and double the word. Dates are currently MDY, but can be DMY or YMD"""
import argparse
import time
try:
import pandas as pd
except ImportError:
print("This script requires Pandas. Try 'pip install pandas', or 'python -m pip install pandas', or do an Internet search for installation instructions.")
exit()
def create_2_digit_year():
"""Returns a list of strings from 00-99."""
string_year_range = []
for i in range(0, 100):
i = str(i)
if len(i) < 2:
i = '0' + i
string_year_range.append(i)
return string_year_range
def create_month_day(start_year, end_year):
"""Returns a list of from 0101-1231."""
month_day_list = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
month_day_list.append(''.join(str(time).split(' ')[0].split('-')[1:]))
return list(set(month_day_list))
def create_month_day_2_dig_year(start_year, end_year):
"""Returns a list of from 010100-123199."""
month_day_year = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
year = str(time).split('-')[0][-2:]
month_day = ''.join(str(time).split(' ')[0].split('-')[1:])
month_day_year.append(month_day + year)
return list(set(month_day_year))
def create_month_day_4_dig_year(start_year, end_year):
"""Returns a list of from 01011900-12312099."""
month_day_year = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
year = ''.join(str(time).split('-')[0])
month_day = ''.join(str(time).split(' ')[0].split('-')[1:])
month_day_year.append(month_day + year)
return list(set(month_day_year))
def create_day_month(start_year, end_year):
"""Returns a list of from 0101-3112."""
day_month_list = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
day = str(time).split(' ')[0].split('-')[2]
month = str(time).split(' ')[0].split('-')[1]
day_month_list.append(day + month)
return list(set(day_month_list))
def create_day_month_2_dig_year(start_year, end_year):
"""Returns a list of from 010100-311299."""
day_month_year = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
day = str(time).split(' ')[0].split('-')[2]
month = str(time).split(' ')[0].split('-')[1]
year = str(time).split('-')[0][-2:]
day_month_year.append(day + month + year)
return list(set(day_month_year))
def create_day_month_4_dig_year(start_year, end_year):
"""Returns a list of from 01011900-31122099."""
day_month_year = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
day = str(time).split(' ')[0].split('-')[2]
month = str(time).split(' ')[0].split('-')[1]
year = str(time).split('-')[0]
day_month_year.append(day + month + year)
return list(set(day_month_year))
def create_2_dig_year_month_day(start_year, end_year):
"""Returns a list of from 000101-991231."""
year_month_day = []
times = pd.date_range('01-01-2000', '12-31-2099')
for time in times:
year = str(time).split('-')[0][-2:]
month_day = ''.join(str(time).split(' ')[0].split('-')[1:])
year_month_day.append(year + month_day)
return list(set(year_month_day))
def create_4_dig_year_month_day(start_year, end_year):
"""Returns a list of from 19000101-20991231."""
year_month_day = []
times = pd.date_range('01-01-{}'.format(start_year), '12-31-{}'.format(end_year))
for time in times:
year_month_day.append(str(time).split(' ')[0].replace('-', ''))
return list(set(year_month_day))
def main():
print("[*] Generating dates...")
# 00-99
two_dig_year = create_2_digit_year()
# 0101-1231
month_day = create_month_day(start_year, end_year)
# 010100-123199
month_day_2_dig_year = create_month_day_2_dig_year(start_year, end_year)
# 01011900-12312099
month_day_4_dig_year = create_month_day_4_dig_year(start_year, end_year)
# 0101-3112
day_month = create_day_month(start_year, end_year)
# 010100-311299
day_month_2_dig_year = create_day_month_2_dig_year(start_year, end_year)
# 01011900-31122099
day_month_4_dig_year = create_day_month_4_dig_year(start_year, end_year)
# 000101-991231
two_dig_year_month_day = create_2_dig_year_month_day(start_year, end_year)
# 19000101-20991231
four_dig_year_month_day = create_4_dig_year_month_day(start_year, end_year)
all_dates = two_dig_year
if date_time_format == 'MDY':
all_dates += month_day
all_dates += month_day_2_dig_year
all_dates += month_day_4_dig_year
if date_time_format == 'YMD':
all_dates += month_day
all_dates += two_dig_year_month_day
all_dates += four_dig_year_month_day
if date_time_format == 'DMY':
all_dates += month_day
all_dates += two_dig_year_month_day
all_dates += four_dig_year_month_day
# Write to rule file
print("[*] Writing rules...")
with open(filename, 'w') as fh:
for date in all_dates:
# Dates only
fh.write('${}\n'.format('$'.join(list(date))))
if args.title:
# Capitalizes first letter then dates
fh.write('c${}\n'.format('$'.join(list(date))))
if args.title and args.double:
# Capitalizes first letter, doubles, then dates
fh.write('cd${}\n'.format('$'.join(list(date))))
# Doubles, dates, then capitalizes first letter
fh.write('d${}c\n'.format('$'.join(list(date))))
if appensions:
for item in appensions:
# Writes dates with items
fh.write('${}${}\n'.format('$'.join(list(date)), '$'.join(list(item))))
if args.title:
# Capitalizes, dates, and writes with items
fh.write('c${}${}\n'.format('$'.join(list(date)), '$'.join(list(item))))
if args.double:
# Capitalizes first letter, doubles, then dates with items
fh.write('cd${}${}\n'.format('$'.join(list(date)), '$'.join(list(item))))
if args.title and args.double:
# Capitalizes first letter, then doubles, dates and items
fh.write('cd${}${}\n'.format('$'.join(list(date)), '$'.join(list(item))))
# Doubles, dates and items, then capitalizes first letter
fh.write('d${}${}c\n'.format('$'.join(list(date)), '$'.join(list(item))))
if prepensions:
for item in appensions:
# Writes items with dates
fh.write('^{}${}\n'.format('^'.join(list(item)[::-1]), '$'.join(list(date)) ))
if args.title:
# Capitalizes, dates, and writes with items
fh.write('c^{}${}\n'.format('^'.join(list(item)[::-1]), '$'.join(list(date)) ))
if args.double:
# Capitalizes first letter, doubles, then dates with items
fh.write('cd^{}${}\n'.format('^'.join(list(item)[::-1]), '$'.join(list(date)) ))
if args.title and args.double:
# Capitalizes first letter, then doubles, dates and items
fh.write('cd^{}${}\n'.format('^'.join(list(item)[::-1]), '$'.join(list(date)) ))
# Doubles, dates and items, then capitalizes first letter
fh.write('d^{}${}c\n'.format('^'.join(list(item)[::-1]), '$'.join(list(date)) ))
print("[+] Complete! Rule file written to {}".format(filename))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--format",
nargs='?',
const='MDY',
default='MDY',
choices=['DMY', 'MDY', 'YMD'],
help="Choose date/time format. Default MDY")
parser.add_argument("-d", "--double",
help="Double word before appending date",
action="store_true")
parser.add_argument("-t", "--title",
help="title case word",
action="store_true")
parser.add_argument("-a", "--append",
nargs="*",
help="Specify characters or words to add to end of dates, separated by spaces. (-a ! !! !!! !@#$")
parser.add_argument("-p", "--prepend",
nargs="*",
help="Specify characters or words to add to the beginning of the word, separated by spaces. (-p ! !! !!! !@#$")
parser.add_argument("-s", "--start_year",
help="Specify 4 digit year as a starting date range. Default is 1970.")
parser.add_argument("-e", "--end_year",
help="Specify 4 digit year as a starting date range. Default is current year + 1.")
parser.add_argument("-o", "--filename",
help="Specify a filename to write to. Default is date_rule.rule")
args = parser.parse_args()
if args.start_year:
if not args.start_year.isdigit() or len(args.start_year) != 4:
print('[-] Start year must be a 4 digit year (-s 2005). Default is 1970')
exit()
start_year = args.start_year if args.start_year else '1970'
if args.end_year:
if not args.end_year.isdigit() or len(args.end_year) != 4:
print('[-] End year must be a 4 digit year (-e 2019). Default is current year + 1')
exit()
date_time_format = args.format
end_year = args.end_year if args.end_year else str(time.localtime().tm_year + 1)
appensions = args.append if args.append else None
prepensions = args.prepend if args.prepend else None
filename = args.filename if args.filename else 'date_rule.rule'
main()