def _execute(self): # API URLの作成 url = 'http://' + self.api.api_host + '/?action=' + self.action + '&' + self.build_query(self.params) print url # APIからデータを受け取る data = urllib2.urlopen(url) status = data.code msg = data.msg content = data.read() # パーサ準備 parser = ModelParser() # HTTPエラー処理 if status != 200: if status != 201: try: err_msg = 'Server error message: %s Content: %s' % msg, content except Exception: err_msg = "Hotfile's API error response: status code = %s " % status raise HotfileError(err_msg, data) # パーサへデータを受け渡し result = parser.paser(self, content) return result
def _execute(self): opener = None headers = [ ('User-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.15 Safari/535.1'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), ('Accept-Language', 'en-us;q=0.7,en;q=0.3'), ('Accept-Encoding', 'gzip, deflate'), ('Accept-Charset', 'utf-8;q=0.7,*;q=0.7'), ('Referer', 'http://hotfile.com/') ] if self.login: login_url = 'http://' + self.api.host + '/login.php' login_payload = urllib.urlencode({'returnto':'/', 'user':self.api.username, 'pass':self.api.passwd}) login_cj = cookielib.LWPCookieJar() opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(login_cj), MultipartPostHandler ) urllib2.install_opener(opener) opener.addheaders = headers self.cookie = login_cj retry = self.retry while(retry > 0): try: opener.open(login_url, login_payload) break except: time.sleep(5) retry -= 1 if not opener: if self.cookie: opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cookie), MultipartPostHandler ) else: opener = urllib2.build_opener() opener.addheaders = headers url = 'http://' + self.api.host + '/' + self.web_path payload = urllib.urlencode(self.payload) retry = self.retry while(retry > 0): try: data = opener.open(url, payload) content = data.read() status = data.code msg = data.msg if status != 200: if status != 201: try: err_msg = 'Server error message: %s Content: %s' % msg, content except Exception: err_msg = "Hotfile's API error response: status code = %s " % status raise HotfileError(err_msg, data) break except Exception: time.sleep(5) retry -= 1 parser = ModelParser() result = parser.paser(self, content) return result