borsha06

task_3.py

May 31st, 2021
1,187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. from datetime import datetime
  2. from collections import Counter
  3.  
  4.  
  5. def read_line(s1):
  6.     fields = s1.split(' ')
  7.     request_time = fields[1][1:-1]
  8.     datetime_object = (datetime.strptime(request_time, '%Y-%m-%dT%H:%M:%S%z'))
  9.     return fields[0], datetime_object
  10.  
  11.  
  12. def read_line_2(s1):
  13.     fields = s1.split(' ')
  14.     request_time = fields[0][1:- 1]
  15.     datetime_object = (datetime.strptime(request_time, '%Y-%m-%dT%H:%M:%S%z'))
  16.     return datetime_object, fields[2]
  17.  
  18.  
  19. def check(time_left, time_right):
  20.     check_time = datetime(time_right.year, time_right.month, time_right.day, time_right.hour, time_right.minute,
  21.                           time_right.second)
  22.     if time_left.year == time_right.year and time_left.month == time_right.month and time_left.day == time_right.day and time_left.hour == time_right.hour and time_left.minute == time_right.minute and time_left.second >= time_right.second - 5:
  23.         return check_time
  24.  
  25.  
  26. def main():
  27.     file_right = open('intersec/right.txt', 'r')
  28.     file_left = open('intersec/left.txt', 'r')
  29.     date_time = []
  30.     final_ip = []
  31.  
  32.     for line in file_right:
  33.         time_date, web = read_line_2(line)
  34.         if 'tv-movie.de' in web and 21 <= time_date.hour <= 22:
  35.             date_time.append(time_date)
  36.     print('from right files\n')
  37.     # for date in date_time:
  38.     #     print(date)
  39.     for line in file_left:
  40.         ip_left, request_time = read_line(line)
  41.         for time in date_time:
  42.             if check(request_time, time):
  43.                 print(ip_left, request_time)
  44.                 final_ip.append(ip_left)
  45.     print(Counter(final_ip))
  46.  
  47.     file_right.close()
  48.     file_left.close()
  49.  
  50.  
  51. if __name__ == '__main__':
  52.     main()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment