class QQWeiboFetcher():
  '''init'''
  def __init__(self, app_key, app_secret, callback_url, open_id=None):
        self.app_key = str(app_key)
        self.app_secret = str(app_secret)
        self.open_id = open_id
        #self.qq_login = str(qq_login)
        #self.qq_password = str(qq_password)
        self.callback_url = str(callback_url)
        self.api_reset_time = 0
        self.remaining_api_hits = 0

  def authorize(self):
    #self.open_id = open_id
    self.client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
    url = self.client.get_authorize_url()
    print "authorize url=%s", url
    access_code = raw_input("paste access code:")
    if not self.open_id:
      self.open_id = raw_input("paste open id:")
    #open_key = raw_input("paste open key:")

    r = self.client.request_access_token(access_code)
    access_token = r.access_token  # access token. e.g. abc123xyz456
    expires_in = r.expires_in      # token expires in
    self.client.set_access_token(r.access_token, self.open_id, r.expires_in)
    print "expires_in=" , time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expires_in)) ,
    print "current_time=", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( time.time() ))
  
  #renew access_token
  def renewToken(self):
    r = self.client.refresh_token(self.client.access_token)
    self.client.set_access_token(r.access_token, fetcher.client.open_id, r.expires_in)
    print "renew access_token expires_in=" , time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expires_in)) ,
    print "current_time=", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( time.time() ))
  
  #send API request after authentication
  def run(self, api_name, params_dict) :
    #parse parameter string into dictionary obj.
    ret = self.client.__getattr__(api_name)(**params_dict)
    return json.dumps(ret,ensure_ascii=False)