server.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import socket, threading
  2. import sqlite3
  3. import time
  4. from selenium import webdriver
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.firefox.options import Options
  7. options = Options()
  8. options.headless = False
  9. class ClientThread(threading.Thread):
  10. def __init__(self,clientAddress,clientsocket):
  11. threading.Thread.__init__(self)
  12. self.csocket = clientsocket
  13. print ("Новое подключение: ", clientAddress)
  14. def run(self):
  15. print ("Подключение с клиента : ", clientAddress)
  16. msg = ''
  17. while True:
  18. data = self.csocket.recv(4096)
  19. msg = data.decode()
  20. print(msg)
  21. if msg == '':
  22. print("Отключение")
  23. break
  24. elif msg == '979879789':
  25. self.csocket.send(bytes('Что найти?','UTF-8'))
  26. elif msg == 'Поиск по названию':
  27. f = open("text.txt", "w")
  28. f.write("название")
  29. f.close()
  30. self.csocket.send(bytes('Введите название:','UTF-8'))
  31. elif msg == 'Поиск по описанию':
  32. f = open("text.txt", "w")
  33. f.write("описание")
  34. f.close()
  35. self.csocket.send(bytes('Введите описание:','UTF-8'))
  36. elif msg == 'Случайный':
  37. f = open("text.txt", "w")
  38. f.write("cлучайный")
  39. f.close()
  40. self.csocket.send(bytes('Обрабатываю запрос...','UTF-8'))
  41. db = sqlite3.connect('KINO3.db')
  42. cur = db.cursor()
  43. for rand in cur.execute('SELECT * FROM KINO3 WHERE ID IN (SELECT ID FROM KINO3 ORDER BY RANDOM() LIMIT 1)'):
  44. print(rand)
  45. name = rand[2]
  46. god = rand[3]
  47. opisanie = rand[4]
  48. link1 = rand[5]
  49. driver = webdriver.Firefox(options=options)
  50. try:
  51. driver.get(link1)
  52. time.sleep(2)
  53. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
  54. element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
  55. print(element2.get_attribute('src'))
  56. itog = str(element2.get_attribute('src'))
  57. a1 = itog.split('/240.mp4')
  58. a2 = str(a1[0]) + "/720.mp4"
  59. print(a2)
  60. link2 = a2
  61. except:
  62. print("Ошибка")
  63. link2 = "Ошибка"
  64. driver.quit()
  65. q = open("text.txt", "w")
  66. q.write(str(name))
  67. q.write('\n')
  68. q.write(str(god))
  69. q.write('\n')
  70. q.write('\n')
  71. q.write(str(opisanie))
  72. q.write('\n')
  73. q.write('\n')
  74. q.write(str(link1))
  75. q.write('\n')
  76. q.write('\n')
  77. q.write(str(link2))
  78. q.close()
  79. msgH = open("text.txt", "r")
  80. msgR = msgH.read()
  81. qqq = str(msgR)
  82. print(qqq)
  83. self.csocket.send(bytes(qqq, 'UTF-8'))
  84. else:
  85. self.csocket.send(bytes('Обрабатываю запрос...','UTF-8'))
  86. db = sqlite3.connect('KINO3.db')
  87. cur = db.cursor()
  88. word = msg
  89. r = open("text.txt", "r")
  90. readR = r.read()
  91. if readR == "название":
  92. driver = webdriver.Firefox(options=options)
  93. name_list = []
  94. opisanie_list = []
  95. god_list = []
  96. Link1_list = []
  97. Link2_list = []
  98. for name in cur.execute('SELECT NAME FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  99. name_list.append(name[0])
  100. for opisanie in cur.execute('SELECT OPISANIE FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  101. opisanie_list.append(opisanie[0])
  102. for god in cur.execute('SELECT GOD FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  103. god_list.append(god[0])
  104. for Link1 in cur.execute('SELECT LINK_STR FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
  105. Link1_list.append(Link1[0])
  106. for film in Link1_list:
  107. try:
  108. driver.get(film)
  109. time.sleep(2)
  110. driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
  111. element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
  112. itog = str(element2.get_attribute('src'))
  113. a1 = itog.split('/240.mp4')
  114. a2 = str(a1[0]) + "/720.mp4"
  115. print(a2)
  116. Link2_list.append(a2)
  117. except:
  118. print("Ошибка")
  119. Link2_list.append("Ошибка")
  120. driver.quit()
  121. i = 0
  122. while i < len(name_list):
  123. q = open("text.txt", "w")
  124. q.write(str(name_list[i]))
  125. q.write('\n')
  126. q.write(str(god_list[i]))
  127. q.write('\n')
  128. q.write('\n')
  129. q.write(str(opisanie_list[i]))
  130. q.write('\n')
  131. q.write('\n')
  132. q.write(str(Link1_list[i]))
  133. q.write('\n')
  134. q.write('\n')
  135. q.write(str(Link2_list[i]))
  136. q.close()
  137. msgH = open("text.txt", "r")
  138. msgR = msgH.read()
  139. self.csocket.send(bytes(msgR, 'UTF-8'))
  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. driver.quit()
  180. i = 0
  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. msgH = open("text.txt", "r")
  197. msgR = msgH.read()
  198. self.csocket.send(bytes(msgR, 'UTF-8'))
  199. time.sleep(1)
  200. i = i + 1
  201. print ("Запрос " + str(msg) + " обработан")
  202. print ("Клиент ", clientAddress , " покинул нас...")
  203. LOCALHOST = "10.8.0.6"
  204. PORT = 1488
  205. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  206. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  207. server.bind((LOCALHOST, PORT))
  208. print("Сервер запущен!")
  209. while True:
  210. server.listen(1)
  211. clientsock, clientAddress = server.accept()
  212. newthread = ClientThread(clientAddress, clientsock)
  213. newthread.start()