-
Notifications
You must be signed in to change notification settings - Fork 2
/
modulex.py
462 lines (332 loc) · 11.4 KB
/
modulex.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# V2.0
## ## ####### ######## ## ## ## ######## ## ##
### ### ## ## ## ## ## ## ## ## ## ##
#### #### ## ## ## ## ## ## ## ## ## ##
## ### ## ## ## ## ## ## ## ## ###### ###
## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ##
## ## ####### ######## ####### ######## ######## ## ##
# ▀█████████▄ ▄██ ▄
# ███ ███ ███ ██▄
# ███ ███ ███▄▄▄███
# _ ▄███▄▄▄██▀ ▀▀▀▀▀▀███
# ▀▀███▀▀▀██▄ ▄██ ███
# ███ ██▄ ███ ███
# ███ ███ ███ ███
# ▄█████████▀ ▀█████▀
# ███╗ ██╗██╗██╗ ██╗██╗ ██╗██╗██╗
# ████╗ ██║██║██║ ██╔╝██║ ██║██║██║
# ██╔██╗ ██║██║█████╔╝ ███████║██║██║
# ██║╚██╗██║██║██╔═██╗ ██╔══██║██║██║
# ██║ ╚████║██║██║ ██╗██║ ██║██║███████╗
# ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚══════╝
# ███████╗██╗ ██╗ █████╗ ███╗ ███╗██╗
# ██╔════╝██║ ██║██╔══██╗████╗ ████║██║
# ███████╗██║ █╗ ██║███████║██╔████╔██║██║
# ╚════██║██║███╗██║██╔══██║██║╚██╔╝██║██║
# ███████║╚███╔███╔╝██║ ██║██║ ╚═╝ ██║██║
# ╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝
import os
import time
import random
import json
import sys
# SET DATABASE ------------------
def setload(path, seperator="\n"):
rset = set(fread(path).split(seperator))
rset.remove("") if "" in rset else ""
return rset
def setwrite(path, setDataType):
fwrite(path, "\n".join(setDataType) + "\n")
def setupdate(path, newset):
diff = newset - setload(path)
if diff:
fappend(path, "\n".join(diff))
# SET DATABASE ------------------
def dictdifference(A, B):
return dict(A.items() - B.items())
# FILES SYSTEM-------------------
def fread(path):
f = open(path, "r+", encoding="utf-8").read()
return f
def fwrite(fpath, content):
f = open(fpath, "w+", encoding="utf-8", errors="ignore")
f.write(content)
def fappend(fname, content, suffix="\n"):
f = open(fname, "a")
f.write(content + suffix)
def touch(fpath, data=""):
try:
os.makedirs(os.path.split(fpath)[0], exist_ok=True)
except: # noqa: E722
pass
if not os.path.exists(fpath):
fwrite(fpath, data)
print("Touched", fpath)
def fgetlastmod(path):
'''
Get Last modified time of a file
'''
return time.time() - os.path.getmtime(path)
def fincrement(cname, lock=None):
'''
uses a file as incrementer, slow, use in
rare cases where you would need persistance storage.
Uses lock to prevent I/O race condition.
lock is derived from threading module.
'''
if lock:
with lock.acquire() as l: # noqa: E741
c = int(fread(cname))
c += 1
fwrite(cname, str(c))
l.release()
else:
print("please use lock")
# JSON----------------------------
def jloads(string):
return json.loads(string) # dict
def jload(path):
return json.load(open(path)) # return dict
def jdumps(dictonary, indent=4):
return json.dumps(dictonary, indent=indent) # return string
def jdump(dictonary, path):
return json.dump(dictonary, open(path, "w+"), indent="\t") # write to disk
def jdumpline(dictonary, indent=None):
return json.dumps(dictonary, indent=indent)
def jdumplines(dictionary, path):
[fappend(path, jdumpline({k: dictionary[k]})) for k in dictionary]
def jloadlines(path):
jsonlines = open(path, "r").readlines()
jldict = {}
for w in jsonlines:
try:
jldict.update(jloads(w))
except Exception:
pass
return jldict
def list_files_timesorted(folder):
return [folder + x for x in os.listdir(folder)].sort(key=os.path.getmtime)
# TIMESTAMPERS---------------------
def datetime(filesafe=1):
from datetime import datetime
template = "%Y%m%dT%H%M%S" if filesafe else "%Y-%m-%dT%H:%M:%S"
return datetime.today().strftime(template)
def date():
return datetime().split("T")[0]
def now():
import time
return int(time.time())
# RANDOMIZERS ---------------------
def shuffle(L):
return [poprandom(L) for x in range(len(L))]
def randindex(L):
return random.randrange(len(L)) # get random index
def poprandom(L):
i = randindex(L)
L[i], L[-1] = L[-1], L[i] # swap with the last element
return L.pop() # pop last element O(1)
# GENERATORS___________________________________
asciirange = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def randomstring(length):
lenascii = len(asciirange)
r = [str(asciirange[random.randrange(lenascii)]) for x in range(length)]
return "".join(r)
def hash(string):
import hashlib
return hashlib.md5(string.encode("utf-8")).hexdigest()
# REQUIRE: THE DEPENDENCY MANAGER____________________
def require(modules_list: list[str]):
if type(modules_list) is list:
pass
else:
modules_list = [modules_list]
for m in modules_list:
try:
exec(f'import {m}')
except Exception as e:
print(e)
os.system(f"pip install {m}")
# SIMPLE-DB_________________________________
def hash_db(hashkey, *hashvalue, dirname="./LOCAL_DATABASE/"):
"""
DESC:
creates a folder , and stores individual hashes as files.
if: an index(hashkey) is given then check is file exists and open and return a dict{}
else: if second argument (hashvalue[]) is given then create a dict
"""
itempath = dirname + hashkey
if hashvalue: # write inputted value to memory
fwrite(itempath, jdumps(hashvalue[0]))
return jload(itempath)
# THREADING__________________________________
MAX_THREADS = 128
def apply_async(*args):
global POOL
from concurrent.futures import ThreadPoolExecutor
try:
result = POOL.submit(
*args,
)
return result
except Exception:
POOL = ThreadPoolExecutor(MAX_THREADS)
result = POOL.submit(
*args,
)
return result
# WEBFN______________________________________
def get_random_proxy():
import re
fname = "proxylist.set"
sourceurl = "https://free-proxy-list.net/"
cacheTime: "seconds" = 30
if os.path.exists(fname):
tdelta = time.time() - os.path.getmtime(fname)
if tdelta >= cacheTime:
pass
else:
print(f"LOG: using preexisting proxyDB: created {tdelta}s ago ")
return setload(fname).pop()
page = get_page(sourceurl)
iplist = re.findall(r"[\d]+\.[\d]+\.[\d]+\.[\d]+:[\d]+", page.text)
proxylist = {"http://" + x for x in iplist}
setwrite(fname, proxylist)
print("LOG: refreshed proxy list")
return proxylist.pop()
def make_session_pool(count=1):
return [requests.Session() for x in range(count)]
UserAgent = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"
}
def get_page(url, headers={}): # return a page req object and retrive text later
import requests
headers.update(UserAgent)
req = requests.get(url, headers=headers)
return req
def post_page(
url,
data,
headers={},
):
import requests
r = requests.post(url, json=data, headers=headers)
if not r:
r = requests.post(url, data=data, headers=headers)
return
def make_soup(markup):
from bs4 import BeautifulSoup as soup
try:
return soup(markup, "lxml")
except Exception as e:
return soup(markup, "html.parser")
def get_page_soup(url, headers={}):
return make_soup(get_page(url, headers=headers).text)
def make_selenium_driver(headless=False, strategy="eager", timeout=10):
from selenium import webdriver as wd
opts = wd.firefox.options.Options()
opts.page_load_strategy = strategy
if headless:
opts.headless = True
# opts.add_argument("--headless")
driver = wd.Firefox(options=opts)
driver.set_page_load_timeout(timeout)
driver.implicitly_wait(10)
return driver
def get_page_selenium(
driver,
url,
new_tab=0,
delay=2,
waitcondition=lambda: True,
waitcondition_polling=0.2,
waitcondition_retries=10,
):
try:
if new_tab:
driver.execute_script("window.open('{}', '_blank')".format(url))
driver.get(url)
while waitcondition() is False:
if retry >= waitcondition_retries:
break
time.sleep(waitcondition_polling)
return driver.page_source
except Exception as e:
print(repr(e))
def parse_header(*firefoxAllHeaders, file=""):
if firefoxAllHeaders:
rawheader = firefoxAllHeaders[0]
if file:
rawheader = jload(file)
serializedHeaders = list((rawheader).values())[0]["headers"]
# print(serializedHeaders)
return {k: v for k, v in [x.values() for x in serializedHeaders]}
def parse_raw_headers(fpath, log=0):
headers = {}
for x in open(fpath).read().split('\n'):
d = dict([[y.strip() for y in x.split(':', 1)]])
headers.update(d)
if log:
print(d)
return headers
def make_cookie(req):
return ";".join([f"{k}={v}" for k, v in req.cookies.items()])
def auto_encoder(d):
'''encode dict to url get params'''
string = "&".join([f"{k}={urllib.parse.quote_plus(str(v))}" for k, v in d.items()])
return string
def wlan_ip():
import subprocess
result = subprocess.run(
"ipconfig", stdout=subprocess.PIPE, text=True
).stdout.lower()
scan = 0
for i in result.split("\n"):
if "wireless" in i:
scan = 1
if scan:
if "ipv4" in i:
print(i.split(":")[1].strip())
# Benchmarking _________________
def timeit(fn, *args, times=1000, verbose=False):
import time
ts = time.time()
print(f"LOG: run {fn.__name__} X {times} Times") if verbose else None
for x in range(times):
fnoutput = fn(*args)
tdelta = time.time() - ts
print(f"LOG: Ttotal: {(tdelta)*1000}ms | time/call: {(tdelta/times)*1000}ms") if verbose else None
print(f"LOG: output == ", fnoutput) if verbose else None
return tdelta
class Tests:
def testWebServerStress():
def reqfn():
d = requests.get(url2)
print("fetch success", d.text)
# url1 = "http://swamix.com/"
url2 = "http://swamix.com/api/news/tech"
Parallelizer.tpoolexec(reqfn, threadCount=100) # noqa: F821
def i_want_to_release_this_version_on_github():
os.system(f"git commit -m \"force committed on \"")
os.system("git push -f")
# _ _
# (_) | |
# _ __ ___ __ _ _ _ __ ___ ___ __| | ___
# | '_ ` _ \ / _` | | '_ \ / __/ _ \ / _` |/ _ \
# | | | | | | (_| | | | | | | (_| (_) | (_| | __/
# |_| |_| |_|\__,_|_|_| |_| \___\___/ \__,_|\___|
# _________________________________________________
if __name__ == "__main__":
...
# mypage = get_page_soup('https://www.edarabia.com/courses/').select('.list-items')
# coursedata = dict()
# for category in mypage:
# try:
# coursename = category.find('h3').text
# subcourses = list(map(lambda x: x.text, category.select('ul>li')))
# coursedata.update({coursename : subcourses})
# except Exception as e:
# pass
# jdump(coursedata, 'course-and-subcourses.json')
# require(['numpy', 'pandas', 'bs4'])
# html body div#root div.a.b.c div.s article.meteredContent div section.de.df.dg.dh.di