def http_request(self, method, action, query=None, urlencoded=None, callback=None, timeout=3): LOG.info('method=%s url=%s query=%s' % (method, action, query)) try: res = None if method == "GET": res = requests.get(action, headers=self.headers, cookies=self.cookies, timeout=timeout) elif method == "POST": res = requests.post(action, data=query, headers=self.headers, cookies=self.cookies, timeout=timeout) elif method == "POST_UPDATE": res = requests.post(action, data=query, headers=self.headers, cookies=self.cookies, timeout=timeout) self.cookies.update(res.cookies.get_dict()) content = show_requests_progress(res, self.signal_load_progress) content_str = content.decode('utf-8') content_dict = json.loads(content_str) return content_dict except Exception as e: LOG.error(str(e)) return {"code": 408}
def save_song(self, song_model, filepath): res = requests.get(song_model['url'], stream=True) if res.status_code == 200: print('status code 200') content = show_requests_progress(res, self.signal_download_progress) with open(filepath, 'wb') as f: f.write(content) LOG.info("save song %s successful" % song_model['name']) return True LOG.info("save song %s failed" % song_model['name']) return False