Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. import config
  2. import requests
  3. import json
  4. import logging
  5. import urllib3
  6. from bs4 import BeautifulSoup
  7.  
  8.  
  9. url = 'https://api.telegram.org/bot{}/'.format(config.TOKEN)
  10. http = urllib3.PoolManager()
  11.  
  12.  
  13. def get_me():
  14. response = requests.get(url + 'getMe')
  15. return response.json()
  16.  
  17.  
  18. def validate_token(token):
  19. if any(symbol.isspace() for symbol in token):
  20. logging.error('[ERROR] : Invalid token')
  21. return false
  22. elif get_me()['ok']:
  23. logging.info('[SUCCESS] : Token successfully validated')
  24. return True
  25. else:
  26. logging.error('[ERROR] : Invalid token')
  27. return False
  28.  
  29.  
  30. def get_updates():
  31. response = requests.get(url + 'getUpdates')
  32. # print(json.dumps(json.loads(response.content), indent=4, sort_keys=True))
  33. return response.json()
  34.  
  35.  
  36. def send_message(chat_id, text,
  37. parse_mode=None,disable_web_page_preview=False,
  38. disable_notification=False, reply_to_message_id=None,
  39. reply_markup=None):
  40.  
  41. response = requests.post(url +
  42. 'sendMessage?chat_id={}&text={}'
  43. '&parse_mode={}&disable_web_page_preview={}'
  44. '&disable_notification={}'
  45. '&reply_to_message_id={},'
  46. '&reply_markup={}'.format(
  47. chat_id,
  48. text,
  49. parse_mode,
  50. disable_web_page_preview,
  51. disable_web_page_preview,
  52. reply_to_message_id,
  53. json.dumps(reply_markup)
  54. ))
  55. # print(json.dumps(json.loads(response.content), indent=4, sort_keys=True))
  56. return response.json()
  57.  
  58.  
  59. def get_afisha_schedule(r):
  60. output_message = ''
  61. soup = BeautifulSoup(r.text, 'html.parser')
  62. content = soup.find('div', {'id': 'widget-content'})
  63. items = content.find_all('li', {'class': 'item___h6O8Y'})
  64. for item in items:
  65. title = item.find('h3', {'class': 'header___3Mt6k'})
  66. output_message += title.text + '\n'
  67. li = item.find_all('li', {'class': 'tooltip timetable__item'})
  68. for l in li:
  69. time = l.find('time', {'class': 'timetable__item-time'})
  70. output_message += time.text + ' '
  71. output_message += '\n\n'
  72.  
  73. return output_message
  74.  
  75.  
  76. def start_handle():
  77. update_id = 0
  78. while True:
  79. data = get_updates()['result'][-1]
  80. if update_id != data['update_id']:
  81. update_id = data['update_id']
  82. chat_id = data['message']['chat']['id']
  83. if 'text' in data['message']:
  84. user_message = data['message']['text']
  85. if data['message']['text'] == '/start':
  86. output_message = 'Привет! Выбери кинотеатр!'
  87. keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
  88. send_message(chat_id, output_message, reply_markup=keyboard)
  89.  
  90. elif data['message']['text'] == 'Иллюзион':
  91. r = requests.get('https://www.afisha.ru/msk/cinema/2692/')
  92. if (r):
  93. output_message = get_afisha_schedule(r)
  94. keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
  95. send_message(chat_id, output_message, reply_markup=keyboard, parse_mode='HTML')
  96. else:
  97. output_message = 'Нет доступа к сайту кинотеатра, попробуйте ещё раз.'
  98. keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
  99. send_message(chat_id, output_message, reply_markup=keyboard)
  100.  
  101. elif data['message']['text'] == 'Родина':
  102. r = requests.get('https://www.afisha.ru/msk/cinema/3083/')
  103. if (r):
  104. output_message = get_afisha_schedule(r)
  105. keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
  106. send_message(chat_id, output_message, reply_markup=keyboard, parse_mode='HTML')
  107. else:
  108. output_message = 'Нет доступа к сайту кинотеатра, попробуйте ещё раз.'
  109. keyboard = {'keyboard': [['Иллюзион'], ['Родина']], 'one_time_keyboard': True}
  110. send_message(chat_id, output_message, reply_markup=keyboard)
  111.  
  112.  
  113.  
  114. def main():
  115. if not validate_token(config.TOKEN):
  116. logging.info('[EXITTING]')
  117. else:
  118. start_handle()
  119.  
  120. if __name__=='__main__':
  121. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement