Skip to content

Commit

Permalink
cleanup kwargs, add test for bypass sni
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Aug 25, 2021
1 parent c66f723 commit 98d374b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 60 deletions.
4 changes: 2 additions & 2 deletions pixivpy_async/aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


class AppPixivAPI(BasePixivAPI):
def __init__(self, bypass=False, **requests_kwargs):
def __init__(self, **requests_kwargs):
"""
initialize requests kwargs if need be
"""
super(AppPixivAPI, self).__init__(bypass=bypass, **requests_kwargs)
super(AppPixivAPI, self).__init__(**requests_kwargs)

# 用户详情
async def user_detail(
Expand Down
4 changes: 2 additions & 2 deletions pixivpy_async/bapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class BasePixivAPI(Net, Utils):
def __init__(self, bypass, **requests_kwargs):
def __init__(self, **requests_kwargs):
self.additional_headers = {}
self.client_id = 'MOBrBDS8blbauoSck0ZfDbtuzpyT'
self.client_secret = 'lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj'
Expand All @@ -32,7 +32,7 @@ def __init__(self, bypass, **requests_kwargs):
'image/png': '.png',
'image/gif': '.gif',
}
super().__init__(bypass=bypass, **requests_kwargs)
super().__init__(**requests_kwargs)

async def requests_(
self,
Expand Down
37 changes: 3 additions & 34 deletions pixivpy_async/bypass_sni.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ 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),
Expand All @@ -76,37 +78,4 @@ async def require_appapi_hosts(self, hostname, timeout=3) -> List[str]:
for r in results:
if not isinstance(r, Exception):
return r


RESOLVER = ByPassResolver()


def get_bypass_client() -> aiohttp.ClientSession:
ssl_ctx = ssl.SSLContext()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
connector = aiohttp.TCPConnector(ssl=ssl_ctx, resolver=RESOLVER)
client = aiohttp.ClientSession(connector=connector)
return client


class BypassClient:
def __init__(self):
self.client = get_bypass_client()

async def __aenter__(self) -> aiohttp.ClientSession:
return self.client

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.client.close()


async def test():
async with BypassClient() as client:
async with client.get(
"https://www.pixiv.net/ajax/search/artworks/%E7%99%BE%E5%90%88?word=%E7%99%BE%E5%90%88&order=date_d&mode=all&p=99999990&s_mode=s_tag&type=all&lang=zh") as rsp:
print(await rsp.json())


if __name__ == '__main__':
asyncio.run(test())

26 changes: 17 additions & 9 deletions pixivpy_async/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import asyncio
import aiohttp

from .bypass_sni import get_bypass_client

class PixivClient:
def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, bypass=False):
"""
Expand All @@ -19,30 +17,40 @@ def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None,
"""

kwargs = {'limit_per_host': limit}

if bypass:
import ssl
from .bypass_sni import ByPassResolver

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:
try:
from aiohttp_socks import ProxyConnector
self.conn = ProxyConnector.from_url(proxy, limit_per_host=limit)
self.conn = ProxyConnector.from_url(proxy, **kwargs)
_flag = False
except ModuleNotFoundError as e:
if proxy.startswith('socks'):
raise e
else:
self.conn = aiohttp.TCPConnector(limit_per_host=limit)
self.conn = aiohttp.TCPConnector(**kwargs)
_flag = True
else:
self.conn = aiohttp.TCPConnector(limit_per_host=limit)
self.conn = aiohttp.TCPConnector(**kwargs)

self.internal = internal

