Advertisement
Alex-Flexer

Untitled

Apr 14th, 2023
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. from bs4 import BeautifulSoup as bs
  2. from requests import get, post
  3. from time import sleep
  4. import spacy_udpipe
  5.  
  6.  
  7. def get_syntaxis_sentence(text: str) -> dict[str: str]:
  8. nlp = spacy_udpipe.load("ru")
  9. doc = nlp(text)
  10.  
  11. string = 'Подлежащее- {sub}\nДополнение- {obj}\nСказуемое- {root}'
  12.  
  13. sub = ''
  14. obj = ''
  15. root = ''
  16.  
  17. for token in doc:
  18. if 'subj' in token.dep_:
  19. sub += token.text
  20. elif 'obj' in token.dep_:
  21. obj += token.text
  22. elif 'ROOT' in token.dep_:
  23. root += token.text
  24.  
  25. return string.format(sub=sub, obj=obj, root=root)
  26.  
  27.  
  28. html = get('https://career.habr.com/vacancies/programmist_python').text
  29. s = bs(html, 'html.parser')
  30.  
  31. all_vacancy = s.find_all('div', class_ = 'vacancy-card__info')
  32.  
  33.  
  34. vacancys = []
  35. for vacancy in all_vacancy:
  36. title = vacancy.find('div', class_='vacancy-card__title').text
  37. title_link = 'https://career.habr.com/' + vacancy.find('a', class_='vacancy-card__title-link')['href']
  38. company = vacancy.find('div', class_='vacancy-card__company').text
  39. meta = vacancy.find('div', class_='vacancy-card__meta').text
  40. salary = vacancy.find('div', class_='vacancy-card__salary').text
  41. if salary == '':
  42. salary = 'будешь работать за еду(('
  43. skils = vacancy.find('div', class_='vacancy-card__skills').text
  44. vacancys.append(title + '\n' + company + '\n' + meta + '\n' + salary + '\n' + skils + '\n' + title_link)
  45.  
  46.  
  47. MY_TOKEN = "6167771133:AAGmG7BBdLBwCZSv4E5bfoyMC22jZBDJNSw"
  48. API_URL_TEMPLATE = "https://api.telegram.org/bot{token}/{method}"
  49.  
  50. previous_message_amount = len(get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()['result'])
  51.  
  52. url = API_URL_TEMPLATE.format(token=MY_TOKEN, method="setMyCommands")
  53. commands = [{'command': "/vacancys", 'description': 'check all vacancys'},
  54. {'command': "/morf", 'description': 'get verb sub and obj'},]
  55. post(url, json={'commands': commands})
  56.  
  57. def send_message(chat_id: int, text: str) -> None:
  58. post(API_URL_TEMPLATE.format(token=MY_TOKEN, method='sendMessage'),
  59. data={'chat_id': chat_id, 'text': text})
  60.  
  61.  
  62. def get_all_users_id_of_new_messages() -> list:
  63. global previous_message_amount
  64.  
  65. updates = get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()
  66. last_message_amount = len(updates['result'])
  67. amount_new_messages = last_message_amount - previous_message_amount
  68. previous_message_amount = last_message_amount
  69.  
  70. if amount_new_messages == 0:
  71. return []
  72.  
  73. return [(message['message']['from']['id'], message['message']['text']) for message in updates['result'][-amount_new_messages:]]
  74.  
  75.  
  76. def start_bot() -> None:
  77.  
  78. while True:
  79. sleep(1)
  80.  
  81. users_id = get_all_users_id_of_new_messages()
  82.  
  83. for user_id, user_text in users_id:
  84. print(user_id, user_text)
  85. if '/vacancys' in user_text:
  86. for vacancy_ in vacancys:
  87. send_message(user_id, vacancy_)
  88. elif '/morf' in user_text:
  89. text = user_text.replace('/morf', '').strip()
  90. syntaxis = get_syntaxis_sentence(text)
  91. send_message(user_id, syntaxis)
  92.  
  93. start_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement