class PosterLib(object): def __init__(self): headers = ['User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 ' \ '(KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',] self.mycurl = MyCurl(header=headers) self.ting_public_data = {"format": "json"} def api_request(self, url, method="GET", extra_data=dict(), retry_limit=2, **params): ret = None data = {} data.update(extra_data) data.update(params) for key in data: if callable(data[key]): data[key] = data[key]() if isinstance(data[key], (list, tuple, set)): data[key] = ",".join(map(str, list(data[key]))) if isinstance(data[key], unicode): data[key] = data[key].encode("utf-8") start = time.time() try: if method == "GET": if data: query = urllib.urlencode(data) url = "%s?%s" % (url, query) ret = self.mycurl.get(url) elif method == "POST": body = urllib.urlencode(data) ret = self.mycurl.post(url, body) except CurlException, e: if retry_limit == 0: # print("API request error: url=%s error=%s", url, e) return dict(result="network_error") else: retry_limit -= 1 return self.api_request(url, method, extra_data, retry_limit, **params) data = parser_json(ret) # print("API response %s: TT=%.3fs", url, time.time() - start ) return data
class DoubanFM(Logger): def __init__(self): headers = ['User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 ' \ '(KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',] self.mycurl = MyCurl(header=headers) self.explore_data = {} self.mine_data = {} self.username = "" self.password = "" self.__uid = "" self.__channel = "2" def get_public_params(self, type_name=None): params = {} for i in ['aid', 'channel', 'du', 'h', 'r', 'rest', 'sid', 'type', 'uid']: params[i] = "" params['r'] = random.random params['uid'] = self.__uid if type_name is not None: params['type'] = type_name return params def set_login_info(self, username, password=""): self.username = username self.password = password def load_user_cookie(self): cookie_file = utils.get_cookie_file(self.username) self.mycurl.set_cookie_file(cookie_file) def get_user_info(self): ret = self.mycurl.get("https://api.douban.com/v2/user/~me") return utils.parser_json(ret) def check_login(self, load_cookie=True, stage=0): # load cookie. if load_cookie: self.load_user_cookie() ret = self.get_user_info() print ret if ret.get("uid", 0): self.__uid = ret.get("uid") self.logdebug("Login check success!") return True if stage >= 2: self.logdebug("Login check failed!") return False params = {} params["source"] = "simple" params["form_email"] = self.username params["form_password"] = self.password params["remember"] = "on" ret = self.mycurl.post("http://www.douban.com/accounts/login", urllib.urlencode(params)) filter_ret = re.search(r"error=(\d+)", ret) if filter_ret: if filter_ret.group(1) == "1011": captcha_id = self.new_captcha() params["captcha-id"] = captcha_id params["captcha-solution"] = self.get_login_captcha(captcha_id, "s") ret = self.mycurl.post("http://www.douban.com/accounts/login", urllib.urlencode(params)) filter_ret = re.search(r"error=(\d+)", ret) if not filter_ret: self.parser_cookie_file() self.logdebug("login info: %s", ret) # Begin second login check.. if stage == 0: self.logdebug("Begin second login check..") elif stage == 1: self.logdebug("Begin three login check..") return self.check_login(load_cookie=False, stage=stage+1) def check_fm_login(self, load_cookie=True, stage=0): # load cookie. if load_cookie: self.load_user_cookie() ret = self.get_user_info() if ret.get("uid", 0): self.__uid = ret.get("uid") self.logdebug("Login check success!") return True captcha_id = self.new_captcha() params = {} params["captcha_id"] = captcha_id params["captcha_solution"] = self.get_login_captcha(captcha_id) params["source"] = "radio" params["alias"] = self.username params["form_password"] = self.password params["remember"] = "on" ret = self.mycurl.post("http://douban.fm/j/login", urllib.urlencode(params)) ret_data = utils.parser_json(ret) print ret_data if ret_data.get("r", -1) == 0: self.parser_cookie_file(replace=(".fm", ".com")) # self.__uid = ret_data["user_info"]["uid"] self.logdebug("Login check success!") return True if stage >= 2: self.logdebug("Login check failed!") return False self.logdebug("login info: %s", ret) # Begin second login check.. if stage == 0: self.logdebug("Begin second login check..") elif stage == 1: self.logdebug("Begin three login check..") return self.check_fm_login(load_cookie=False, stage=stage+1) def parser_cookie_file(self, replace=(".com", ".fm")): cookie_file = utils.get_cookie_file(self.username) if os.path.exists(cookie_file): with open(cookie_file, "rw+") as fp: new_line = None for line in fp: if line.startswith("#HttpOnly_.douban"): new_line = line.replace(replace[0], replace[1]) break if new_line is not None: fp.write(new_line) def get_login_captcha(self, captcha_id, size="m"): url = "http://www.douban.com/misc/captcha?size=%s&id=%s" % (size, captcha_id) pic_image = utils.get_cache_file("pic") if utils.download(url, pic_image): self.logdebug("Verify code pic download ok!") return raw_input("piz input code > ").strip() def new_captcha(self): ret = self.mycurl.get("http://www.douban.com/j/new_captcha") return ret.strip("\"") def api_request(self, url, method="GET", extra_data=dict(), retry_limit=2, **params): ret = None data = {} data.update(extra_data) data.update(params) for key in data: if callable(data[key]): data[key] = data[key]() if isinstance(data[key], (list, tuple, set)): data[key] = ",".join(map(str, list(data[key]))) if isinstance(data[key], unicode): data[key] = data[key].encode("utf-8") start = time.time() try: if method == "GET": if data: query = urllib.urlencode(data) url = "%s?%s" % (url, query) ret = self.mycurl.get(url) elif method == "POST": body = urllib.urlencode(data) ret = self.mycurl.post(url, body) except CurlException, e: if retry_limit == 0: self.logdebug("API request error: url=%s error=%s", url, e) return dict(result="network_error") else: retry_limit -= 1 return self.api_request(url, method, extra_data, retry_limit, **params) data = utils.parser_json(ret) self.logdebug("API response %s: TT=%.3fs", url, time.time() - start ) return data
class DoubanFM(Logger): def __init__(self): headers = ['User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 ' \ '(KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',] self.mycurl = MyCurl(header=headers) self.explore_data = {} self.mine_data = {} self.username = "" self.password = "" self.__uid = "" self.__channel = "2" def get_public_params(self, type_name=None): params = {} for i in [ 'aid', 'channel', 'du', 'h', 'r', 'rest', 'sid', 'type', 'uid' ]: params[i] = "" params['r'] = random.random params['uid'] = self.__uid if type_name is not None: params['type'] = type_name return params def set_login_info(self, username, password=""): self.username = username self.password = password def load_user_cookie(self): cookie_file = utils.get_cookie_file(self.username) self.mycurl.set_cookie_file(cookie_file) def get_user_info(self): ret = self.mycurl.get("https://api.douban.com/v2/user/~me") return utils.parser_json(ret) def check_login(self, load_cookie=True, stage=0): # load cookie. if load_cookie: self.load_user_cookie() ret = self.get_user_info() print ret if ret.get("uid", 0): self.__uid = ret.get("uid") self.logdebug("Login check success!") return True if stage >= 2: self.logdebug("Login check failed!") return False params = {} params["source"] = "simple" params["form_email"] = self.username params["form_password"] = self.password params["remember"] = "on" ret = self.mycurl.post("http://www.douban.com/accounts/login", urllib.urlencode(params)) filter_ret = re.search(r"error=(\d+)", ret) if filter_ret: if filter_ret.group(1) == "1011": captcha_id = self.new_captcha() params["captcha-id"] = captcha_id params["captcha-solution"] = self.get_login_captcha( captcha_id, "s") ret = self.mycurl.post("http://www.douban.com/accounts/login", urllib.urlencode(params)) filter_ret = re.search(r"error=(\d+)", ret) if not filter_ret: self.parser_cookie_file() self.logdebug("login info: %s", ret) # Begin second login check.. if stage == 0: self.logdebug("Begin second login check..") elif stage == 1: self.logdebug("Begin three login check..") return self.check_login(load_cookie=False, stage=stage + 1) def check_fm_login(self, load_cookie=True, stage=0): # load cookie. if load_cookie: self.load_user_cookie() ret = self.get_user_info() if ret.get("uid", 0): self.__uid = ret.get("uid") self.logdebug("Login check success!") return True captcha_id = self.new_captcha() params = {} params["captcha_id"] = captcha_id params["captcha_solution"] = self.get_login_captcha(captcha_id) params["source"] = "radio" params["alias"] = self.username params["form_password"] = self.password params["remember"] = "on" ret = self.mycurl.post("http://douban.fm/j/login", urllib.urlencode(params)) ret_data = utils.parser_json(ret) print ret_data if ret_data.get("r", -1) == 0: self.parser_cookie_file(replace=(".fm", ".com")) # self.__uid = ret_data["user_info"]["uid"] self.logdebug("Login check success!") return True if stage >= 2: self.logdebug("Login check failed!") return False self.logdebug("login info: %s", ret) # Begin second login check.. if stage == 0: self.logdebug("Begin second login check..") elif stage == 1: self.logdebug("Begin three login check..") return self.check_fm_login(load_cookie=False, stage=stage + 1) def parser_cookie_file(self, replace=(".com", ".fm")): cookie_file = utils.get_cookie_file(self.username) if os.path.exists(cookie_file): with open(cookie_file, "rw+") as fp: new_line = None for line in fp: if line.startswith("#HttpOnly_.douban"): new_line = line.replace(replace[0], replace[1]) break if new_line is not None: fp.write(new_line) def get_login_captcha(self, captcha_id, size="m"): url = "http://www.douban.com/misc/captcha?size=%s&id=%s" % (size, captcha_id) pic_image = utils.get_cache_file("pic") if utils.download(url, pic_image): self.logdebug("Verify code pic download ok!") return raw_input("piz input code > ").strip() def new_captcha(self): ret = self.mycurl.get("http://www.douban.com/j/new_captcha") return ret.strip("\"") def api_request(self, url, method="GET", extra_data=dict(), retry_limit=2, **params): ret = None data = {} data.update(extra_data) data.update(params) for key in data: if callable(data[key]): data[key] = data[key]() if isinstance(data[key], (list, tuple, set)): data[key] = ",".join(map(str, list(data[key]))) if isinstance(data[key], unicode): data[key] = data[key].encode("utf-8") start = time.time() try: if method == "GET": if data: query = urllib.urlencode(data) url = "%s?%s" % (url, query) ret = self.mycurl.get(url) elif method == "POST": body = urllib.urlencode(data) ret = self.mycurl.post(url, body) except CurlException, e: if retry_limit == 0: self.logdebug("API request error: url=%s error=%s", url, e) return dict(result="network_error") else: retry_limit -= 1 return self.api_request(url, method, extra_data, retry_limit, **params) data = utils.parser_json(ret) self.logdebug("API response %s: TT=%.3fs", url, time.time() - start) return data