]> khamidou — GitHub mirrors - tunnel.git/commitdiff
implementing deltas support and the like
authorkhamidou <khamidou@gmail.com>
Mon, 27 Dec 2010 17:31:04 +0000 (18:31 +0100)
committerkhamidou <khamidou@gmail.com>
Mon, 27 Dec 2010 17:31:04 +0000 (18:31 +0100)
README
tunnel

diff --git a/README b/README
index 0a876fde40ac94a007be993df90a8df7bb3767f2..a8051e29e2f51f2290c4e77bac75763950a70332 100644 (file)
--- a/README
+++ b/README
@@ -1,3 +1,10 @@
-This is tunnel.
+This is tunnel, a personal proxy program which lets you monitor how much time you
+effectively spend on various websites.
 
-To run it, just type "./tunnel"
\ No newline at end of file
+To run it, just type "./tunnel"
+
+How does it compute the time spent ?
+
+To compute how much time you have spent on a website, tunnel uses a simple 
+heuristic : it considers that you're actively surfing a website if you're
+clicking on link on the same domain every five minutes
diff --git a/tunnel b/tunnel
index e0a16b35f256dac6a650f0235987ee70f60b7644..f1052b9f83f74108fa5f1c5d6af23ac3b2a6d53a 100755 (executable)
--- a/tunnel
+++ b/tunnel
@@ -2,52 +2,58 @@
 
 import BaseHTTPServer
 import urllib2
-import re
+import datetime
+from urlparse import urlparse
 
 base_url = r"http://0.0.0.0:8000/"
+start_time = datetime.datetime.now()
+sites_dict = {}
 
-
-def rewrite_link(link):
-    pass
+class Domain(object):
+       def __init__(self, name):
+               self.name = name
+               self.time_spent = datetime.timedelta()
+               self.last_access = datetime.datetime.now()
+       
 
 class TunnelHandler(BaseHTTPServer.BaseHTTPRequestHandler):
-    def do_GET(self):
-        if self.path[0] == '/':
-            self.path = self.path[1:]
-
-        try:
-            fd = urllib2.urlopen(self.path)
-        except urllib2.URLError:
-            self.wfile.write("400 : The URL " + self.path + " isn't reachable")
-            return
-
-        if fd.info().type == "text/html":
-            content = fd.read()
-            def reg_repl(m):
-                return base_url + "\0"
-
-            # A replacement function for a relative path (like /~karim)
-            def relative_repl(m):
-                if m.group(1).find('http') != -1:
-                    print "guh"
-                    print "rewritten to : " + base_url + m.group(1)
-                    return base_url + m.group(1)
-
-                print "rewritten to : " "http://" + self.path + "/" +  m.group(1)
-                return base_url + self.path + "/" + m.group(1)
-
-            # Relative urls
-            re.sub("href=\"([a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)\"", relative_repl, content)
-
-            # Regular urls
-            # re.sub("href=\"http:\/\/([a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)\"", reg_repl, content)
-
-            self.wfile.write(content)
-        else:
-            self.wfile.write(fd.read())
+       def do_GET(self):
+               url = urlparse(self.path)
+               if url.netloc != "":
+                       domain = url.netloc
+                       if not sites_dict.has_key(domain):
+                               sites_dict[domain] = Domain(domain)
+                       
+                       # Increase the timedelta in the dict by
+                       # the time spent between the last request
+                       # and now
+                       if self.last_request == None:
+                               self.last_request = datetime.datetime.now()
+               
+                       sites_dict[domain].time_spent += datetime.datetime.now() - self.last_request
+                       self.last_request = datetime.datetime.now()
+
+               try:
+                   fd = urllib2.urlopen(self.path)
+               except urllib2.URLError:
+                   self.wfile.write("400 : The URL " + self.path + " isn't reachable")
+                   return
+
+               print_time_spent(sites_dict)
+               self.wfile.write(fd.read())
+
+       def __init__(self, request, client_address, server):
+               self.last_request = None
+               BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
+
+def print_time_spent(d):
+       print ""
+       for e in d:
+               print "%s : spent %d seconds" % (d[e].name, d[e].time_spent.seconds)
 
 if __name__ == "__main__":
     server_address = ('', 8000)
     httpd = BaseHTTPServer.HTTPServer(server_address, TunnelHandler)
+    print "tunnel proxy running on http://localhost:8000"
     httpd.serve_forever()