Skip to content
Snippets Groups Projects
txtdownloader.py 2.45 KiB
Newer Older
kjk's avatar
kjk committed
import urllib.parse
from pathlib import Path

kjk's avatar
kjk committed
import pandas

from UltraStarSongFileParser import UltraStarSongFileParser
kjk's avatar
kjk committed
import requests
from bs4 import BeautifulSoup


class TXTDownloader:
kjk's avatar
kjk committed
    def __init__(self, sessid: str, outdir: Path, cache: bool):
kjk's avatar
kjk committed
        self.sessid = sessid
        self.outdir = outdir
kjk's avatar
kjk committed
        self.txtparser = UltraStarSongFileParser()
        self.cache = cache

    def download(self, url: str, artist: str, song: str, spotify_uri: str, gap: str, video_gap: str, start: str, end: str, language: str, year: str):
        if (Path(self.outdir) / f"{artist} - {song}.txt").is_file() and self.cache:
            return
kjk's avatar
kjk committed

        url = url.replace("detail", "gettxt")

        with requests.Session() as s:
            page = s.post(url, data={'wd': 1}, cookies={'PHPSESSID': self.sessid})
            soup = BeautifulSoup(page.content, 'html.parser')
            txt = soup.find(name="textarea").text

            Path(self.outdir).mkdir(parents=True, exist_ok=True)
kjk's avatar
kjk committed
            with open(Path(self.outdir) / f"{artist} - {song}.txt", "w", encoding="utf-8") as f:
kjk's avatar
kjk committed
                f.write(txt)

kjk's avatar
kjk committed
            self.fix_file(Path(self.outdir) / f"{artist} - {song}.txt", artist, song, spotify_uri, gap, video_gap, start, end, language, year)

    def fix_file(self, file: Path, artist: str, song: str, spotify_uri: str, gap: str, video_gap: str, start: str, end: str, language: str, year: str):
        txt = self.txtparser.parse_file(str(file), encoding="utf-8")
        txt.mp3 = str(file.with_suffix(".mp3").relative_to(file.parent))
        txt.video = str(file.with_suffix(".mp4").relative_to(file.parent))
        txt.cover = str(file.with_suffix(".jpg").relative_to(file.parent))

TheJoKlLa's avatar
TheJoKlLa committed
        if (type(gap) == int or type(gap) == float)and type(gap) != str:
kjk's avatar
kjk committed
            txt.gap = gap

TheJoKlLa's avatar
TheJoKlLa committed
        if (type(gap) == int or type(gap) == float)and type(gap) != str:
kjk's avatar
kjk committed
            txt.videogap = video_gap

TheJoKlLa's avatar
TheJoKlLa committed
        if (type(gap) == int or type(gap) == float)and type(gap) != str:
kjk's avatar
kjk committed
            txt.start = start

TheJoKlLa's avatar
TheJoKlLa committed
        if (type(gap) == int or type(gap) == float)and type(gap) != str:
kjk's avatar
kjk committed
            txt.end = end

        if type(language) == str and language != " ":
            txt.language = language

        if type(artist) == str and artist != " ":
            txt.artist = artist

        if type(song) == str and song != " ":
            txt.title = song

        if type(year) == str and year != " ":
            txt.year = year

        txt.songid = spotify_uri

        txt.dump(file)