diff --git a/Perf.py b/Perf.py index 4f6d461..519609e 100644 --- a/Perf.py +++ b/Perf.py @@ -93,7 +93,8 @@ async def _test_async_illust_detail(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(illust_detail(aapi, i)) for i in range(num)] + tasks = [asyncio.ensure_future(illust_detail(aapi, i)) + for i in range(num)] await asyncio.wait(tasks) @@ -131,7 +132,8 @@ async def _test_async_user_illusts(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(user_illusts(aapi, i)) for i in range(num)] + tasks = [asyncio.ensure_future(user_illusts(aapi, i)) + for i in range(num)] await asyncio.wait(tasks) @@ -169,7 +171,8 @@ async def _test_async_user_detail(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(user_detail(aapi, i)) for i in range(num)] + tasks = [asyncio.ensure_future(user_detail(aapi, i)) + for i in range(num)] await asyncio.wait(tasks) @@ -207,7 +210,8 @@ async def _test_async_ugoira_metadata(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(ugoira_metadata(aapi, i)) for i in range(num)] + tasks = [asyncio.ensure_future( + ugoira_metadata(aapi, i)) for i in range(num)] await asyncio.wait(tasks) @@ -283,7 +287,8 @@ async def _test_async_me_following_works(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(me_following_works(papi, i)) for i in range(num)] + tasks = [asyncio.ensure_future( + me_following_works(papi, i)) for i in range(num)] await asyncio.wait(tasks) @@ -359,7 +364,8 @@ async def _test_async_latest_works(num): await aapi.login(refresh_token=_TOKEN) # await aapi.login(_USERNAME, _PASSWORD) # await papi.login(_USERNAME, _PASSWORD) - tasks = [asyncio.ensure_future(latest_works(papi, i)) for i in range(num)] + tasks = [asyncio.ensure_future(latest_works(papi, i)) + for i in range(num)] await asyncio.wait(tasks) diff --git a/pixivpy_async/aapi.py b/pixivpy_async/aapi.py index 6a858c9..e47d565 100644 --- a/pixivpy_async/aapi.py +++ b/pixivpy_async/aapi.py @@ -305,7 +305,7 @@ async def illust_recommended( max_bookmark_id_for_recommend: int = None, min_bookmark_id_for_recent_illust: int = None, offset: int = None, - include_ranking_illusts = None, + include_ranking_illusts=None, bookmark_illust_ids: list = None, include_privacy_policy=None, req_auth: bool = True, diff --git a/pixivpy_async/bapi.py b/pixivpy_async/bapi.py index 6145203..96fb4da 100644 --- a/pixivpy_async/bapi.py +++ b/pixivpy_async/bapi.py @@ -147,7 +147,8 @@ async def download(self, url, prefix='', path=os.path.curdir, fname=None, auto_e if auto_ext and type in self.content_type_mapping: _ext = re.findall(r'(\.\w+)$', img_path) if _ext: - img_path = img_path.replace(_ext[0], self.content_type_mapping[type]) + img_path = img_path.replace( + _ext[0], self.content_type_mapping[type]) else: img_path += self.content_type_mapping[type] async with aiofiles.open(img_path, mode='wb') as out_file: diff --git a/pixivpy_async/bypass_sni.py b/pixivpy_async/bypass_sni.py index 8af0302..b94cfe5 100644 --- a/pixivpy_async/bypass_sni.py +++ b/pixivpy_async/bypass_sni.py @@ -39,8 +39,7 @@ async def fetch(self, client_session: aiohttp.ClientSession, url: str, params, t async with client_session.get(url, params=params, timeout=timeout) as rsp: response = await rsp.text() obj = json.loads(response) - pattern = re.compile( - "((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(1\d\d|2[0-4]\d|25[0-5]|[1-9]\d|\d)") + pattern = re.compile(r"((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(1\d\d|2[0-4]\d|25[0-5]|[1-9]\d|\d)") result = [] for i in obj["Answer"]: ip = i["data"] @@ -68,14 +67,12 @@ async def require_appapi_hosts(self, hostname, timeout=3) -> List[str]: "cd": "false", } - # print("resolve: %s" % hostname) - async with aiohttp.ClientSession() as session: results = await asyncio.gather( - *(asyncio.create_task(self.fetch(session, url, params, ClientTimeout(total=timeout))) for url in URLS), + *(asyncio.create_task(self.fetch(session, url, params, + ClientTimeout(total=timeout))) for url in URLS), return_exceptions=True) for r in results: if not isinstance(r, Exception): return r - \ No newline at end of file diff --git a/pixivpy_async/client.py b/pixivpy_async/client.py index eb6d3fb..f71f9b8 100644 --- a/pixivpy_async/client.py +++ b/pixivpy_async/client.py @@ -1,6 +1,7 @@ import asyncio import aiohttp + class PixivClient: def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, bypass=False): """ @@ -26,7 +27,7 @@ def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, ssl_ctx = ssl.SSLContext() ssl_ctx.check_hostname = False ssl_ctx.verify_mode = ssl.CERT_NONE - + kwargs.update({'ssl': ssl_ctx, 'resolver': ByPassResolver()}) if proxy: @@ -44,13 +45,12 @@ def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, self.conn = aiohttp.TCPConnector(**kwargs) self.internal = internal - + self.client = aiohttp.ClientSession( - connector=self.conn, - timeout=aiohttp.ClientTimeout(total=timeout), - trust_env=env, - ) - + connector=self.conn, + timeout=aiohttp.ClientTimeout(total=timeout), + trust_env=env, + ) if proxy and _flag: from functools import partial diff --git a/pixivpy_async/error.py b/pixivpy_async/error.py index 9bec621..21dc0bb 100644 --- a/pixivpy_async/error.py +++ b/pixivpy_async/error.py @@ -41,7 +41,8 @@ def __str__(self): class TokenError(PixivError): def __init__(self, token=None, e=None): - self.reason = 'Get access_token error! \nResponse: %s\nError: %s' % (token, e) + self.reason = 'Get access_token error! \nResponse: %s\nError: %s' % ( + token, e) super(PixivError, self).__init__(self, self.reason) def __str__(self): @@ -61,4 +62,4 @@ def __init__(self): self.reason = 'No access_token Found!' def __str__(self): - return self.reason \ No newline at end of file + return self.reason diff --git a/pixivpy_async/net.py b/pixivpy_async/net.py index ce06660..b6c05e5 100644 --- a/pixivpy_async/net.py +++ b/pixivpy_async/net.py @@ -5,7 +5,7 @@ from .client import PixivClient retryable_error = asyncio.TimeoutError, aiohttp.ClientError, aiohttp.ServerTimeoutError, \ - aiohttp.ServerConnectionError, aiohttp.ServerDisconnectedError + aiohttp.ServerConnectionError, aiohttp.ServerDisconnectedError class ClientManager: diff --git a/pixivpy_async/papi.py b/pixivpy_async/papi.py index 5f11b91..cc83487 100644 --- a/pixivpy_async/papi.py +++ b/pixivpy_async/papi.py @@ -67,7 +67,8 @@ async def me_favorite_works( page=page, per_page=per_page, publicity=publicity, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -107,7 +108,8 @@ async def me_following_works( params = self.set_params( page=page, per_page=per_page, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], include_stats=include_stats, include_sanity_level=include_sanity_level, ) @@ -166,7 +168,8 @@ async def users_works( per_page=per_page, include_stats=include_stats, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'] + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'] ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -183,7 +186,8 @@ async def users_favorite_works( page=page, per_page=per_page, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'] + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'] ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -235,8 +239,10 @@ async def ranking( per_page=per_page, include_stats=include_stats, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], - profile_image_sizes=profile_image_sizes if profile_image_sizes else ['px_170x170', 'px_50x50'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], + profile_image_sizes=profile_image_sizes if profile_image_sizes else [ + 'px_170x170', 'px_50x50'], ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -265,7 +271,8 @@ async def search_works( per_page=per_page, include_stats=include_stats, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], types=types if types else ['illustration', 'manga', 'ugoira'], ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -285,8 +292,10 @@ async def latest_works( per_page=per_page, include_stats=include_stats, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], - profile_image_sizes=profile_image_sizes if profile_image_sizes else ['px_170x170', 'px_50x50'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], + profile_image_sizes=profile_image_sizes if profile_image_sizes else [ + 'px_170x170', 'px_50x50'], ) return await self.requests_(method=method, url=url, params=params, auth=True) @@ -309,8 +318,10 @@ def ranking_all( date=date, include_stats=include_stats, include_sanity_level=include_sanity_level, - image_sizes=image_sizes if image_sizes else ['px_128x128', 'px_480mw', 'large'], - profile_image_sizes=profile_image_sizes if profile_image_sizes else ['px_170x170', 'px_50x50'], + image_sizes=image_sizes if image_sizes else [ + 'px_128x128', 'px_480mw', 'large'], + profile_image_sizes=profile_image_sizes if profile_image_sizes else [ + 'px_170x170', 'px_50x50'], ) async def bad_words(self): diff --git a/pixivpy_async/retry.py b/pixivpy_async/retry.py index 6af8c3d..f3505d4 100644 --- a/pixivpy_async/retry.py +++ b/pixivpy_async/retry.py @@ -34,7 +34,8 @@ async def inner(*args, **kwargs): if retries_count > retries: verbose and log.exception(message) - raise RetryExhaustedError(func.__qualname__, args, kwargs) from err + raise RetryExhaustedError( + func.__qualname__, args, kwargs) from err else: verbose and log.warning(message) diff --git a/pixivpy_async/sync.py b/pixivpy_async/sync.py index 19ec220..fb964b4 100644 --- a/pixivpy_async/sync.py +++ b/pixivpy_async/sync.py @@ -45,4 +45,4 @@ def syncify(*types): __all__ = [ "PixivAPI", "AppPixivAPI", "error", "PixivClient" -] \ No newline at end of file +] diff --git a/pixivpy_async/utils.py b/pixivpy_async/utils.py index 9017a95..b542b3c 100644 --- a/pixivpy_async/utils.py +++ b/pixivpy_async/utils.py @@ -41,7 +41,8 @@ def set_params(**kwargs): ) elif key in "ids": result.update( - {'%s[]' % key: ",".join([str(pid) for pid in value])} + {'%s[]' % key: ",".join( + [str(pid) for pid in value])} ) else: result.update( diff --git a/setup.py b/setup.py index 75fb47a..bc2f848 100644 --- a/setup.py +++ b/setup.py @@ -22,11 +22,14 @@ ], keywords=['pixiv', 'api', 'pixivpy', 'pixivpy_async'], packages=['pixivpy_async'], - install_requires=['aiohttp[speedups]', 'aiofiles'], + install_requires=['aiohttp', 'aiofiles'], extra_requires={ 'socks': [ 'aiohttp_socks', + ], + 'speedups': [ + 'aiohttp[speedups]' ] }, python_requires='>=3.5.3', -) \ No newline at end of file +) diff --git a/socks5_server.py b/socks5_server.py index 846d238..8c329d2 100644 --- a/socks5_server.py +++ b/socks5_server.py @@ -1,4 +1,4 @@ -# Modified From https://github.com/rushter/socks5 +# Modified From https://github.com/rushter/socks5 import logging import select @@ -35,7 +35,8 @@ def handle(self): self.connection.sendall(struct.pack("!BB", SOCKS_VERSION, 0)) # request - version, cmd, _, address_type = struct.unpack("!BBBB", self.connection.recv(4)) + version, cmd, _, address_type = struct.unpack( + "!BBBB", self.connection.recv(4)) assert version == SOCKS_VERSION if address_type == 1: # IPv4 @@ -96,4 +97,4 @@ def exchange_loop(self, client, remote): if __name__ == '__main__': with ThreadingTCPServer(('127.0.0.1', 9011), SocksProxy) as server: - server.serve_forever() \ No newline at end of file + server.serve_forever() diff --git a/test.py b/test.py index b19e7ab..ce0876e 100644 --- a/test.py +++ b/test.py @@ -17,12 +17,13 @@ _PASSWORD = "UserPay" _TOKEN = "0zeYA-PllRYp1tfrsq_w3vHGU1rPy237JMf5oDt73c4" -aapi = AppPixivAPI(bypass=True) -papi = PixivAPI(bypass=True) +aapi = AppPixivAPI() +papi = PixivAPI() papi.login(refresh_token=_TOKEN) aapi.login(refresh_token=_TOKEN) t = time.time() + class TestMethods(unittest.TestCase): def test_login(self): newaapi = AppPixivAPI(bypass=True) @@ -69,9 +70,9 @@ def test_socks_1(self): self.assertIsNotNone(detail) self.assertIsNotNone(detail.user) self.assertEqual(detail.user.id, 275527) - + def test_socks_2(self): - os.environ['ALL_PROXY']="socks5://127.0.0.1:9011" + os.environ['ALL_PROXY'] = "socks5://127.0.0.1:9011" newaapi = AppPixivAPI(env=True) credential = newaapi.login(refresh_token=_TOKEN) self.assertIsNotNone(credential) @@ -86,7 +87,7 @@ def test_socks_2(self): def test_illust_1(self): illust = aapi.illust_detail(59580629) self.assertIsNotNone(illust) - self.assertIsNotNone(illust,illust) + self.assertIsNotNone(illust, illust) self.assertEqual(illust.illust.id, 59580629) self.assertIsNotNone(illust.illust.image_urls) @@ -103,13 +104,15 @@ def test_illust_5(self): self.assertIsNotNone(aapi.illust_bookmark_detail(59580629)) def test_page(self): - self.assertIsNotNone(aapi.illust_recommended(bookmark_illust_ids=[59580629], req_auth=False)) - self.assertIsNotNone(aapi.illust_recommended(bookmark_illust_ids=[59580629])) + self.assertIsNotNone(aapi.illust_recommended( + bookmark_illust_ids=[59580629], req_auth=False)) + self.assertIsNotNone(aapi.illust_recommended( + bookmark_illust_ids=[59580629])) json_result = aapi.illust_recommended(bookmark_illust_ids=[59580629]) - self.assertIsNotNone(aapi.parse_qs(json_result.next_url)) # page down in some case + # page down in some case + self.assertIsNotNone(aapi.parse_qs(json_result.next_url)) next_qs = aapi.parse_qs(json_result.next_url) self.assertIsNotNone(aapi.illust_recommended(**next_qs)) - def test_user_1(self): self.assertIsNotNone(aapi.user_detail(275527)) @@ -119,7 +122,7 @@ def test_user_2(self): def test_user_3(self): self.assertIsNotNone(aapi.user_list(2088434)) - + def test_user_4(self): self.assertIsNotNone(aapi.user_related(7314824)) @@ -127,7 +130,6 @@ def test_user_5(self): self.assertIsNotNone(aapi.search_user('ほし')) self.assertIsNotNone(aapi.user_bookmark_tags_illust()) - def test_bookmark(self): self.assertIsNotNone(aapi.user_bookmarks_illust(2088434)) @@ -142,13 +144,13 @@ def test_follow_1(self): def test_follow_2(self): self.assertIsNotNone(aapi.user_follower(275527)) - + def test_follow_3(self): self.assertIsNotNone(aapi.user_mypixiv(275527)) def test_add(self): self.assertIsNot(aapi.user_follow_add(24687177), {}) - + def test_del(self): self.assertIsNot(aapi.user_follow_del(24687177), {}) @@ -166,7 +168,8 @@ def test_tag(self): for trend_tag in response.trend_tags[:10]: if not first_tag: first_tag = trend_tag.tag - self.assertIsNotNone(aapi.search_illust(first_tag, search_target='partial_match_for_tags')) + self.assertIsNotNone(aapi.search_illust( + first_tag, search_target='partial_match_for_tags')) def test_ranking(self): @@ -189,7 +192,8 @@ def test_download(self): # Disabled due to Rate Limit for illust in json_result.illusts[:5]: - image_url = illust.meta_single_page.get('original_image_url', illust.image_urls.large) + image_url = illust.meta_single_page.get( + 'original_image_url', illust.image_urls.large) url_basename = os.path.basename(image_url) extension = os.path.splitext(url_basename)[1] name = "illust_id_%d_%s%s" % (illust.id, illust.title, extension) @@ -198,28 +202,29 @@ def test_download(self): self.assertTrue(os.path.exists(name)) def test_papi_base(self): - + self.assertIsNotNone(papi.works(46363414)) self.assertIsNotNone(papi.users(1184799)) - + def test_papi_me(self): - + self.assertIsNotNone(papi.me_feeds(show_r18=0)) self.assertIsNotNone(papi.me_favorite_works(publicity='private')) self.assertIsNotNone(papi.me_following_works()) self.assertIsNotNone(papi.me_following()) def test_papi_users(self): - + self.assertIsNotNone(papi.users_works(1184799)) self.assertIsNotNone(papi.users_favorite_works(1184799)) self.assertIsNotNone(papi.users_feeds(1184799, show_r18=0)) self.assertIsNotNone(papi.users_following(4102577)) def test_papi_others(self): - + self.assertIsNotNone(papi.ranking('illust', 'weekly', 1)) - self.assertIsNotNone(papi.ranking(ranking_type='all', mode='daily', page=1, date='2019-08-01')) + self.assertIsNotNone(papi.ranking( + ranking_type='all', mode='daily', page=1, date='2019-08-01')) self.assertIsNotNone(papi.search_works("しらたま", page=1, mode='text')) self.assertIsNotNone(papi.latest_works()) self.assertIsNotNone(papi.ranking_all(date='2019-07-01')) @@ -235,9 +240,11 @@ def test_others(self): newaapi = AppPixivAPI() self.assertRaises(error.NoLoginError, newaapi.login, None) newaapi2 = AppPixivAPI() - self.assertRaises(error.AuthTokenError, newaapi2.login, refresh_token='89urwei') + self.assertRaises(error.AuthTokenError, + newaapi2.login, refresh_token='89urwei') newaapi3 = AppPixivAPI() - self.assertRaises(error.AuthCredentialsError, newaapi3.login, username='89urwei', password='97324yuieh') + self.assertRaises(error.AuthCredentialsError, newaapi3.login, + username='89urwei', password='97324yuieh') self.assertIsNone(newaapi.set_auth('None', 'None')) self.assertIsNone(newaapi.set_client('None', 'None')) self.assertIsNone(newaapi.set_accept_language('ja-jp')) @@ -246,8 +253,10 @@ def test_others(self): def test_http_methods(self): loop = asyncio.get_event_loop() - self.assertIsNotNone(loop.run_until_complete(Net().post('https://httpbin.org/post', None, {'accept': 'application/json'}, None))) - self.assertIsNotNone(loop.run_until_complete(Net().delete('https://httpbin.org/delete', {'accept': 'application/json'}, None))) + self.assertIsNotNone(loop.run_until_complete(Net().post( + 'https://httpbin.org/post', None, {'accept': 'application/json'}, None))) + self.assertIsNotNone(loop.run_until_complete(Net().delete( + 'https://httpbin.org/delete', {'accept': 'application/json'}, None))) if __name__ == '__main__':