123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- import socket, threading
- import sqlite3
- import time
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.firefox.options import Options
- options = Options()
- options.headless = False
- class ClientThread(threading.Thread):
- def __init__(self,clientAddress,clientsocket):
- threading.Thread.__init__(self)
- self.csocket = clientsocket
- print ("Новое подключение: ", clientAddress)
- def run(self):
- print ("Подключение с клиента : ", clientAddress)
- msg = ''
- while True:
- data = self.csocket.recv(4096)
- msg = data.decode()
- print(msg)
-
- if msg == '':
- print("Отключение")
- break
-
- elif msg == '979879789':
- self.csocket.send(bytes('Что найти?','UTF-8'))
-
- elif msg == 'Поиск по названию':
- f = open("text.txt", "w")
- f.write("название")
- f.close()
- self.csocket.send(bytes('Введите название:','UTF-8'))
- elif msg == 'Поиск по описанию':
- f = open("text.txt", "w")
- f.write("описание")
- f.close()
- self.csocket.send(bytes('Введите описание:','UTF-8'))
- elif msg == 'Случайный':
- f = open("text.txt", "w")
- f.write("cлучайный")
- f.close()
- self.csocket.send(bytes('Обрабатываю запрос...','UTF-8'))
- db = sqlite3.connect('KINO3.db')
- cur = db.cursor()
- for rand in cur.execute('SELECT * FROM KINO3 WHERE ID IN (SELECT ID FROM KINO3 ORDER BY RANDOM() LIMIT 1)'):
- print(rand)
- name = rand[2]
- god = rand[3]
- opisanie = rand[4]
- link1 = rand[5]
- driver = webdriver.Firefox(options=options)
- try:
- driver.get(link1)
- time.sleep(2)
-
- driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
- element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
-
-
- print(element2.get_attribute('src'))
-
- itog = str(element2.get_attribute('src'))
-
- a1 = itog.split('/240.mp4')
- a2 = str(a1[0]) + "/720.mp4"
- print(a2)
-
- link2 = a2
-
- except:
- print("Ошибка")
-
- link2 = "Ошибка"
- driver.quit()
-
- q = open("text.txt", "w")
- q.write(str(name))
- q.write('\n')
- q.write(str(god))
- q.write('\n')
- q.write('\n')
- q.write(str(opisanie))
- q.write('\n')
- q.write('\n')
- q.write(str(link1))
- q.write('\n')
- q.write('\n')
- q.write(str(link2))
- q.close()
-
- msgH = open("text.txt", "r")
- msgR = msgH.read()
- qqq = str(msgR)
- print(qqq)
- self.csocket.send(bytes(qqq, 'UTF-8'))
-
- else:
- self.csocket.send(bytes('Обрабатываю запрос...','UTF-8'))
- db = sqlite3.connect('KINO3.db')
- cur = db.cursor()
- word = msg
- r = open("text.txt", "r")
- readR = r.read()
- if readR == "название":
- driver = webdriver.Firefox(options=options)
-
- name_list = []
- opisanie_list = []
- god_list = []
- Link1_list = []
- Link2_list = []
- for name in cur.execute('SELECT NAME FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
- name_list.append(name[0])
- for opisanie in cur.execute('SELECT OPISANIE FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
- opisanie_list.append(opisanie[0])
- for god in cur.execute('SELECT GOD FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
- god_list.append(god[0])
- for Link1 in cur.execute('SELECT LINK_STR FROM KINO3 WHERE NAME LIKE ?', ('%'+word+'%',)):
- Link1_list.append(Link1[0])
- for film in Link1_list:
- try:
- driver.get(film)
- time.sleep(2)
- driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
- element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
- itog = str(element2.get_attribute('src'))
- a1 = itog.split('/240.mp4')
- a2 = str(a1[0]) + "/720.mp4"
- print(a2)
- Link2_list.append(a2)
- except:
- print("Ошибка")
- Link2_list.append("Ошибка")
- driver.quit()
- i = 0
- while i < len(name_list):
- q = open("text.txt", "w")
- q.write(str(name_list[i]))
- q.write('\n')
- q.write(str(god_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(opisanie_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(Link1_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(Link2_list[i]))
- q.close()
- msgH = open("text.txt", "r")
- msgR = msgH.read()
-
- self.csocket.send(bytes(msgR, 'UTF-8'))
- time.sleep(1)
- i = i + 1
- elif readR == "описание":
- driver = webdriver.Firefox(options=options)
- z = open('text.txt', 'w')
- z.seek(0)
- z.close()
- name_list = []
- opisanie_list = []
- god_list = []
- Link1_list = []
- Link2_list = []
- for name in cur.execute('SELECT NAME FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
- print(name)
- name_list.append(name[0])
- for opisanie in cur.execute('SELECT OPISANIE FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
- print(opisanie)
- opisanie_list.append(opisanie[0])
- for god in cur.execute('SELECT GOD FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
- print(god)
- god_list.append(god[0])
- for Link1 in cur.execute('SELECT LINK_STR FROM KINO3 WHERE OPISANIE LIKE ?', ('%'+word+'%',)):
- print(Link1)
- Link1_list.append(Link1[0])
- for film in Link1_list:
-
- try:
- driver.get(film)
- time.sleep(2)
- driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
- element2 = driver.find_element(By.XPATH, """/html/body/div/pjsdiv/pjsdiv[1]/video""")
-
- print(element2.get_attribute('src'))
- itog = str(element2.get_attribute('src'))
- a1 = itog.split('/240.mp4')
- a2 = str(a1[0]) + "/720.mp4"
- print(a2)
- Link2_list.append(a2)
- except:
- print("Ошибка")
- Link2_list.append("Ошибка")
- driver.quit()
- i = 0
- while i < len(name_list):
- q = open("text.txt", "w")
- q.write(str(name_list[i]))
- q.write('\n')
- q.write(str(god_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(opisanie_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(Link1_list[i]))
- q.write('\n')
- q.write('\n')
- q.write(str(Link2_list[i]))
- q.close()
- msgH = open("text.txt", "r")
- msgR = msgH.read()
-
- self.csocket.send(bytes(msgR, 'UTF-8'))
- time.sleep(1)
- i = i + 1
-
- print ("Запрос " + str(msg) + " обработан")
- print ("Клиент ", clientAddress , " покинул нас...")
- LOCALHOST = "10.8.0.6"
- PORT = 1488
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- server.bind((LOCALHOST, PORT))
- print("Сервер запущен!")
- while True:
- server.listen(1)
- clientsock, clientAddress = server.accept()
- newthread = ClientThread(clientAddress, clientsock)
- newthread.start()
|