bot_utilities.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from random import choice as randChoise
  2. from urllib.parse import urlparse
  3. from regex_tests import *
  4. #Для парсера
  5. from bs4 import BeautifulSoup
  6. import requests
  7. import random
  8. #Для подсчета спецсимволов
  9. from collections import Counter
  10. #Для сохранения объектов
  11. import pickle
  12. def get_random_emotion() -> str:
  13. emotions = ['GunRun', 'GlitchCat', 'FallHalp', 'FallWinning', 'BrokeBack', 'BloodTrail', 'CaitlynS', 'CarlSmile']
  14. emotion = randChoise(emotions)
  15. return emotion
  16. def check_regex_rule(val, rule):
  17. import re
  18. regex = re.compile(rule, re.IGNORECASE)
  19. res = re.search(regex, val)
  20. return res is not None
  21. def is_valid_args(args) -> bool:
  22. if check_regex_rule(str(args).replace(" ",""), REGEX_IS_URL_TEST):
  23. return False
  24. if not check_regex_rule(str(args), REGEX_SPEC_SYMB_RULE_TEST):
  25. return False
  26. if get_char_cnt(args, set(SPEC_SYMBOLS)) > 7:
  27. return False
  28. if len(args) > 25 and not ' ' in args:
  29. return False
  30. for arg in args.split():
  31. if check_regex_rule(str(arg), REGEX_IS_URL_TEST):
  32. return False
  33. if not check_regex_rule(str(arg), REGEX_SPEC_SYMB_RULE_TEST):
  34. return False
  35. return True
  36. def get_rand_anek() -> str:
  37. # url = f'https://anekdotbar.ru/korotkie/page/{random.randrange(1,33)}/'
  38. #TODO Спарсить всё на диск и брать оттуда
  39. url = f'https://anekdotbar.ru/page/{random.randrange(1,756)}/'
  40. req = requests.get(url)
  41. soup = BeautifulSoup(req.text, "html.parser")
  42. aneksHTML = soup.find_all('div', class_ = 'tecst')
  43. fullAnek = ''
  44. while True:
  45. aneksList = random.choice(tuple(aneksHTML)).find_all(text = True, recursive=False)
  46. fullAnek = ' '.join(aneksList).strip()
  47. if len(fullAnek) < 250:
  48. break
  49. return fullAnek
  50. def get_today_holiday() -> str:
  51. url = f'https://kakoysegodnyaprazdnik.ru//'
  52. header = {
  53. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  54. }
  55. req = requests.get(url, headers=header)
  56. soup = BeautifulSoup(req.content.decode('utf-8','ignore'), "html.parser")
  57. holiHTML = random.choice(soup.find_all('span', itemprop='text')).find_all(text = True, recursive=False)[0]
  58. return str(holiHTML)
  59. def get_char_cnt(string, symbols) -> int:
  60. cnt = 0
  61. counter = Counter(string)
  62. for char in symbols:
  63. cnt += counter[char]
  64. return cnt
  65. def save_obj(obj, name):
  66. with open('obj/'+ name + '.pkl', 'wb') as f:
  67. pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
  68. def load_obj(name):
  69. try:
  70. with open('obj/' + name + '.pkl', 'rb') as f:
  71. return pickle.load(f)
  72. except (OSError, IOError) as e:
  73. return None
  74. def decl_of_num(n, titles):
  75. if n%10==1 and n%100!=11:
  76. return titles[0]
  77. elif n%10>=2 and n%10<=4 and (n%100<10 or n%100>=20):
  78. return titles[1]
  79. else:
  80. return titles[2]
  81. def replace_chars(s, chars_map):
  82. res = str()
  83. for char in s:
  84. res += chars_map[char]
  85. return res
  86. def get_val_by_max_val(dictionary, val):
  87. for k, v in dictionary.items():
  88. if val <= k:
  89. return v
  90. return None