Skip to content

Commit

Permalink
Minor fix, format, typo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Aug 25, 2021
1 parent 98d374b commit 2db3a5a
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 68 deletions.
18 changes: 12 additions & 6 deletions Perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion pixivpy_async/aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pixivpy_async/bapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions pixivpy_async/bypass_sni.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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

14 changes: 7 additions & 7 deletions pixivpy_async/client.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand All @@ -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:
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pixivpy_async/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -61,4 +62,4 @@ def __init__(self):
self.reason = 'No access_token Found!'

def __str__(self):
return self.reason
return self.reason
2 changes: 1 addition & 1 deletion pixivpy_async/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 22 additions & 11 deletions pixivpy_async/papi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pixivpy_async/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pixivpy_async/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def syncify(*types):

__all__ = [
"PixivAPI", "AppPixivAPI", "error", "PixivClient"
]
]
3 changes: 2 additions & 1 deletion pixivpy_async/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
)
7 changes: 4 additions & 3 deletions socks5_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Modified From https://github.com/rushter/socks5
# Modified From https://github.com/rushter/socks5

import logging
import select
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
server.serve_forever()
Loading

0 comments on commit 2db3a5a

Please sign in to comment.