Commits

Paul McLanahan committed b27daa7

Fixed bugs and got it working with Linode.

Comments (0)

Files changed (1)

 import os
 import re
 import sys
-import urlparse
 
 from ConfigParser import RawConfigParser
 from urllib2 import urlopen
+from urllib import urlencode
 
 try:
     import simplejson as json
 LOG_FILE = '/var/log/linode_ddns.log'
 CONFIG_FILE = os.environ.get('LINODEDDNS_CONFIG', '/etc/linode_ddns.conf')
 
+logging.basicConfig(filename=LOG_FILE,
+                    level=logging.INFO,
+                    format='%(asctime)s %(levelname)s %(message)s')
 
 class Linode(object):
 
 
     def request(self, action, **data):
         data.update({'api_key':self.api_key, 'api_action':action})
-        urldata = urlparse(data)
-        response = urlopen(self.api_url, data=urlparse(data), timeout=5)
-        return json.loads(response.read())
+        logging.debug('REQUEST: %s' % data)
+        response = urlopen(self.api_url, data=urlencode(data), timeout=5)
+        rtext = response.read().strip()
+        logging.debug('RESPONSE: %s' % rtext)
+        return json.loads(rtext)
 
     def get_all_domains(self):
-        return self.request('domain.list')
+        return self.request('domain.list').get('DATA', [])
 
     def get_domain_id(self, dname):
-        data = self.get_all_domains()
-        for domain in data['DATA']:
+        domains = self.get_all_domains()
+        for domain in domains:
             if domain['DOMAIN'] == dname:
                 return domain['DOMAINID']
 
 
 
 def main(cfg_file=CONFIG_FILE):
-    logging.basicConfig(filename=LOG_FILE,
-                        level=logging.INFO,
-                        format='%(asctime)s %(levelname)s %(message)s')
 
     def print_result(message='success'):
         print "ContentType: text/plain\n"
     if not os.path.exists(CONFIG_FILE):
         print_result('no config file')
 
-    config = RawConfigParser({'names':None, 'ip_check_url':IP_CHECK_URL})
+    config = RawConfigParser()
     config.read(cfg_file)
     key = config.get('linode', 'api_key')
     domain = config.get('linode', 'domain_name')