import socket
from geoip import geolite2


class BearStorage():
    def __init__(self, ip, rawrequest, timestamp, parsed_request, isDetected,
                 hostname):
        self.ip = ip  # String
        self.rawrequest = rawrequest  # String
        self.timestamp = timestamp  # Datetime
        if hasattr(parsed_request, 'path'):
            self.path = parsed_request.path  # String
        else:
            self.path = ""
        if hasattr(parsed_request, 'command'):
            self.command = parsed_request.command  # String
        else:
            self.command = ""
        if hasattr(parsed_request, 'request_version'):
            self.version = parsed_request.request_version  # String
        else:
            self.version = ""
        if hasattr(parsed_request, 'headers'):
            self.headers = parsed_request.headers  # Dictionary
        else:
            self.headers = ""
        if 'User-Agent' in parsed_request.headers.keys():
            self.ua = parsed_request.headers['User-Agent']  # Dictionary
        else:
            self.ua = ""
        self.isDetected = isDetected  # Bool
        self.hostname = hostname  # Bool
        location = geolite2.lookup(ip)
        if location is not None:
            self.country = location.country  # String
            self.continent = location.continent  # String
            self.timezone = location.timezone  # String
        else:
            self.country = ''
            self.continent = ''
            self.timezone = ''
        self.tracert = ""  # TODO
        try:
            self.dnsname = socket.gethostbyaddr(ip)[0]  # String
        except:
            self.dnsname = ""

    def __str__(self):
        output = "hostname: " + self.hostname + "\r\n"
        output += "IP: " + self.ip + "\r\n"
        # output += "rawrequest: " + self.rawrequest + "\r\n"
        output += "timestamp: " + self.timestamp + "\r\n"
        output += "User-Agent: " + self.ua + "\r\n"
        output += "path: " + self.path + "\r\n"
        output += "command: " + self.command + "\r\n"
        output += "version: " + self.version + "\r\n"
        # output += "headers: " + str(self.headers) + "\r\n"
        output += "country: " + self.country + "\r\n"
        output += "continent: " + self.continent + "\r\n"
        output += "timezone: " + self.timezone + "\r\n"
        if self.isDetected:
            output += "Detected: Yes" + "\r\n"
        else:
            output += "Detected: No" + "\r\n"
        return output

    def __repr__(self):
        return self.__str__()

# import subprocess
# host = 'www.microsoft.com'
# p = subprocess.Popen(["tracert", '-d', '-w', '100', host],
#                      stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# while True:
#     line = p.stdout.readline()
#     if not line:
#         break
#     print '-->', line,
# p.wait()

@@ -183,26 +183,16 @@
 .ip = ip
-  # String
 %0A       
@@ -220,26 +220,16 @@
 wrequest
-  # String
 %0A       
@@ -259,20 +259,8 @@
 tamp
-  # Datetime
 %0A   
@@ -343,26 +343,16 @@
 est.path
-  # String
 %0A       
@@ -392,32 +392,24 @@
 %0A        if 
-hasattr(
 parsed_reque
@@ -414,20 +414,28 @@
 uest
-, '
+.
 command
-')
+ is not None
 :%0A  
@@ -481,26 +481,16 @@
 .command
-  # String
 %0A       
@@ -638,26 +638,16 @@
 _version
-  # String
 %0A       
@@ -779,30 +779,188 @@
 .headers
-  # Dictionary
+%0A            if 'User-Agent' in parsed_request.headers.keys():%0A                self.ua = parsed_request.headers%5B'User-Agent'%5D%0A            else:%0A                self.ua = %22%22
 %0A       
@@ -1884,70 +1884,8 @@
 %5Cn%22%0A
-        # output += %22rawrequest: %22 + self.rawrequest + %22%5Cr%5Cn%22%0A
     
@@ -2150,69 +2150,8 @@
 %5Cn%22%0A
-        # output += %22headers: %22 + str(self.headers) + %22%5Cr%5Cn%22%0A
     
@@ -2530,306 +2530,4 @@
 _()%0A
-%0A# import subprocess%0A# host = 'www.microsoft.com'%0A# p = subprocess.Popen(%5B%22tracert%22, '-d', '-w', '100', host%5D,%0A#                      stdout=subprocess.PIPE, stderr=subprocess.STDOUT)%0A# while True:%0A#     line = p.stdout.readline()%0A#     if not line:%0A#         break%0A#     print '--%3E', line,%0A# p.wait()%0A

Remove shitty comments