-
+ 32C82B3D22623BD85715E1B568BEC222B708CF817F00558D41DAB821C4B1769B7FA78465CB19671146C780041090AB6595233BA7DFA260C6B1B1D413AC158443
logotron/logconverters/znc2tmsr/convert.py
(0 . 0)(1 . 58)
35 #!/usr/bin/env python
36
37 from os import listdir
38 from os.path import isfile, join
39 from datetime import datetime
40
41 # Set the dir you'd like to eat, the suffix of each file, and the filename to shit into
42 dir_to_eat = '/path/to/eat/dir/'
43 dir_files_end_with = '.log'
44 shit_into = '/path/to/shitfile.txt'
45
46 # Set the top limit of the line index
47 archive_top_inx = 999999
48
49 # Leave these variables alone
50 archive_bottom_inx = 0
51 dir_data = [f for f in listdir(dir_to_eat) if isfile(join(dir_to_eat, f))]
52 logline_array = []
53
54 # Sort the dir_data in chronological order
55 date_format = '%Y-%m-%d'
56 dir_data_chrono = []
57 for logfile in dir_data:
58 if dir_files_end_with in logfile:
59 logdate = logfile.replace('.log','')
60 dir_data_chrono.append(logdate)
61 dir_data_chrono = sorted(dir_data_chrono, key=lambda d:datetime.strptime(d,date_format))
62
63 # Shit lines into a single file
64 def shit_lines(line_inx):
65 f = open(shit_into, 'a')
66 for line in logline_array:
67 indexed_line = ('%s;%s') % (line_inx,line)
68 f.write(indexed_line)
69 line_inx = line_inx + 1
70 f.close()
71
72 # Eat each file in the dir
73 for logfile in dir_data_chrono:
74 logfile_full_path = join(dir_to_eat,('%s.log' % logfile))
75 with open(logfile_full_path, 'r') as logfile_data:
76 for logline in logfile_data:
77 znc_timestamp = ('%s %s') % (logfile, logline[1:9])
78 znc_datetimestamp = datetime.strptime(znc_timestamp, '%Y-%m-%d %H:%M:%S')
79 epoch_datetimestamp = znc_datetimestamp.strftime('%s')
80 if '<' in logline[11:12]:
81 #normal line
82 speaker = logline[12:logline.find('>')]
83 logline_array.append(('%s;%s;%s') % (epoch_datetimestamp, speaker, logline[logline.find('>')+2:]))
84 elif '*' in logline[12:13]:
85 #connectolade; ignore
86 pass
87 else:
88 #action
89 logline_array.append(('%s;*;%s') % (epoch_datetimestamp, logline[13:]))
90 archive_bottom_inx = archive_top_inx - (len(logline_array) - 1)
91 shit_lines(archive_bottom_inx)
92 logline_array = []