from multiprocessing import Process, Pool
import paramiko
import requests
import os
from bs4 import BeautifulSoup
from googletrans import Translator
import random
def main(domen):
with open('server_auth.ini', 'r') as file:
server_auth = file.read().splitlines()
host = server_auth[0]
username = server_auth[1]
password = server_auth[2]
port = 1321
translator = Translator()
while True:
transport = paramiko.Transport((host, port))
transport.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
if f'temp_{domen}_repeats_ru.txt' in os.listdir():
pass
else:
with open(f'temp_{domen}_repeats_ru.txt', 'x') as file:
pass
with open(f'temp_{domen}_repeats_ru.txt', 'r') as file:
repeats = file.read().splitlines()
for file in sftp.listdir(f'/home/default/default/{domen}'):
#копирование вопросов
if '.html' in file and file not in repeats and file[0].isupper == True:
res = requests.get(f'http://{domen}/{file}')
soup = BeautifulSoup(res.text, 'lxml')
if soup.select_one('div.card:nth-child(4)'): #проверяем есть ли на странице с вопросом другие вопросы
for tag in soup.find_all(): #перебираем все теги на странице
if tag.text: #если в теге есть текст
current_tag_text = tag.text
tag.text = translator.translate(current_tag_text, dest = 'ru')
rnd_file = f'{random.range(0,999999999999)}.txt'
with open(rnd_file, 'a+', encoding='utf-8') as rndf:
rndf.write(soup.prettify())
if f'ru.{domen}' not in sftp.listdir(f'/home/default/default'):
sftp.mkdir(f'/home/default/default/ru.{domen}')
sftp.put(rnd_file, f'/home/default/default/ru.{domen}')
with open(f'temp_{domen}_repeats_ru.txt', 'a') as rfile:
rfile.write(f'{file}\n')
os.remove(rnd_file)
pass
#копирование индекса
elif 'index.html' in file and file not in repeats:
pass
#копирование категорий
elif '.html' in file and file not in repeats and file[0].islower == True and file != 'index.html':
pass
sftp.close()
transport.close()
if __name__ == '__main__':
with open('domens.ini', 'r') as file:
domens = file.read().splitlines()
p = Pool(processes=len(domens))
p.map(main, domens)