1
- import aiohttp
2
1
import json
3
- from aiosocks .connector import ProxyConnector , ProxyClientRequest
2
+
3
+ import aiohttp
4
+ from aiosocks .connector import ProxyClientRequest , ProxyConnector
4
5
5
6
6
7
async def get (url , ** kwargs ):
7
- return await request (' get' , url , ** kwargs )
8
+ return await request (" get" , url , ** kwargs )
8
9
9
10
10
11
async def post (url , data , ** kwargs ):
11
12
if data is dict or data is str :
12
- kwargs [' json' ] = data
13
+ kwargs [" json" ] = data
13
14
else :
14
- kwargs [' data' ] = data
15
+ kwargs [" data" ] = data
15
16
16
- return await request (' post' , url , ** kwargs )
17
+ return await request (" post" , url , ** kwargs )
17
18
18
19
19
20
async def request (method , url , ** kwargs ):
20
21
session_kwargs = {}
21
- if ' proxy' in kwargs and kwargs [' proxy' ].startswith (' socks' ):
22
- session_kwargs [' connector' ] = ProxyConnector (remote_resolve = False )
23
- session_kwargs [' request_class' ] = ProxyClientRequest
22
+ if " proxy" in kwargs and kwargs [" proxy" ].startswith (" socks" ):
23
+ session_kwargs [" connector" ] = ProxyConnector (remote_resolve = False )
24
+ session_kwargs [" request_class" ] = ProxyClientRequest
24
25
25
- if ' cookies' in kwargs :
26
- session_kwargs [' cookies' ] = kwargs [' cookies' ]
27
- del kwargs [' cookies' ]
26
+ if " cookies" in kwargs :
27
+ session_kwargs [" cookies" ] = kwargs [" cookies" ]
28
+ del kwargs [" cookies" ]
28
29
29
- if ' timeout' not in kwargs :
30
- kwargs [' timeout' ] = 10
30
+ if " timeout" not in kwargs :
31
+ kwargs [" timeout" ] = 10
31
32
32
33
# headers={'User-Agent': get_random_user_agent()}
33
- if 'headers' not in kwargs :
34
- kwargs ['headers' ] = {
35
- 'User-Agent' : get_random_user_agent ()
36
- }
37
- elif 'User-Agent' not in kwargs ['headers' ]:
38
- kwargs ['headers' ]['User-Agent' ] = get_random_user_agent ()
39
-
40
- if 'override_session' in kwargs :
41
- session = kwargs ['override_session' ]
42
- del kwargs ['override_session' ]
34
+ if "headers" not in kwargs :
35
+ kwargs ["headers" ] = {"User-Agent" : get_random_user_agent ()}
36
+ elif "User-Agent" not in kwargs ["headers" ]:
37
+ kwargs ["headers" ]["User-Agent" ] = get_random_user_agent ()
38
+
39
+ if "override_session" in kwargs :
40
+ session = kwargs ["override_session" ]
41
+ del kwargs ["override_session" ]
43
42
async with session .request (method , url , ** kwargs ) as response :
44
43
return await Response .from_aiohttp_response (response )
45
44
@@ -57,22 +56,24 @@ def __init__(self, status, text, aiohttp_response=None):
57
56
@staticmethod
58
57
async def from_aiohttp_response (aiohttp_response ):
59
58
return Response (
60
- status = aiohttp_response .status ,
61
- text = await aiohttp_response .text (),
62
- aiohttp_response = aiohttp_response
59
+ status = aiohttp_response .status ,
60
+ text = await aiohttp_response .text (),
61
+ aiohttp_response = aiohttp_response ,
63
62
)
64
63
65
64
def __str__ (self ):
66
- return json .dumps ({
67
- 'status' : self .status ,
68
- 'text' : self .text ,
69
- })
65
+ return json .dumps (
66
+ {
67
+ "status" : self .status ,
68
+ "text" : self .text ,
69
+ }
70
+ )
70
71
71
72
__repr__ = __str__
72
73
73
74
74
75
def get_random_user_agent ():
75
- return ' Mozilla/5.0 (Windows NT;) Gecko/20100101 Firefox/58.0'
76
+ return " Mozilla/5.0 (Windows NT;) Gecko/20100101 Firefox/58.0"
76
77
# return 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0'
77
78
# TODO: do it
78
79
# return UserAgent().random
0 commit comments