# -*- coding: utf-8 -*-

import dns.resolver
import urllib2
import re


def check_dns_txt(domain, prefix, code):
    """
    Validates a domain by checking that {prefix}={code} is present in the TXT DNS record
    of the domain to check.

    Returns true if verification suceeded.
    """
    token = '{}={}'.format(prefix, code)
    try:
        for rr in dns.resolver.query(domain, 'TXT'):
            if token in rr.to_text():
                return True
    except:
        pass
    return False


def check_dns_cname(domain, prefix, code):
    """
    Validates a domain by checking the existance of the CNAME record on the domain as:
    {prefix}-{code}.{domain} pointing to a domain (usually yours) which the host is {prefix}
    (i.e.: {prefix}.yourdomain.com).

    Returns true if verification suceeded.
    """
    fqdn = '{}-{}.{}'.format(prefix, code, domain)
    try:
        for rr in dns.resolver.query(fqdn, 'CNAME'):
            if rr.to_text().startswith(prefix + '.'):
                return True
    except:
        pass
    return False

def search_meta_tag(html_doc, prefix, code):
  """
  Checks whether the html_doc contains a meta matching the prefix & code
  """
  meta = re.compile('<meta\s+name=([\'\"]){0}\\1\s+content=([\'\"]){1}\\2\s*/?>|<meta\s+content=([\'\"]){1}\\3\s+name=([\'\"]){0}\\4\s*/?>'.format(prefix, code),
                    flags=re.MULTILINE | re.IGNORECASE)
  m = meta.search(html_doc)
  if m:
    head = re.search(r'</head>', html_doc, flags=re.IGNORECASE)
    if head and m.start() < head.start():
      return True
  return False


def check_meta_tag(domain, prefix, code):
    """
    Validates a domain by checking the existance of a <meta name="{prefix}" content="{code}">
    tag in the <head> of the home page of the domain using either HTTP or HTTPs protocols.

    Returns true if verification suceeded.
    """
    url = '://{}'.format(domain)
    for proto in ('http', 'https'):
      try:
          res = urllib2.urlopen(proto + url)
          if res.code == 200:
              content = res.read()
              res.close()
              return search_meta_tag(content, prefix, code)
          else:
              res.close()
      except:
          pass
    return False


def check_html_file(domain, prefix, code):
    """
    Validates a domain by checking the existance of a file named {code}.html at the root of the
    website using either HTTP or HTTPS protocols. The file must contain {prefix}={code} in the
    body of the file to ensure the host isn't responding 200 to any requests.

    Returns true if verification suceeded.
    """
    url = '://{}/{}.html'.format(domain, code)
    token = '{}={}'.format(prefix, code)
    for proto in ('http', 'https'):
        try:
            res = urllib2.urlopen(proto + url)
            if res.code == 200:
                content = res.read()
                res.close()
                if token in content:
                    return True
            else:
                res.close()
        except:
            pass
    return False

@@ -1054,16 +1054,17 @@
  False%0A%0A
+%0A
 def sear
@@ -1100,22 +1100,26 @@
  code):%0A
+  
   %22%22%22%0A
+  
   Checks
@@ -1183,22 +1183,26 @@
  & code%0A
+  
   %22%22%22%0A
+  
   meta =
@@ -1375,16 +1375,18 @@
         
+  
 flags=re
@@ -1413,16 +1413,18 @@
 RECASE)%0A
+  
   m = me
@@ -1443,16 +1443,18 @@
 ml_doc)%0A
+  
   if m:%0A
@@ -1457,16 +1457,20 @@
  m:%0A    
+ 
+   
 head = r
@@ -1517,24 +1517,28 @@
 IGNORECASE)%0A
+    
     if head 
@@ -1569,24 +1569,30 @@
 rt():%0A      
+ 
+     
 return True%0A
@@ -1591,16 +1591,18 @@
 rn True%0A
+  
   return
@@ -1968,29 +1968,33 @@
 ps'):%0A      
+  
 try:%0A
+  
           re
@@ -2028,32 +2028,34 @@
  url)%0A          
+ 
+ 
 if res.code == 2
@@ -2050,32 +2050,34 @@
 es.code == 200:%0A
+  
               co
@@ -2087,32 +2087,34 @@
 nt = res.read()%0A
+  
               re
@@ -2133,24 +2133,26 @@
             
+  
 return searc
@@ -2187,38 +2187,42 @@
 code)%0A          
+  
 else:%0A
+  
               re
@@ -2233,32 +2233,34 @@
 ose()%0A      
+  
 except:%0A
           pa
@@ -2239,32 +2239,34 @@
         except:%0A
+  
           pass%0A 

Fix wrong indentation