home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / imagehost / imgur / imgur.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.1 KB  |  83 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from contextlib import closing
  5. import urllib2
  6. import util
  7. import util.net as net
  8. import util.apitools as apitools
  9. import util.callbacks as callbacks
  10.  
  11. class ImgurApi(object):
  12.     api_key = '086876da7b51c619ffddb16eaca51400'
  13.     BASE = 'http://imgur.com/'
  14.     
  15.     def API_BASE(self):
  16.         return net.httpjoin(self.BASE, 'api/')
  17.  
  18.     API_BASE = property(API_BASE)
  19.     
  20.     def upload(self, image):
  21.         pass
  22.  
  23.     upload = apitools.apicall('data', name = 'upload', format = 'json')(upload)
  24.     
  25.     def get_response_handler(self, method_call, callback):
  26.         if method_call.spec.format == 'json':
  27.             
  28.             def response_parser(data):
  29.                 import simplejson as json
  30.                 return {
  31.                     'url': json.loads(data)['rsp']['image']['imgur_page'] }
  32.  
  33.         else:
  34.             
  35.             def response_parser(data):
  36.                 import lxml.etree as ET
  37.                 doc = ET.fromstring(data)
  38.                 
  39.                 def keyval(node):
  40.                     if node.text and node.text.strip():
  41.                         return (node.tag, node.text.strip())
  42.                     if node.getchildren():
  43.                         return (node.tag, dict(map(keyval, node.getchildren())))
  44.                     if node.attrib:
  45.                         return (node.tag, dict(node.attrib))
  46.  
  47.                 print data
  48.                 res = dict((keyval(doc),)).get('rsp', { })
  49.                 print res
  50.                 return res
  51.  
  52.         
  53.         def response_handler(resp):
  54.             
  55.             try:
  56.                 closing(resp).__enter__()
  57.                 
  58.                 try:
  59.                     data = resp.read()
  60.                 finally:
  61.                     pass
  62.  
  63.                 api_response = response_parser(data)
  64.             except Exception:
  65.                 e = None
  66.                 return callback.error(e)
  67.  
  68.             callback.success(api_response)
  69.  
  70.         return response_handler
  71.  
  72.     
  73.     def send_method(self, call, callback = None):
  74.         url = net.UrlQuery(call.get_endpoint(self.API_BASE) + '.' + call.spec.format)
  75.         params = dict((lambda .0: for name, arg in .0:
  76. (name, arg.value))(call.bound_args.items()))
  77.         params['key'] = self.api_key
  78.         req = urllib2.Request(url, params)
  79.         util.threaded(urllib2.urlopen)(req, callback = callback)
  80.  
  81.     send_method = callbacks.callsback(send_method)
  82.  
  83.