Skip to content
Snippets Groups Projects
txtdownloader.py 2.17 KiB
Newer Older
kjk's avatar
kjk committed
from pathlib import Path
kjk's avatar
kjk committed
from UltraStarSongFileParser import UltraStarSongFileParser
kjk's avatar
kjk committed
import requests
from bs4 import BeautifulSoup
TheJoKlLa's avatar
TheJoKlLa committed
import datetime
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
from song import Song
kjk's avatar
kjk committed

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

TheJoKlLa's avatar
TheJoKlLa committed
    def download(self, song: Song):
        if (Path(self.outdir) / f"{song.getFileName_Hash_TXT()}.txt").is_file() and self.cache:
kjk's avatar
kjk committed
            return
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        url = song.txt_link.replace("detail", "gettxt")
TheJoKlLa's avatar
TheJoKlLa committed
        url = url.replace("http://", "https://")
kjk's avatar
kjk committed

        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
TheJoKlLa's avatar
TheJoKlLa committed
            
            lines = []
            for line in txt.split('\n'):
                if line.strip() != "":
                    lines.append(line.replace("\r", ""))
            txt = "\n".join(lines)
kjk's avatar
kjk committed

            Path(self.outdir).mkdir(parents=True, exist_ok=True)
TheJoKlLa's avatar
TheJoKlLa committed
            with open(Path(self.outdir) / f"{song.getFileName_Hash_TXT()}.txt", "w", encoding="utf-8") as f:
kjk's avatar
kjk committed
                f.write(txt)
TheJoKlLa's avatar
TheJoKlLa committed
                
    def fix_file(self, file: Path, song: Song):
kjk's avatar
kjk committed
        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
        txt.songid = song.uri
        txt.artist = song.artist_name
        txt.title = song.track_name
        
TheJoKlLa's avatar
TheJoKlLa committed
        if song.gap != None:
TheJoKlLa's avatar
TheJoKlLa committed
            txt.gap = song.gap
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        if song.video_gap != None:
TheJoKlLa's avatar
TheJoKlLa committed
            txt.videogap = song.video_gap
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        if song.start != None:
TheJoKlLa's avatar
TheJoKlLa committed
            txt.start = song.start
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        if song.gap != None:
TheJoKlLa's avatar
TheJoKlLa committed
            txt.end = song.end
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        if song.language != None:
TheJoKlLa's avatar
TheJoKlLa committed
            txt.language = song.language
kjk's avatar
kjk committed

TheJoKlLa's avatar
TheJoKlLa committed
        if song.release_date != datetime.date.min:
            txt.year = song.release_date
kjk's avatar
kjk committed

        txt.dump(file)