Music_Download icon indicating copy to clipboard operation
Music_Download copied to clipboard

音乐名称带有windows不合法的半角字符,导致下载失败的解决方法

Open One-sixth opened this issue 4 years ago • 2 comments

将windows对应不合法的半角字符转化为全角字符就行了。

为节省时间 可以在 Music_Funciton.py 将 music_download 整个函数替换为如下代码即可快速解决

import os

def half2full(s):
    ns = []
    for c in s:
        num = ord(c)
        if num == 0x20:
            num = 0x3000
        elif 0x21 <= num <= 0x7e:
            num += 0xfee0
        ns.append(chr(num))
    return ''.join(ns)


def full2half(s):
    ns = []
    for c in s:
        num = ord(c)
        if num == 0x3000:
            num = 0x20
        elif 0x21+0xfee0 <= num <= 0x7e+0xfee0:
            num -= 0xfee0
        ns += chr(num)
    ns = ''.join(ns)
    return ns


def make_valid_name(name):
    bad_chars = r'/\:*?"<>|'
    nl = list(name)
    for i, c in enumerate(nl):
        if c in bad_chars:
            nl[i] = half2full(c)
    name = ''.join(nl)
    return name


def music_download(song_url,name,sing_name=None):
    response = requests.get(song_url, headers=heades[random.randint(0, len(heades)-1)])

    os.makedirs('Music', exist_ok=True)
    name = make_valid_name(name)
    if sing_name is not None:
        sing_name = make_valid_name(sing_name)

    if sing_name == None:
        with open("Music" + '/' + f"{name}.mp3", 'wb') as f:
            f.write(response.content)
    else:
        with open("Music" + '/' + f"{name}-{sing_name}.mp3", 'wb') as f:
            f.write(response.content)


One-sixth avatar May 31 '21 09:05 One-sixth

感谢!这个问题我之前就准备解决,但是时间过得太久了,忘记了。现在这个软件已经没有维护了

---原始邮件--- 发件人: @.> 发送时间: 2021年5月31日 17:59:55 收件人: @.>; 抄送: @.***>; 主题: [Java-S12138/Music_Dowload] 音乐名称带有windows不合法的半角字符,导致下载失败的解决方法 (#2)

将windows对应不合法的半角字符转化为全角字符就行了。

为节省时间 可以在 Music_Funciton.py 将 music_download 整个函数替换为如下代码即可快速解决 import os def half2full(s): ns = [] for c in s: num = ord(c) if num == 0x20: num = 0x3000 elif 0x21 <= num <= 0x7e: num += 0xfee0 ns.append(chr(num)) return ''.join(ns) def full2half(s): ns = [] for c in s: num = ord(c) if num == 0x3000: num = 0x20 elif 0x21+0xfee0 <= num <= 0x7e+0xfee0: num -= 0xfee0 ns += chr(num) ns = ''.join(ns) return ns def make_valid_name(name): bad_chars = r'/:*?"<>|' nl = list(name) for i, c in enumerate(nl): if c in bad_chars: nl[i] = half2full(c) name = ''.join(nl) return name def music_download(song_url,name,sing_name=None): response = requests.get(song_url, headers=heades[random.randint(0, len(heades)-1)]) os.makedirs('Music', exist_ok=True) name = make_valid_name(name) if sing_name is not None: sing_name = make_valid_name(sing_name) if sing_name == None: with open("Music" + '/' + f"{name}.mp3", 'wb') as f: f.write(response.content) else: with open("Music" + '/' + f"{name}-{sing_name}.mp3", 'wb') as f: f.write(response.content)
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Java-S12138 avatar May 31 '21 10:05 Java-S12138

@Java-S12138 这个软件现在还能工作,那就稍微更新下,让它更好用把。

One-sixth avatar Jun 01 '21 03:06 One-sixth