common.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # encoding: utf-8
  2. """
  3. @author: luojunhui
  4. """
  5. import aiohttp
  6. import asyncio
  7. def clean_title(strings):
  8. """
  9. :param strings:
  10. :return:
  11. """
  12. return (
  13. strings.strip()
  14. .replace("\n", "")
  15. .replace("/", "")
  16. .replace("\r", "")
  17. .replace("#", "")
  18. .replace(".", "。")
  19. .replace("\\", "")
  20. .replace("&NBSP", "")
  21. .replace(":", "")
  22. .replace("*", "")
  23. .replace("?", "")
  24. .replace("?", "")
  25. .replace('"', "")
  26. .replace("<", "")
  27. .replace(">", "")
  28. .replace("|", "")
  29. .replace(" ", "")
  30. .replace('"', "")
  31. .replace("'", "")
  32. )
  33. def sensitive_flag(s_words, ori_title):
  34. """
  35. :param s_words:
  36. :param ori_title:
  37. :return:
  38. """
  39. # for word in s_words:
  40. # if str(word) in ori_title:
  41. # return False
  42. return True
  43. async def request_etl(url, headers, json_data, retries=6):
  44. """
  45. :param url:
  46. :param headers:
  47. :param json_data:
  48. :param retries:
  49. :return:
  50. """
  51. async with aiohttp.ClientSession() as session:
  52. for attempt in range(retries):
  53. try:
  54. async with session.post(url, headers=headers, json=json_data, timeout=120) as response:
  55. return await response.json()
  56. except asyncio.TimeoutError:
  57. if attempt < retries - 1:
  58. await asyncio.sleep(2) # 等待一段时间后重试
  59. else:
  60. raise
  61. async def async_post(url, headers, payload):
  62. """
  63. :param url:
  64. :param headers:
  65. :param payload:
  66. :return:
  67. """
  68. retries = 3
  69. async with aiohttp.ClientSession() as session:
  70. for attempt in range(3):
  71. try:
  72. async with session.post(url, headers=headers, data=payload, timeout=60) as response:
  73. return await response.json()
  74. except asyncio.TimeoutError:
  75. if attempt < retries - 1:
  76. await asyncio.sleep(2) # 等待一段时间后重试
  77. else:
  78. raise