def request(self, artist, title): if isinstance(artist, unicode): artist = artist.encode(self.locale) if isinstance(title, unicode): title = title.encode(self.locale) try: artist_quote = urllib.quote_plus( unicode(artist, self.locale).encode(self.net_encoder)) title_quote = urllib.quote_plus( unicode(title, self.locale).encode(self.net_encoder)) except: artist_quote = urllib.quote_plus(artist) title_quote = urllib.quote_plus(title) url = self.DUOMI_SEARCH_URL % (title_quote, artist_quote) try: fp = urllib.urlopen(url, None, self.proxy) info_utf8 = fp.read() fp.close() except IOError: return None else: try: raw_dict = parser_json(info_utf8) except: return None else: if "item" in raw_dict: if len(raw_dict["item"]) < 1: return None else: return self.order_results( self.parser(raw_dict["item"]), artist, title) return None
def request_data(self, title, artist): quote_title = urllib.quote(title) quote_artist = urllib.quote(artist) raw_data = public_curl.get(self.TTPOD_SEARCH_URL % (quote_title, quote_artist)) json_data = parser_json(raw_data) lrc_data = json_data.get('data', {}).get('lrc', None) return lrc_data
def request(self, artist, title): if isinstance(artist, unicode): artist = artist.encode(self.locale) if isinstance(title, unicode): title = title.encode(self.locale) try: artist_quote = urllib.quote_plus(unicode(artist, self.locale).encode(self.net_encoder)) title_quote = urllib.quote_plus(unicode(title, self.locale).encode(self.net_encoder)) except: artist_quote = urllib.quote_plus(artist) title_quote = urllib.quote_plus(title) url = self.DUOMI_SEARCH_URL % (title_quote, artist_quote) try: fp = urllib.urlopen(url, None, self.proxy) info_utf8 = fp.read() fp.close() except IOError: return None else: try: raw_dict = parser_json(info_utf8) except: return None else: if "item" in raw_dict: if len(raw_dict["item"]) < 1: return None else: return self.order_results(self.parser(raw_dict["item"]), artist, title) return None
def upload_pic(self, image_file): fp = open(image_file, "rb") image_data = fp.read() fp.close() img_ext = os.path.splitext(image_file)[1] url = "http://upload.tieba.baidu.com/upload/pic" pic_url = "http://imgsrc.baidu.com/forum/pic/item/%s%s" content_type, body = encode_multipart( [("Filename", os.path.split(image_file)[1]), ("fid", "2196998"), ("tbs", self.get_common_imgtbs())], [("file", os.path.split(image_file)[1], image_data)]) req = urllib2.Request(url, data=body, headers={"content-type": content_type}) ret = self.opener.open(req).read() data = utils.parser_json(ret) try: content = { "src": pic_url % (data["info"]["pic_id_encode"], img_ext), "width": data["info"]["fullpic_width"] if int(data["info"]["fullpic_width"]) <= 500 else 500, "height": data["info"]["fullpic_height"] if int(data["info"]["fullpic_height"]) <= 450 else 450, "pic_type": data["info"]["pic_type"] } return content, data["info"]["full_sign1"] except KeyError: return False
def api_request(self, url, method="GET", extra_data=dict(), retry_limit=2, encoding=None, **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() ret = self.curl.request(url, data, method) if ret == None: if retry_limit == 0: logger.debug("API request error: url=%s" % self.curl.url) return dict() else: retry_limit -= 1 return self.api_request(url, method, extra_data, retry_limit, **params) if encoding != None: ret = ret.decode(encoding) data = parser_json(ret) logger.debug("API response %s: TT=%.3fs", self.curl.url, time.time() - start ) return data
def upload_pic(self, image_file): fp = open(image_file, "rb") image_data = fp.read() fp.close() img_ext = os.path.splitext(image_file)[1] url = "http://upload.tieba.baidu.com/upload/pic" pic_url = "http://imgsrc.baidu.com/forum/pic/item/%s%s" content_type, body = encode_multipart( [("Filename", os.path.split(image_file)[1]), ("fid", "2196998"), ("tbs", self.get_common_imgtbs())], [("file", os.path.split(image_file)[1], image_data)]) req = urllib2.Request(url, data=body, headers={"content-type": content_type}) ret = self.opener.open(req).read() data = utils.parser_json(ret) try: content = {"src" : pic_url % (data["info"]["pic_id_encode"], img_ext), "width" : data["info"]["fullpic_width"] if int(data["info"]["fullpic_width"]) <= 500 else 500, "height" : data["info"]["fullpic_height"] if int(data["info"]["fullpic_height"]) <= 450 else 450, "pic_type" : data["info"]["pic_type"] } return content, data["info"]["full_sign1"] except KeyError: return False
def request_userinfo(self): url = "http://musicmini.baidu.com/api/index.php" data = {"ver" : 1, "type" : 2, "format" : "json", "clientver" : self.client_version, "from" : "baidumusic", "bduss" : self.bduss} ret = public_curl.request(url, data) return parser_json(ret)
def restserver_request(self, method, **kwargs): data = {} data.update(self.common_kwargs) data.update(kwargs) data["method"] = method url = "http://tingapi.ting.baidu.com/v1/restserver/ting" ret = public_curl.request(url, data) return parser_json(ret)
def request_userinfo(self): url = "http://musicmini.baidu.com/api/index.php" data = { "ver": 1, "type": 2, "format": "json", "clientver": self.client_version, "from": "baidumusic", "bduss": self.bduss } ret = public_curl.request(url, data) return parser_json(ret)
def request(self, title, artist): quote_title = urllib.quote(title) quote_artist = urllib.quote(artist) raw_data = public_curl.get(self.TTPOD_SEARCH_URL % (quote_title, quote_artist)) json_data = parser_json(raw_data) lrc_infos = [] lrc_data = json_data.get('data', {}).get('lrc', None) if lrc_data: temp_url = self.save_to_temp(lrc_data) lrc_infos.append((artist, title, temp_url)) return lrc_infos
def upload(self, filepath, upload_to="/"): params = dict(method='upload', type='tmpfile', app_id=250528, BDUSS=self._bduss()) files = [("file", (pycurl.FORM_FILE, filepath)),] resp = self.curl.request("http://c.pcs.baidu.com/rest/2.0/pcs/file?" + \ urllib.urlencode(params), method="UPLOAD", data=files) ret = parser_json(resp) size = os.path.getsize(filepath) return self._create(os.path.join(upload_to, os.path.basename(filepath)), [ret['md5']], size)
def request_songinfo(song): url = "http://musicmini.baidu.com/app/link/getLinks.php" data = dict(songId=song['sid'], songArtist=song['artist'], songTitle=song['title'], linkType=9, isLogin=0, clientVer="8.0.0.5", isCloud=0 ) ret = public_curl.request(url, data) pret = parser_json(ret) if len(pret) > 0: return parse_to_dsong(pret[0], song) return None
def request_songinfo(self, song): url = "http://musicmini.baidu.com/app/link/getLinks.php" data = dict(songId=song['sid'], songArtist=song['artist'], songTitle=song['title'], linkType=self.link_type, isLogin=self.is_login, clientVer=self.client_version, isCloud=self.is_cloud, isHq=self.is_hq_enabled ) ret = public_curl.request(url, data) pret = parser_json(ret) if len(pret) > 0: return parse_to_dsong(pret[0], song) return None
def request_songinfo(self, song): url = "http://musicmini.baidu.com/app/link/getLinks.php" data = dict(songId=song['sid'], songArtist=song['artist'], songTitle=song['title'], linkType=self.link_type, isLogin=self.is_login, clientVer=self.client_version, isCloud=self.is_cloud, isHq=self.is_hq_enabled) params = {'param': base64.b64encode(json.dumps(data))} ret = public_curl.request(url, params, method="POST") pret = parser_json(ret) if len(pret) > 0: return parse_to_dsong(pret[0], song) return None
class PosterLib(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.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: 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
def request_songinfo(self, song): url = "http://musicmini.baidu.com/app/link/getLinks.php" data = dict(songsiId=song['sid'], songArtist=song['artist'], songTitle=song['title'], songAppend="", linkType=self.link_type, isHq=self.is_hq_enabled, isCloud=self.is_cloud, hasMV=0, noFlac=0, rate=0 ) params = {'param' : base64.b64encode(json.dumps(data))} header = {"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"} ret = public_curl.request(url, params, method="POST", header=header) pret = parser_json(ret) if len(pret) > 0: return parse_to_dsong(pret[0], song) return None
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 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 load_channels(self): ret = self.mycurl.get('http://www.douban.com/j/app/radio/channels') return utils.parser_json(ret)
except Exception, 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(api, method, extra_data, retry_limit, **params) if encoding is None: raw = ret.read() else: try: raw = ret.read().decode(encoding) except: raw = ret.read() data = utils.parser_json(raw) self.logdebug("API response %s: %s TT=%.3fs", api, data, time.time() - start ) return data def test(self): # ret_data = self.api_request("/i/sys/user_json?v=%s" % utils.timestamp()) # http://tieba.baidu.com/manager-apply/data/frs?dtype=json&ie=gbk&kw=bpython&fid=2196998&is_mgr=0&mgr_num=0&t=17b3ndkt9 # ret_data = self.api_request("/i/commit", "POST", extra_data={"cmd" : "follow_all", "tbs": self.get_common_tbs()}) pass if __name__ == "__main__": tieba_lib = TiebaLib(sys.argv[1], sys.argv[2]) if tieba_lib.check_login(): print tieba_lib.get_user_portrait() # tieba_lib.new_tie("bpython", "【bpython】我就不说什么了", "测试表情哈!", smiley=("tsj", "t_0013"))
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
def get_user_info(self): ret = self.mycurl.get("https://api.douban.com/v2/user/~me") return utils.parser_json(ret)
def get_mv(self, song_id): url = "http://musicmini.baidu.com/app/mv/getMV.php" data = dict(songid=song_id) ret = public_curl.request(url, data) return parser_json(ret)
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(api, method, extra_data, retry_limit, **params) if encoding is None: raw = ret.read() else: try: raw = ret.read().decode(encoding) except: raw = ret.read() data = utils.parser_json(raw) self.logdebug("API response %s: %s TT=%.3fs", api, data, time.time() - start) return data def test(self): # ret_data = self.api_request("/i/sys/user_json?v=%s" % utils.timestamp()) # http://tieba.baidu.com/manager-apply/data/frs?dtype=json&ie=gbk&kw=bpython&fid=2196998&is_mgr=0&mgr_num=0&t=17b3ndkt9 # ret_data = self.api_request("/i/commit", "POST", extra_data={"cmd" : "follow_all", "tbs": self.get_common_tbs()}) pass if __name__ == "__main__": tieba_lib = TiebaLib(sys.argv[1], sys.argv[2]) if tieba_lib.check_login(): print tieba_lib.get_user_portrait()