-
Notifications
You must be signed in to change notification settings - Fork 1
/
jfbrequests.py
137 lines (102 loc) · 4.24 KB
/
jfbrequests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import asyncio
from cchardet import detect
from lxml import etree
import json
import aiohttp
reulst = []
loop = asyncio.get_event_loop()
class HighRequest():
def __init__(self, method, url, timeout=None, session=False, headers=None, cookies=None, unsafe=None,
mark='xqq', **kwargs):
self.method, self.session, self.url, self.mark, self.timeout = method, session, url, mark, timeout
callback = kwargs.pop('callback', None)
self.callback = callback
self.kwargs = kwargs
if not session:
self.sessiondict = (cookies, headers, aiohttp.CookieJar(unsafe=True) if unsafe else None)
async def main(url, method, timeout=None, headers=None, cookies=None, proxy=None, data=None, params=None):
async with aiohttp.ClientSession() as client:
try:
async with client.request(url=url, method=method, headers=headers, timeout=timeout, cookies=cookies,
proxy=proxy, data=data, params=params) as resp:
reulst.append(HighResponse(await resp.read(), resp))
except:
pass
def request(url, method, timeout=None, headers=None, cookies=None, proxy=None, data=None, params=None):
loop.run_until_complete(main(url, method, timeout, headers, cookies, proxy, data, params))
return reulst
def requests(urls, method, timeout=None, headers=None, cookies=None, proxy=None, data=None, params=None):
tasks = []
for url in urls:
tasks.append(asyncio.ensure_future(main(url, method, timeout, headers, cookies, proxy, data, params)))
loop.run_until_complete(asyncio.wait(tasks))
return reulst
def get(url, timeout=60, headers=None, cookies=None, proxy=None, data=None):
request(url, 'GET', timeout, headers, cookies, proxy, data=None, params=data)
return reulst[0]
def post(url, timeout=60, headers=None, cookies=None, proxy=None, data=None):
request(url, 'POST', timeout, headers, cookies, proxy, data=data, params=None)
return reulst[0]
def gets(urls, timeout=60, headers=None, cookies=None, proxy=None, data=None):
requests(urls, 'GET', timeout, headers, cookies, proxy, data=None, params=data)
return reulst
class HighResponse():
def __init__(self, content, clientResponse):
self.content = content
self.clientResponse = clientResponse
def raw(self):
return self.clientResponse
@property
def url(self):
return self.clientResponse.url
@property
def cookies(self):
return self.clientResponse.cookies
@property
def headers(self):
return self.clientResponse.headers
@property
def status(self):
return self.clientResponse.status
@property
def method(self):
return self.clientResponse.method
@property
def text(self, encoding=None):
encoding = encoding or detect(self.content)['encoding']
return self.content.decode(encoding=encoding)
def get_element_for_xpath(self, str):
return etree.HTML(self.content).xpath(str)[0]
def get_elements_for_xpath(self, str):
return etree.HTML(self.content).xpath(str)
@property
def json(self):
return json.loads(self.text)
def get_value_from_key(self, key):
json_results = []
return get_value_by_key(self.json, json_results, key)[0]
def get_values_from_key(self, key):
json_results = []
return get_value_by_key(self.json, json_results, key)
def __repr__(self):
return "<HighResponse [status {}]>".format(self.clientResponse.status)
__str__ = __repr__
def get_dict_from_str(param):
params = {}
for data in param.split('\n'):
params.update({data.split(':')[0].strip(): data.split(':')[1].strip()})
return params
def get_value_by_key(input_json, results, key):
key_value = ''
if isinstance(input_json, dict):
for json_result in input_json.values():
if key in input_json.keys():
key_value = input_json.get(key)
else:
get_value_by_key(json_result, results, key)
elif isinstance(input_json, list):
for json_array in input_json:
get_value_by_key(json_array, results, key)
if key_value != '':
results.append(key_value)
return results