bot.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import sqlite3
  2. import telebot
  3. from telebot import types
  4. import time
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.firefox.options import Options
  8. bot = telebot.TeleBot("токен бота")
  9. options = Options()
  10. options.headless = True
  11. @bot.message_handler(commands=['start'])
  12. def send_welcome(message):
  13. chat_id = message.chat.id
  14. markup = types.ReplyKeyboardMarkup(row_width=1)
  15. itembtn1 = types.KeyboardButton('Поиск по названию')
  16. itembtn2 = types.KeyboardButton('Поиск по описанию')
  17. itembtn3 = types.KeyboardButton('Случайный')
  18. markup.add(itembtn1, itembtn2, itembtn3)
  19. bot.send_message(chat_id, "Что найти?", reply_markup=markup)
  20. @bot.message_handler(func=lambda message: True)
  21. def echo_all(message):
  22. if message.text == 'Поиск по названию':
  23. f = open("text.txt", "w")
  24. f.write("название")
  25. f.close()
  26. chat_id = message.chat.id
  27. bot.send_message(chat_id, "Введите название:")
  28. elif message.text == 'Поиск по описанию':
  29. f = open("text.txt", "w")
  30. f.write("описание")
  31. f.close()
  32. chat_id = message.chat.id
  33. bot.send_message(chat_id, "Введите описание:")
  34. elif message.text == 'Случайный':
  35. f = open("text.txt", "w")
  36. f.write("cлучайный")
  37. f.close()
  38. chat_id = message.chat.id
  39. bot.send_message(chat_id, "Обрабатываю запрос...")
  40. db = sqlite3.connect('KINO3.db')
  41. cur = db.cursor()
  42. for rand in cur.execute('SELECT * FROM KINO3 WHERE ID IN (SELECT ID FROM KINO3 ORDER BY RANDOM() LIMIT 1)'):
  43. print(rand)
  44. name = rand[2]
  45. god = rand[3]
  46. opisanie = rand[4]
  47. link1 = rand[5]
  48. driver = webdriver.Firefox(options=options)
  49. try:
  50. driver.get(link1)
  51. time.sleep(2)
  52. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
  53. element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
  54. print(element2.get_attribute('src'))
  55. itog = str(element2.get_attribute('src'))
  56. a1 = itog.split('/240.mp4')
  57. a2 = str(a1[0]) + "/720.mp4"
  58. print(a2)
  59. link2 = a2
  60. except:
  61. print("Ошибка")
  62. link2 = "Ошибка"
  63. driver.quit()
  64. q = open("text.txt", "w")
  65. q.write(str(name))
  66. q.write('\n')
  67. q.write(str(god))
  68. q.write('\n')
  69. q.write('\n')
  70. q.write(str(opisanie))
  71. q.write('\n')
  72. q.write('\n')
  73. q.write(str(link1))
  74. q.write('\n')
  75. q.write('\n')
  76. q.write(str(link2))
  77. q.close()
  78. msg = open("text.txt", "r")
  79. msgR = msg.read()
  80. chat_id = message.chat.id
  81. bot.send_message(chat_id, msgR)
  82. else:
  83. chat_id = message.chat.id
  84. bot.send_message(chat_id, "Обрабатываю запрос...")
  85. db = sqlite3.connect('KINO3.db')
  86. cur = db.cursor()
  87. word = message.text
  88. r = open("text.txt", "r")
  89. readR = r.read()
  90. if readR == "название":
  91. driver = webdriver.Firefox(options=options)
  92. name_list = []
  93. opisanie_list = []
  94. god_list = []
  95. Link1_list = []
  96. Link2_list = []
  97. for name in cur.execute('SELECT NAME FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  98. name_list.append(name[0])
  99. for opisanie in cur.execute('SELECT OPISANIE FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  100. opisanie_list.append(opisanie[0])
  101. for god in cur.execute('SELECT GOD FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  102. god_list.append(god[0])
  103. for Link1 in cur.execute('SELECT LINK_STR FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  104. Link1_list.append(Link1[0])
  105. for film in Link1_list:
  106. try:
  107. driver.get(film)
  108. time.sleep(2)
  109. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
  110. element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
  111. itog = str(element2.get_attribute('src'))
  112. a1 = itog.split('/240.mp4')
  113. a2 = str(a1[0]) + "/720.mp4"
  114. print(a2)
  115. Link2_list.append(a2)
  116. except:
  117. print("Ошибка")
  118. Link2_list.append("Ошибка")
  119. driver.quit()
  120. i = 0
  121. while i < len(name_list):
  122. q = open("text.txt", "w")
  123. q.write(str(name_list[i]))
  124. q.write('\n')
  125. q.write(str(god_list[i]))
  126. q.write('\n')
  127. q.write('\n')
  128. q.write(str(opisanie_list[i]))
  129. q.write('\n')
  130. q.write('\n')
  131. q.write(str(Link1_list[i]))
  132. q.write('\n')
  133. q.write('\n')
  134. q.write(str(Link2_list[i]))
  135. q.close()
  136. msg = open("text.txt", "r")
  137. msgR = msg.read()
  138. chat_id = message.chat.id
  139. bot.send_message(chat_id, msgR)
  140. time.sleep(1)
  141. i = i + 1
  142. elif readR == "описание":
  143. driver = webdriver.Firefox(options=options)
  144. z = open('text.txt', 'w')
  145. z.seek(0)
  146. z.close()
  147. name_list = []
  148. opisanie_list = []
  149. god_list = []
  150. Link1_list = []
  151. Link2_list = []
  152. for name in cur.execute('SELECT NAME FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
  153. print(name)
  154. name_list.append(name[0])
  155. for opisanie in cur.execute('SELECT OPISANIE FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
  156. print(opisanie)
  157. opisanie_list.append(opisanie[0])
  158. for god in cur.execute('SELECT GOD FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
  159. print(god)
  160. god_list.append(god[0])
  161. for Link1 in cur.execute('SELECT LINK_STR FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
  162. print(Link1)
  163. Link1_list.append(Link1[0])
  164. for film in Link1_list:
  165. try:
  166. driver.get(film)
  167. time.sleep(2)
  168. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
  169. element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
  170. print(element2.get_attribute('src'))
  171. itog = str(element2.get_attribute('src'))
  172. a1 = itog.split('/240.mp4')
  173. a2 = str(a1[0]) + "/720.mp4"
  174. print(a2)
  175. Link2_list.append(a2)
  176. except:
  177. print("Ошибка")
  178. Link2_list.append("Ошибка")
  179. i = 0
  180. driver.quit()
  181. while i < len(name_list):
  182. q = open("text.txt", "w")
  183. q.write(str(name_list[i]))
  184. q.write('\n')
  185. q.write(str(god_list[i]))
  186. q.write('\n')
  187. q.write('\n')
  188. q.write(str(opisanie_list[i]))
  189. q.write('\n')
  190. q.write('\n')
  191. q.write(str(Link1_list[i]))
  192. q.write('\n')
  193. q.write('\n')
  194. q.write(str(Link2_list[i]))
  195. q.close()
  196. msg = open("text.txt", "r")
  197. msgR = msg.read()
  198. chat_id = message.chat.id
  199. bot.send_message(chat_id, msgR)
  200. time.sleep(1)
  201. i = i + 1
  202. bot.polling()