Guest User

Untitled

a guest
Feb 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import psycopg2
  4. import re
  5. import time
  6. import psyco
  7. psyco.full()
  8. connection = psycopg2.connect("dbname='testing' user='test' host='localhost' password='pass'");
  9. cursor = connection.cursor()
  10. file_list = os.listdir("logs")
  11. def find_date(date):
  12. cursor.execute("SELECT * FROM days WHERE date = '%s'" % time.strftime("%d-%m-%Y", date))
  13. return cursor.fetchone()
  14.  
  15. def find_hostname(hostname):
  16. cursor.execute("SELECT * FROM hostnames WHERE hostname = '%s'" % hostname)
  17. return cursor.fetchone()
  18.  
  19. def find_person(name):
  20. name = name.replace("\\", "\\\\")
  21. cursor.execute("SELECT * FROM people WHERE name = '%s'" % name)
  22. return cursor.fetchone()
  23.  
  24. def log_hostname(name, person_id):
  25. hostname = find_hostname(name)
  26. if hostname == None:
  27. cursor.execute("INSERT INTO hostnames (hostname) VALUES ('%s')" % name)
  28. connection.commit()
  29. hostname = find_hostname(name)
  30. hostname_id = str(hostname[0])
  31. cursor.execute("SELECT * FROM hostnames_people WHERE hostname_id = '%s' AND person_id = '%s'" % (hostname_id, person_id))
  32. join = cursor.fetchone()
  33. if join == None:
  34. cursor.execute("INSERT INTO hostnames_people (hostname_id, person_id) VALUES ('%s', '%s')" % (hostname_id, person_id))
  35. connection.commit()
  36. return hostname[0]
  37.  
  38. def log_date(date):
  39. d = find_date(date)
  40. if d == None:
  41. cursor.execute("INSERT INTO days (date) VALUES ('%s')" % time.strftime("%d-%m-%Y", date))
  42. connection.commit()
  43.  
  44. def log_person(name):
  45. name = re.sub('<', '', name)
  46. name = re.sub('>', '', name)
  47.  
  48. person = find_person(name)
  49. if person == None:
  50. cursor.execute("INSERT INTO people (name) VALUES ('%s')" % name.replace("\\", "\\\\"))
  51. connection.commit()
  52. person = find_person(name)
  53. return str(person[0])
  54.  
  55. def time_to_string(time):
  56. return time.strftime("%d-%m-%Y %H:%M", t)
  57.  
  58. def sanitize(string):
  59. return unicode(" ".join(string).replace("\\", "\\\\").replace('"', '\\"').replace("'", "\\'"), "iso-8859-1")
  60.  
  61. channel = '#rubyonrails'
  62. for file in file_list:
  63. if os.path.isdir(file) == False:
  64. f = open(os.path.join("logs", file))
  65. print file
  66. date = re.sub('ror.log.','', file)
  67. lines = f.readlines()
  68. line_count = 0
  69. time_taken = time.time()
  70. log_date(time.strptime(date, "%Y%m%d"))
  71. for line in lines:
  72. line_count += 1
  73. if line_count % 100 == 0:
  74. print "100 lines done in: " + str(time.time() - time_taken)
  75. time_taken = time.time()
  76. l = re.sub(r'\n$', '', line).split(" ")
  77. t = time.strptime(date + " " + re.sub(r'\[|\]','',l[0]), "%Y%m%d %H:%M")
  78. # when a person does /me
  79. if l[1] == "Action:":
  80. person_id = log_person(l[2])
  81. message = l[3:len(l)]
  82. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel, message) VALUES ('%s', 'part', '%s' , '%s', '%s')" % (person_id, time_to_string(time), channel, sanitize(message)))
  83. # when a person parts..
  84. elif len(l) >= 5 and (l[4] == channel or l[4] == channel + "." or l[1] == channel or l[4] == "irc:") and re.match(r'<', l[1]) == None:
  85. log_hostname(l[2], person_id)
  86. if l[4] == "irc:":
  87. person_id = log_person(l[1])
  88. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel) VALUES ('%s', 'quit', '%s', '%s')" % (person_id, time_to_string(time), channel))
  89. if l[3] == 'left' and l[4] == channel:
  90. person_id = log_person(l[1])
  91. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel) VALUES ('%s', 'part', '%s' , '%s')" % (person_id, time_to_string(time), channel))
  92. if l[3] == 'joined':
  93. person_id = log_person(l[1])
  94. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel) VALUES ('%s', 'join', '%s' , '%s')" % (person_id, time_to_string(time), channel))
  95. elif " ".join(l[2:4]) == "kicked from " + channel:
  96. person_id = log_person(l[1])
  97. other_person_id = log_person(l[6])
  98. cursor.execute("INSERT INTO chats (person_id, other_person_id, message_type, created_at, channel, message) VALUES ('%s','%s', 'kick', '%s' , '%s', '%s')" % (person_id, other_person_id, time_to_string(time), channel, sanitize(t[7:len(t)])))
  99. elif l[1] == channel + ":" and " ".join(l[2:3]) == "mode change":
  100. person_id = log_person(l[7].split('!')[0])
  101. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel, message) VALUES ('%s', 'mode', '%s' ,'%s', '%s')" % (person_id, time_to_string(time), channel, t[4:5]))
  102. elif " ".join(l[1:2]) == "Nick change:":
  103. person_id = log_person(l[3])
  104. other_person_id = log_person(l[5])
  105. cursor.execute("INSERT INTO chats (person_id, other_person_id, message_type, created_at, channel) VALUES ('%s','%s', 'nick-change','%s' , '%s')" % (person_id, other_person_id, time_to_string(time), channel))
  106. else:
  107. person_id = log_person(l[1])
  108. cursor.execute("INSERT INTO chats (person_id, message_type, created_at, channel, message) VALUES ('%s', 'message', '%s' , '%s', '%s')" % (person_id, time_to_string(time), channel, sanitize(l[2:len(l)])))
  109. cursor.execute("UPDATE people SET chats_count = chats_count + 1 WHERE id = '%s'" % person_id)
  110. connection.commit()
  111. connection.close()
Add Comment
Please, Sign In to add comment