if bypass:
self.client = get_bypass_client()
else:
self.client = aiohttp.ClientSession(
self.client = aiohttp.ClientSession(
connector=self.conn,
timeout=aiohttp.ClientTimeout(total=timeout),
trust_env=env,
)


if proxy and _flag:
from functools import partial
Expand Down
18 changes: 8 additions & 10 deletions pixivpy_async/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@


class ClientManager:
def __init__(self, s, env, proxy, bypass):
def __init__(self, s, **opt):
if s is None:
self.session = PixivClient(internal=True, env=env, proxy=proxy, bypass=bypass)
self.session = PixivClient(internal=True, **opt)
else:
self.session = s

Expand All @@ -30,14 +30,12 @@ async def __aexit__(self, exception_type, exception_value, traceback):
class Net(object):
def __init__(self, client=None, env=False, proxy=None, bypass=False, **requests_kwargs):
self.session = client
self.env = env
self.proxy = proxy
self.bypass = bypass
self.conn_opt = {'env': env, 'proxy': proxy, 'bypass': bypass}
self.requests_kwargs = requests_kwargs

@retry(*retryable_error, retries=10, cooldown=random.randint(1, 3))
async def down(self, _url, _referer):
async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session:
async with ClientManager(self.session, **self.conn_opt) as session:
async with session.get(_url, headers={'Referer': _referer}, **self.requests_kwargs) as res:
c = await res.read()
t = res.content_type
Expand All @@ -47,7 +45,7 @@ async def down(self, _url, _referer):

@retry(*retryable_error, retries=10, cooldown=random.randint(1, 3))
async def auth(self, _url, _headers, _data):
async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session:
async with ClientManager(self.session, **self.conn_opt) as session:
async with session.post(_url, headers=_headers, data=_data, **self.requests_kwargs) as res:
r, b, q = await res.json(), res.status in [200, 301, 302], res.status

Expand All @@ -56,7 +54,7 @@ async def auth(self, _url, _headers, _data):

@retry(*retryable_error, retries=10, cooldown=random.randint(1, 3))
async def fetch(self, _url, _headers, _params):
async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session:
async with ClientManager(self.session, **self.conn_opt) as session:
async with session.get(_url, headers=_headers, params=_params, **self.requests_kwargs) as res:
q = await res.json()

Expand All @@ -65,7 +63,7 @@ async def fetch(self, _url, _headers, _params):

@retry(*retryable_error, retries=10, cooldown=random.randint(1, 3))
async def post(self, _url, _data, _headers, _params):
async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session:
async with ClientManager(self.session, **self.conn_opt) as session:
async with session.post(_url, data=_data, headers=_headers, params=_params, **self.requests_kwargs) as res:
r = await res.json()

Expand All @@ -74,7 +72,7 @@ async def post(self, _url, _data, _headers, _params):

@retry(*retryable_error, retries=10, cooldown=random.randint(1, 3))
async def delete(self, _url, _headers, _params):
async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session:
async with ClientManager(self.session, **self.conn_opt) as session:
async with session.delete(_url, headers=_headers, params=_params, **self.requests_kwargs) as res:
q = await res.json()

Expand Down
4 changes: 2 additions & 2 deletions pixivpy_async/papi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


class PixivAPI(BasePixivAPI):
def __init__(self, bypass=False,**requests_kwargs):
def __init__(self, **requests_kwargs):
"""
initialize requests kwargs if need be
"""
super().__init__(bypass,**requests_kwargs)
super().__init__(**requests_kwargs)

async def works(
self,
Expand Down
1 change: 0 additions & 1 deletion socks5_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def handle(self):
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((address, port))
bind_address = remote.getsockname()
# logging.info('Connected to %s %s' % (address, port))
else:
self.server.close_request(self.request)

Expand Down
24 changes: 24 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ def test_login(self):
self.assertIsNotNone(credential.refresh_token)
# self.assertIsNotNone(newpapi.login(_USERNAME, _PASSWORD))

def test_bypass(self):
newaapi = AppPixivAPI(bypass=True)
credential = newaapi.login(refresh_token=_TOKEN)
self.assertIsNotNone(credential)
self.assertIsNotNone(credential.access_token)
self.assertIsNotNone(credential.refresh_token)

detail = newaapi.user_detail(275527)
self.assertIsNotNone(detail)
self.assertIsNotNone(detail.user)
self.assertEqual(detail.user.id, 275527)

def test_bypass_2(self):
newaapi = AppPixivAPI(bypass=True, proxy="socks5://127.0.0.1:9011")
credential = newaapi.login(refresh_token=_TOKEN)
self.assertIsNotNone(credential)
self.assertIsNotNone(credential.access_token)
self.assertIsNotNone(credential.refresh_token)

detail = newaapi.user_detail(275527)
self.assertIsNotNone(detail)
self.assertIsNotNone(detail.user)
self.assertEqual(detail.user.id, 275527)

def test_socks_1(self):
newaapi = AppPixivAPI(proxy="socks5://127.0.0.1:9011")
credential = newaapi.login(refresh_token=_TOKEN)
Expand Down

0 comments on commit 98d374b

Please sign in to comment.