import os
import sys
import urllib2
import hashlib
import urlparse
from datetime import datetime, timedelta

from lxml import etree
import config

NAME="RESOURCE"


def log(message):
    if config.LOG:
        args = (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), message)
        print >> sys.stderr, "[XPATH] %s - %s" % args

def md5(data):
    return hashlib.md5(data).hexdigest()

def is_url(url):
    return urlparse.urlparse(url).scheme != ""

def urlcontent(url):

    # If its a url just return as it is
    if not is_url(url):
        with open(url) as f:
            content = f.read()
        return content

    # Use the md5 of the url as the cache file name
    cachefile = os.path.join('.cache', "%s.htm" % md5(url))

    # if the file is dowloaded more than 1 hours ago
    # it'll be redownloaded

    current_dt = datetime.now()
    try:
        file_creation_dt = datetime.fromtimestamp(os.path.getctime(cachefile))
    except OSError:
        # if file doesn't exist we just set an old date
        # which is guranteed to make this file old enough
        file_creation_dt = current_dt - timedelta(hours=4)

    if (current_dt - file_creation_dt) > timedelta(hours=3):
        req = urllib2.Request(url, headers={'User-Agent': config.USER_AGENT})
        content = urllib2.urlopen(req).read()
        with open(cachefile, 'w') as f:
            f.write(content)
    else:
        with open(cachefile, 'r') as f:
            content = f.read()

    return content


def main():
    xpaths, url = xpaths_url()


    root = etree.HTML(urlcontent(url), base_url=url)
    for xpath in xpaths:
       for el in root.xpath(xpath):
           print el
        


def xpaths_url():
    if len(sys.argv) < 3:
        print >> sys.stderr, "XPath and Website sould be passed as command-line argument"
        print >> sys.stderr, "Syntax: webquerymx.py url xpath1 xpath2 xpath3 ..."
        sys.exit(1)

    args = sys.argv
    url = args[1]
    xpaths = args[2:]

    log("XPaths: %s" % xpaths)
    log("URL: %s" % url)
    return xpaths, url

if __name__ == '__main__':
    main()

@@ -27,17 +27,52 @@
 t urllib
-2
+.request, urllib.error, urllib.parse
 %0Aimport 
@@ -85,24 +85,28 @@
 b%0Aimport url
+lib.
 parse%0Afrom d
@@ -183,17 +183,19 @@
 ig%0A%0ANAME
-=
+ = 
 %22RESOURC
@@ -320,32 +320,17 @@
    print
- %3E%3E sys.stderr, 
+(
 %22%5BXPATH%5D
@@ -345,16 +345,35 @@
 %22 %25 args
+, file=sys.stderr)%0A
 %0A%0Adef md
@@ -408,16 +408,25 @@
 md5(data
+.encode()
 ).hexdig
@@ -432,16 +432,17 @@
 gest()%0A%0A
+%0A
 def is_u
@@ -464,16 +464,20 @@
 turn url
+lib.
 parse.ur
@@ -502,16 +502,17 @@
  != %22%22%0A%0A
+%0A
 def urlc
@@ -1263,17 +1263,24 @@
 = urllib
-2
+.request
 .Request
@@ -1352,17 +1352,24 @@
 = urllib
-2
+.request
 .urlopen
@@ -1376,24 +1376,49 @@
 (req).read()
+.decode(encoding='utf-8')
 %0A        wit
@@ -1622,17 +1622,16 @@
 _url()%0A%0A
-%0A
     root
@@ -1703,24 +1703,25 @@
 ths:%0A       
+ 
 for el in ro
@@ -1752,25 +1752,18 @@
     
+ 
 print
- el%0A        
+(el)
 %0A%0A%0Ad
@@ -1818,32 +1818,17 @@
    print
- %3E%3E sys.stderr, 
+(
 %22XPath a
@@ -1883,38 +1883,41 @@
 ent%22
-%0A        print %3E%3E sys.stderr, 
+, file=sys.stderr)%0A        print(
 %22Syn
@@ -1964,16 +1964,34 @@
 th3 ...%22
+, file=sys.stderr)
 %0A       
@@ -2145,16 +2145,17 @@
 s, url%0A%0A
+%0A
 if __nam

Upgrade to python 3