Files
music-downloader/main.py
2026-03-31 14:56:37 +03:00

97 lines
3.3 KiB
Python

from asyncio.windows_events import NULL
import os
import subprocess
from rich import console
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.progress import track
from selenium import webdriver
from selenium.webdriver import Chrome
import selenium.webdriver
import selenium.webdriver.chrome
import selenium.webdriver.chrome.options
from selenium.webdriver.common.by import By
import selenium
import time
import re
# driver = webdriver.Chrome()
# driver.get("https://muzpab.xn--41a.ws/")
# time.sleep(1)
# search = driver.find_element(By.NAME, "q")
# search.send_keys("Апалогия Время")
# search.submit()
# print(driver.find_element(By.CLASS_NAME, "artist_name").text)
# button = driver.find_element(By.CLASS_NAME, "downloadbtn")
# button.click()
# time.sleep(1)
# button = driver.find_element(By.CLASS_NAME, "__download_btn")
# button.click()
# print("Скачивание началось")
# time.sleep(10)
def normalize(text):
# 1. нижний регистр
text = text.lower()
# 2. заменить тире на пробел
text = text.replace("", " ").replace("-", " ")
# 3. убрать всё кроме букв и цифр
text = re.sub(r"[^\w\s]", "", text)
# 4. разбить на слова и отсортировать
words = text.split()
words.sort()
return words
def start():
console = Console()
console.print(Panel("Запуск программы"))
options = selenium.webdriver.chrome.options.Options()
options.add_argument("--headless")
driver = Chrome(options=options)
driver.get("https://muzpab.xn--41a.ws/")
console.print(driver.title, style="bold green")
file = open("music.txt", "r", encoding="utf-8")
new_file = open("new_music.txt", "w", encoding="utf-8")
bad_music = open("bad_music.txt", "w", encoding="utf-8")
table = Table(title="Музыка для поиска")
table.add_column("Номер трека", justify="center", style="cyan", no_wrap=True)
table.add_column("Название трека", justify="center")
table.add_column("Найденный трек", justify="center")
i = 1
for line in file:
sound_name = re.sub(r'^\d+\.\s*', '', line.strip())
sound_name_normalized = normalize(sound_name)
console.print(f"Ищем трек: {sound_name}")
search = driver.find_element(By.NAME, "q")
search.send_keys(sound_name)
search.submit()
track = driver.find_elements(By.CLASS_NAME, "artist_name")
track_normalized = normalize(track[0].text)
if track_normalized == sound_name_normalized:
console.print(f"Найден трек: [green]{track[0].text}[/green]")
table.add_row(str(i), sound_name, f"[green]{track[0].text}[/green]")
new_file.write(track[0].text + "\n")
else:
console.print(f"Найден трек: [red]{track[0].text}[/red]")
table.add_row(str(i), sound_name, f"[red]{track[0].text}[/red]")
bad_music.write(sound_name + " // " + track[0].text + "\n")
i += 1
subprocess.run("cls", shell=True)
console.print(table)
new_file.close()
bad_music.close()
if __name__ == "__main__":
start()