diff --git a/src/download.py b/src/download.py index b85c7cc..73311ec 100644 --- a/src/download.py +++ b/src/download.py @@ -10,12 +10,18 @@ from io import StringIO from contextlib import redirect_stdout import string +import urllib3 +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) mp3_url_template = "https://antiserver.kuwo.cn/anti.s?type=convert_url&rid={}&format=mp3&response=url" music_name_template = "{name}_{artist}_{album}_{id}.mp3" -destination_folder = os.getenv('MUSIC_FOLDER', "/nas/music") +destination_folder = os.getenv('MUSIC_FOLDER', "/nas/music/kuwo") root_url = os.getenv("MUSIC_URL", "https://www.kuwo.cn/bang/content?name=%E6%8A%96%E9%9F%B3%E7%83%AD%E6%AD%8C%E6%A6%9C") +invalid_music_file_path = os.path.join(destination_folder, "__INVALID_MUSICS.txt") +if not os.path.exists(invalid_music_file_path): + open(invalid_music_file_path, 'a').close() +invalid_musics=set(line.strip() for line in open(invalid_music_file_path)) @dataclass class Music: def __init__(self, id: str, artist: str, album: str, name: str, pay:str): @@ -28,9 +34,9 @@ self.targeting_file = "" self.status ="pending" self.file_name = "" - + def exists(self): - return os.path.exists(self.targeting_file) + return os.path.exists(self.targeting_file) or self.file_name in invalid_musics def download(self): print("Checking url " + self.url) @@ -116,6 +122,15 @@ except: print("error") print("[Info] Download music \"{}\" with the status: {}".format(music.file_name, music.status)) + # If the size of the file is small to an identical number, it means the music is invalid. + if os.path.exists(music.targeting_file) and os.path.getsize(music.targeting_file) == 181521: + with open(invalid_music_file_path, 'a') as the_file: + the_file.write("{}\n".format(music.file_name)) + invalid_musics.add(music.file_name) + os.remove(music.targeting_file) + new_music_count -=1 + print("[Info] Delete the music \"{}\" due to the invalid size".format(music.file_name)) + print("[Summary] Found {} new musics".format(new_music_count)) # print("Processing the music") diff --git a/src/sort.py b/src/sort.py new file mode 100644 index 0000000..0b15059 --- /dev/null +++ b/src/sort.py @@ -0,0 +1,43 @@ +import glob +import os + +musics=set() +def retrieve_musics(path): + files = glob.glob(path + '/**/*.mp3', recursive=True) + for file in files: + if file.find("@eaDir") == -1: + tokens = file.split("/") + fname = tokens[len(tokens)-1] + if fname in musics: + print ("{} in the set already".format(fname)) + os.remove(file) + else: + musics.add(fname) + print ("add {} into set".format(fname)) + os.rename(file, "/nas/music/抖音类歌曲/{}".format(fname)) + + +filename="__INVALID_MUSICS.txt" +def loadInvalidMusicsFromFile(path): + if not os.path.exists(path): + open(path, 'a').close() + return set(line.strip() for line in open(path)) + + +def findInvalidMusics(path): + invalidfilepath = os.path.join(path, filename) + invalidmusics = loadInvalidMusicsFromFile(invalidfilepath) + files = glob.glob(path + '/**/*.mp3', recursive=True) + for file in files: + if file.find("@eaDir") == -1: + tokens = file.split("/") + fname = tokens[len(tokens)-1] + sz = os.path.getsize(file) + if sz == 181521 and fname not in invalidmusics: + with open(invalidfilepath, 'a') as the_file: + the_file.write("{}\n".format(fname)) + invalidmusics.add(fname) + print("file found: {} is invalid and add it into the list".format(file)) + os.remove(file) + +findInvalidMusics("/nas/music/kuwo") \ No newline at end of file diff --git a/test/start.sh b/test/start.sh new file mode 100644 index 0000000..9e4bdf2 --- /dev/null +++ b/test/start.sh @@ -0,0 +1,6 @@ +# /bin/bash + +export MUSIC_OP="download" +export MUSIC_FOLDER="music" +python3 ../src/download.py +