def auth(self, username=None, password=None, refresh_token=None): url = 'https://oauth.secure.pixiv.net/auth/token' headers = { 'App-OS': 'ios', 'App-OS-Version': '10.3.1', 'App-Version': '6.8.3', 'User-Agent': 'PixivIOSApp/6.8.3 (iOS 10.3.1; iPhone8,1)', } data = { 'get_secure_url': 1, 'client_id': 'MOBrBDS8blbauoSck0ZfDbtuzpyT', 'client_secret': 'lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj', } if CommonUtils.is_not_empty(username) and CommonUtils.is_not_empty( password): data['grant_type'] = 'password' data['username'] = username data['password'] = password elif (refresh_token is not None) or (self.refresh_token is not None): data['grant_type'] = 'refresh_token' data['refresh_token'] = refresh_token or self.refresh_token else: raise PixivError( '[ERROR] auth() but no password or refresh_token is set.') r = requests.post(url, headers=headers, data=data, proxies=pixiv_config.PROXIES) if r.status_code not in [200, 301, 302]: if data['grant_type'] == 'password': raise PixivError( '[ERROR] auth() failed! check username and password.\nHTTP %s: %s' % (r.status_code, r.text), header=r.headers, body=r.text) else: raise PixivError( '[ERROR] auth() failed! check refresh_token.\nHTTP %s: %s' % (r.status_code, r.text), header=r.headers, body=r.text) token = None try: token = parse_json(r.text) self.access_token = token.response.access_token self.user_id = token.response.user.id self.refresh_token = token.response.refresh_token except Exception as e: print(e) raise PixivError('Get access_token error! Response: %s' % token, header=r.headers, body=r.text) print("ACCESS TOKEN " + self.access_token) print("ACCESS Refresh Token " + self.refresh_token) return token
def requests_call(method, url, **kwargs): try: if method.upper() == 'GET': return requests.get(url, **kwargs) elif method.upper() == 'POST': return requests.post(url, **kwargs) elif method.upper() == 'DELETE': return requests.delete(url, **kwargs) except Exception as e: raise PixivError('requests %s %s error: %s' % (method, url, e)) raise PixivError('Unknow method: %s' % method)
def login(self): r = self.session.get(PIXIV_LOGIN_KEY, headers=PIXIV_PAGE_HEADERS) if r.ok: post_key = get_post_key(r.content) if post_key: post_data = { 'pixiv_id': self.username, 'password': self.password, 'post_key': post_key, 'source': 'accounts' } response = self.session.post(PIXIV_LOGIN_URL, data=post_data, headers=PIXIV_PAGE_HEADERS) res_obj = parse_resp(response) # 返回json 抽风了,一下successed,一下success 这里都验证一下 if res_obj.body.has_key("successed") or res_obj.body.has_key( "success"): print("Login Success getCookies:" + str( requests.utils.dict_from_cookiejar(self.session.cookies))) return self.session else: raise PixivError('username or password wrong!.') else: print('get post_key error')
def search_nologin(word, page=1, search_type='illust', download_threshold=DOWNLOAD_THRESHOLD): if word: url = (PIXIV_SEARCH_URL % (word, search_type, int(page))) else: raise PixivError('search word can not be null') print(url) response = requests.get(url, timeout=10, headers=PIXIV_PAGE_HEADERS) html = None if response and response.ok: response.encoding = 'utf-8' html = response.content if not html: print("Get Page is None!URL:" + url) return [] search_result = PixivHtmlParser.parse_search_result(html) pop_result = PixivHtmlParser.parse_popular_introduction(html) if not pop_result: pop_result = [] if search_result: # 过滤数据不完整和收藏数不超过阈值的插画信息 search_result = filter( lambda data: (data.has_key("url") and data.has_key("title") and data.has_key( "mark_count") and int(data.mark_count) >= download_threshold), search_result) pop_result.extend(search_result) return pop_result
def search(self, word, page=1, search_type='illust', download_threshold=DOWNLOAD_THRESHOLD): if word: url = (PIXIV_SEARCH_URL % (word, search_type, int(page))) else: raise PixivError('search word can not be null') print(url) html = self.request_page(url) if not html: print("Get Page is None!URL:" + url) return [] search_result = PixivHtmlParser.parse_search_result(html) pop_result = PixivHtmlParser.parse_popular_introduction(html) if not pop_result: pop_result = [] if search_result: pop_result.extend(search_result) # 过滤数据不完整和收藏数不超过阈值的插画信息 if len(pop_result) > 0: pop_result = filter( lambda data: (data.has_key("url") and data.has_key( "title") and data.has_key("mark_count") and int( data.mark_count) >= download_threshold), pop_result) for result in pop_result: if not result.has_key('id'): result['id'] = CommonUtils.get_url_param( result['url'], "illust_id") return pop_result
def __init__(self, root, api, search_handler): Frame.__init__(self, root) if not search_handler or not api: raise PixivError( 'You must set authPixivApi and search_handler for the DownloadFrame' ) self.api = api self.search_handler = search_handler self.downloader = IllustrationDownloader(api) self.queue = PixivQueue(self.downloader, callback=self.download_callback) self.task_text = ScrolledText(self, height=20, width=30, bg='light gray') self.print_text = ScrolledText(self, height=20, width=40, bg='light gray') self.root = root self.frames = [ DownloadFrame(self, 'By Url or Id', self.queue, self.api, self.task_text), SearchFrame(self, 'By Search', self.queue, self.api, self.search_handler), RankingFrame(self, 'By Ranking', self.queue, self.api), RelatedFrame(self, 'By Related', self.queue, self.api) ] self.switch_menu = None self.init_ui()
def api_search(keyword, api, page=1, download_threshold=DOWNLOAD_THRESHOLD): illusts = [] if CommonUtils.is_empty(keyword): raise PixivError('[ERROR] keyword is empty') ids = set() count = 0 for data in api.search_popular_illust('百合').illusts: if download_threshold: if data.total_bookmarks >= download_threshold: if data.id not in ids: ids.add(data.id) illusts.append(data) elif data.id not in ids: ids.add(data.id) illusts.append(data) if page: while page > 0: for data in api.search_illust('百合', offset=count).illusts: count = count + 1 if download_threshold: if data.total_bookmarks >= download_threshold: if data.id not in ids: ids.add(data.id) illusts.append(data) elif data.id not in ids: ids.add(data.id) illusts.append(data) page = page - 1 return illusts
def __init__(self, username=None, password=None, cookies=None, use_proxy=USE_PROXY): self.session = requests.session() self.use_proxy = use_proxy if use_proxy and len(PROXIES) == 0: raise PixivError('Proxy config Error') if use_proxy: self.session.proxies = PROXIES if username and password: self.username = username self.password = password self.login() elif cookies: self.session.cookies = requests.utils.cookiejar_from_dict(cookies) else: raise PixivError( 'Please input username and password or input cookies!')
def __init__(self, username=None, password=None, cookies=None): self.session = requests.session() if username and password: self.username = username self.password = password self.login() elif cookies: self.session.cookies = requests.utils.cookiejar_from_dict(cookies) else: raise PixivError( 'Please input username and password or input cookies!')
def __init__(self, downloader, callback=None, thread_num=5): if not downloader: raise PixivError('You must set a downloader for the queue') self.downloader = downloader self.queue = Queue() self.workers = [] self.thread_num = thread_num for i in range(1, thread_num + 1): self.workers.append( Thread(target=self.worker_run, name='Worker' + str(i), args=(self.queue, callback)))
def __init__(self, username, password, access_token=None, refresh_token=None, use_proxy=pixiv_config.USE_PROXY): self.username = username self.password = password self.user_id = None self.refresh_token = refresh_token self.access_token = access_token self.use_proxy = use_proxy if use_proxy and len(pixiv_config.PROXIES) == 0: raise PixivError('Proxy config Error') if CommonUtils.is_empty(access_token): self.login(username, password, refresh_token)
def check_api(cls): if cls.__apiClient: return try: cls.__lock.acquire() if not cls.__apiClient: cls.__apiClient = AuthPixivApi.get_authApi_by_token() if cls.__apiClient is None: cls.__apiClient = AuthPixivApi(USERNAME, PASSWORD) if cls.__apiClient and not cls.__apiClient.check_login_success( ): raise PixivError( '[ERROR] auth() failed! Please check username and password' ) except Exception as e: raise e finally: cls.__lock.release()
def auth_requests_call(self, method, url, headers=None, use_proxy_method=True, **kwargs): if headers is None: headers = {} self.require_auth() headers['Referer'] = 'http://spapi.pixiv.net/' headers['User-Agent'] = 'PixivIOSApp/6.0.9' # 指定语言 # ja 日文 # zh 中文简体 # zh-tw 中文繁体 # en 或空或其他无法解析语言 英文 headers['Accept-Language'] = 'zh' headers['Authorization'] = 'Bearer %s' % self.access_token # 先判断全局是否使用代理,再判断方法是否使用代理 if self.use_proxy: if use_proxy_method: response = requests_call(method, url, proxies=pixiv_config.PROXIES, headers=headers, **kwargs) else: response = requests_call(method, url, headers=headers, **kwargs) else: response = requests_call(method, url, headers=headers, **kwargs) if response.status_code != 200: raise PixivError(response.content) response.encoding = 'utf-8' return response
def require_auth(self): if self.access_token is None: raise PixivError('Authentication required! Call login() first!')