1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
from bs4 import BeautifulSoup from requests import get,head from os.path import isdir from os import mkdir from shutil import copy from sys import argv,exit from eyed3 import load from loguru import logger from fake_useragent import UserAgent
true = True
def main(id,path=0): try: if isdir('output'): pass else: mkdir('output') ua = UserAgent() url = f"https://music.163.com/song?id={id}" headers = { "User-Agent": ua.random,
} response = get(url=url,headers=headers) a = response.text bs = BeautifulSoup(a,'html.parser') contents = [] for i in bs.find_all('meta'): try: i['property'] contents.append(i['content']) except: pass title = contents[1] artist = contents[9] album = contents[10] img_url = contents[3] save_name = f'{title} - {artist}'.replace('/',' ')
if path: copy(path,'output\\'+path.split('\\')[-1]) save_name = '' data = path.split('\\')[-1].split('.') del(data[-1]) for i in data: save_name += i else: logger.info('下载音乐') song_url = head(f'https://music.163.com/song/media/outer/url?id={id}.mp3',headers=headers).headers['Location'] if song_url == 'http://music.163.com/404': logger.error('无资源') else: data = get(song_url) with open('output\\'+save_name+'.mp3','wb') as f: f.write(data.content)
logger.info('下载歌词') data = get('https://music.163.com/api/song/media?id='+id).text try: lrc = eval(data)['lyric'] with open('output\\'+save_name+'.lrc', 'w', encoding='utf-8') as f: f.write(lrc) except: logger.info('无歌词') logger.info('下载封面') data = get(img_url).content
logger.info(f''' -----歌曲信息----- 歌曲名:{title} 作曲家:{artist} 专辑:{album}''') logger.info('写入信息') write_tags(save_name,title,artist,album,data) logger.success('成功:'+'output\\'+save_name+'.mp3') except Exception as e: logger.error(str(e))
def write_tags(save_name,title,artist,album,data): audioFile = load(path='output\\'+save_name+'.mp3') audioFile.tag.artist = artist audioFile.tag.title = title audioFile.tag.album = album audioFile.tag.images.set(3, data, 'image/jpeg', u'Cover') audioFile.tag.save()
try: data = argv[1] if len(argv) > 2: path = argv[2] else: path = '' except Exception as e: logger.error(str(e)) print(f'用法:\n{argv[0]} [网易云音乐歌曲id或url] [需要写入信息的歌曲文件路径(可选)]') exit()
try: id = str(int(data)) except: try: id = data.split('id=')[1].split('&')[0] except Exception as e: logger.error(str(e)) logger.info('id:'+id) main(id,path)
|