Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup as bs
- from requests import get, post
- from time import sleep
- import spacy_udpipe
- def get_syntaxis_sentence(text: str) -> dict[str: str]:
- nlp = spacy_udpipe.load("ru")
- doc = nlp(text)
- string = 'Подлежащее- {sub}\nДополнение- {obj}\nСказуемое- {root}'
- sub = ''
- obj = ''
- root = ''
- for token in doc:
- if 'subj' in token.dep_:
- sub += token.text
- elif 'obj' in token.dep_:
- obj += token.text
- elif 'ROOT' in token.dep_:
- root += token.text
- return string.format(sub=sub, obj=obj, root=root)
- html = get('https://career.habr.com/vacancies/programmist_python').text
- s = bs(html, 'html.parser')
- all_vacancy = s.find_all('div', class_ = 'vacancy-card__info')
- vacancys = []
- for vacancy in all_vacancy:
- title = vacancy.find('div', class_='vacancy-card__title').text
- title_link = 'https://career.habr.com/' + vacancy.find('a', class_='vacancy-card__title-link')['href']
- company = vacancy.find('div', class_='vacancy-card__company').text
- meta = vacancy.find('div', class_='vacancy-card__meta').text
- salary = vacancy.find('div', class_='vacancy-card__salary').text
- if salary == '':
- salary = 'будешь работать за еду(('
- skils = vacancy.find('div', class_='vacancy-card__skills').text
- vacancys.append(title + '\n' + company + '\n' + meta + '\n' + salary + '\n' + skils + '\n' + title_link)
- MY_TOKEN = "6167771133:AAGmG7BBdLBwCZSv4E5bfoyMC22jZBDJNSw"
- API_URL_TEMPLATE = "https://api.telegram.org/bot{token}/{method}"
- previous_message_amount = len(get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()['result'])
- url = API_URL_TEMPLATE.format(token=MY_TOKEN, method="setMyCommands")
- commands = [{'command': "/vacancys", 'description': 'check all vacancys'},
- {'command': "/morf", 'description': 'get verb sub and obj'},]
- post(url, json={'commands': commands})
- def send_message(chat_id: int, text: str) -> None:
- post(API_URL_TEMPLATE.format(token=MY_TOKEN, method='sendMessage'),
- data={'chat_id': chat_id, 'text': text})
- def get_all_users_id_of_new_messages() -> list:
- global previous_message_amount
- updates = get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()
- last_message_amount = len(updates['result'])
- amount_new_messages = last_message_amount - previous_message_amount
- previous_message_amount = last_message_amount
- if amount_new_messages == 0:
- return []
- return [(message['message']['from']['id'], message['message']['text']) for message in updates['result'][-amount_new_messages:]]
- def start_bot() -> None:
- while True:
- sleep(1)
- users_id = get_all_users_id_of_new_messages()
- for user_id, user_text in users_id:
- print(user_id, user_text)
- if '/vacancys' in user_text:
- for vacancy_ in vacancys:
- send_message(user_id, vacancy_)
- elif '/morf' in user_text:
- text = user_text.replace('/morf', '').strip()
- syntaxis = get_syntaxis_sentence(text)
- send_message(user_id, syntaxis)
- start_bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement