pyctx.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : pyctx.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/24
  6. import json
  7. import requests
  8. from utils.web import *
  9. from utils.log import logger
  10. from utils.encode import base64Encode, base64Decode, fetch, post, request, getCryptoJS, getPreJs, buildUrl, getHome, \
  11. parseText, atob,btoa
  12. from utils.encode import setDetail, join, urljoin2, parseText, requireCache, base64ToImage, encodeStr, decodeStr
  13. from utils.encode import md5 as mmd5
  14. from utils.parser import runPy, runJScode, JsObjectWrapper, PyJsObject, PyJsString
  15. from urllib.parse import quote
  16. from utils.htmlParser import jsoup
  17. from controllers.service import storage_service
  18. def setItem(key, value):
  19. lsg = storage_service()
  20. if isinstance(key, PyJsString):
  21. key = parseText(str(key))
  22. if isinstance(value, PyJsString):
  23. value = parseText(str(value))
  24. return lsg.setItem(key, value)
  25. def getItem(key, value=''):
  26. lsg = storage_service()
  27. if isinstance(key, PyJsString):
  28. key = parseText(str(key))
  29. if isinstance(value, PyJsString):
  30. value = parseText(str(value))
  31. return lsg.getItem(key, value)
  32. def clearItem(key):
  33. lsg = storage_service()
  34. if isinstance(key, PyJsString):
  35. key = parseText(str(key))
  36. return lsg.clearItem(key)
  37. def encodeUrl(url):
  38. # return base64Encode(quote(url))
  39. # return base64Encode(url)
  40. # print(type(url))
  41. if isinstance(url, PyJsString):
  42. # obj = obj.to_dict()
  43. url = parseText(str(url))
  44. return quote(url)
  45. def stringify(obj):
  46. if isinstance(obj, PyJsObject):
  47. # obj = obj.to_dict()
  48. obj = parseText(str(obj))
  49. return json.dumps(obj, separators=(',', ':'), ensure_ascii=False)
  50. def requireObj(url):
  51. if isinstance(url, PyJsString):
  52. url = parseText(str(url))
  53. return requireCache(url)
  54. def md5(text):
  55. if isinstance(text, PyJsString):
  56. text = parseText(str(text))
  57. return mmd5(text)
  58. py_ctx = {
  59. 'requests': requests, 'print': print, 'base64Encode': base64Encode, 'base64Decode': base64Decode,
  60. 'log': logger.info, 'fetch': fetch, 'post': post, 'request': request, 'getCryptoJS': getCryptoJS,
  61. 'buildUrl': buildUrl, 'getHome': getHome, 'setDetail': setDetail, 'join': join, 'urljoin2': urljoin2,
  62. 'PC_UA': PC_UA, 'MOBILE_UA': MOBILE_UA, 'UC_UA': UC_UA, 'IOS_UA': IOS_UA,
  63. 'setItem': setItem, 'getItem': getItem, 'clearItem': clearItem, 'stringify': stringify, 'encodeUrl': encodeUrl,
  64. 'requireObj': requireObj, 'md5': md5, 'atob': atob, 'btoa':btoa,'base64ToImage': base64ToImage, 'encodeStr': encodeStr,
  65. 'decodeStr': decodeStr
  66. }