Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import config
- import requests
- import json
- import logging
- import urllib3
- from bs4 import BeautifulSoup
- url = 'https://api.telegram.org/bot{}/'.format(config.TOKEN)
- http = urllib3.PoolManager()
- def get_me():
- response = requests.get(url + 'getMe')
- return response.json()
- def validate_token(token):
- if any(symbol.isspace() for symbol in token):
- logging.error('[ERROR] : Invalid token')
- return false
- elif get_me()['ok']:
- logging.info('[SUCCESS] : Token successfully validated')
- return True
- else:
- logging.error('[ERROR] : Invalid token')
- return False
- def get_updates():
- response = requests.get(url + 'getUpdates')
- # print(json.dumps(json.loads(response.content), indent=4, sort_keys=True))
- return response.json()
- def send_message(chat_id, text,
- parse_mode=None,disable_web_page_preview=False,
- disable_notification=False, reply_to_message_id=None,
- reply_markup=None):
- response = requests.post(url +
- 'sendMessage?chat_id={}&text={}'
- '&parse_mode={}&disable_web_page_preview={}'
- '&disable_notification={}'
- '&reply_to_message_id={},'
- '&reply_markup={}'.format(
- chat_id,
- text,
- parse_mode,
- disable_web_page_preview,
- disable_web_page_preview,
- reply_to_message_id,
- json.dumps(reply_markup)
- ))
- # print(json.dumps(json.loads(response.content), indent=4, sort_keys=True))
- return response.json()
- def get_afisha_schedule(r):
- output_message = ''
- soup = BeautifulSoup(r.text, 'html.parser')
- content = soup.find('div', {'id': 'widget-content'})
- items = content.find_all('li', {'class': 'item___h6O8Y'})
- for item in items:
- title = item.find('h3', {'class': 'header___3Mt6k'})
- output_message += title.text + '\n'
- li = item.find_all('li', {'class': 'tooltip timetable__item'})
- for l in li:
- time = l.find('time', {'class': 'timetable__item-time'})
- output_message += time.text + ' '
- output_message += '\n\n'
- return output_message
- def start_handle():
- update_id = 0
- while True:
- data = get_updates()['result'][-1]
- if update_id != data['update_id']:
- update_id = data['update_id']
- chat_id = data['message']['chat']['id']
- if 'text' in data['message']:
- user_message = data['message']['text']
- if data['message']['text'] == '/start':
- output_message = 'Привет! Выбери кинотеатр!'
- keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
- send_message(chat_id, output_message, reply_markup=keyboard)
- elif data['message']['text'] == 'Иллюзион':
- r = requests.get('https://www.afisha.ru/msk/cinema/2692/')
- if (r):
- output_message = get_afisha_schedule(r)
- keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
- send_message(chat_id, output_message, reply_markup=keyboard, parse_mode='HTML')
- else:
- output_message = 'Нет доступа к сайту кинотеатра, попробуйте ещё раз.'
- keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
- send_message(chat_id, output_message, reply_markup=keyboard)
- elif data['message']['text'] == 'Родина':
- r = requests.get('https://www.afisha.ru/msk/cinema/3083/')
- if (r):
- output_message = get_afisha_schedule(r)
- keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
- send_message(chat_id, output_message, reply_markup=keyboard, parse_mode='HTML')
- else:
- output_message = 'Нет доступа к сайту кинотеатра, попробуйте ещё раз.'
- keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
- send_message(chat_id, output_message, reply_markup=keyboard)
- def main():
- if not validate_token(config.TOKEN):
- logging.info('[EXITTING]')
- else:
- start_handle()
- if __name__=='__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement