Files
MikOS/sys/lib/music/__init__.mia
2026-01-25 17:01:16 +08:00

182 lines
7.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import tkinter as tk
try:
#绝对会报错,只是用于高亮提示,可以删除,真正的导入部分在Kernel
import sys.kernel.UI as UI
from sys.kernel.tools import *
except:
pass
import requests
import json
from tkinter.simpledialog import askstring
from os import getcwd, mkdir, startfile
from tkinter.messagebox import showinfo
from shutil import move, Error
from os.path import exists
from os import listdir
from tkinter import Tk, StringVar,Button,messagebox, Radiobutton
from tkinter import ttk
from sys import exit as ex
from pygame import mixer
import threading
from tkinter.simpledialog import askfloat
from tkinter.messagebox import showerror
def init():
if exists('Music_Download') == True:
pass
else:
dir_name = getcwd()
mkdir(dir_name + '\\Music_Download')
def search():
# 伪装自己的信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0',
'Accept': 'application/json, text/plain, */*',
'Referer': 'https://kuwo.cn',
'Secret': '10373b58aee58943f95eaf17d38bc9cf50fbbef8e4bf4ec6401a3ae3ef8154560507f032',
'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1687520303,1689840209; _ga=GA1.2.2021483490.1666455184; _ga_ETPBRPM9ML=GS1.2.1689840210.4.1.1689840304.60.0.0; Hm_Iuvt_cdb524f42f0ce19b169b8072123a4727=NkA4TadJGeBWwmP2mNGpYRrM8f62K8Cm; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1689840223; _gid=GA1.2.1606176174.1689840209; _gat=1',
}
name = askstring('获取名称', "请输入你要搜索的“音乐名称”或“歌手")
# 音乐列表的地址
list_url = f'https://songsearch.kugou.com/song_search_v2?keyword={name}&platform=WebFilter'
list_resp = requests.get(list_url, headers=headers)
m_list = json.loads(list_resp.text[:])['data']['lists']
name_list = []
for i, s in enumerate(m_list):
name_list.append(f'{i + 1}------{s.get("FileName")}')
Download_window = UI.Window()
Download_window.geometry('300x300')
Download_window.TitleText.config(text="搜素结果")
Download_window=Download_window.Widget
MusicN_List = tk.Listbox(Download_window, width = 40, height = 37)
MusicN_List.pack()
for j in name_list:
MusicN_List.insert("end", j)
num = askstring('输入序列号', '请输入要下载的音乐号(输入格式为 1 2').split()
for s in num:
# 此处的 header1 可有可无 但是不带 cookie 的 headers 请求时 可能会出现问题
headers1 = {
'UserAgent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0',
'Cookie': 'kg_mid=536e52feec7d1c95137aa1d341f8144e; kg_dfid=3exupN3BZJMQ3ftR4x0KOXDk; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; kg_mid_temp=536e52feec7d1c95137aa1d341f8144e'}
# 构造你请求资源的URL
info_url = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash={m_list[int(s) - 1].get("FileHash")}&album_audio_id={m_list[int(s) - 1].get("ID")}'
# 进行访问
info_resp = requests.get(info_url, headers=headers1)
# 拿取你所请求资源的数据(这里是得到播放音乐的地址)
get_music_url = info_resp.json()['data']['play_url']
# print(f"音乐的播放地址为:'\n'{get_music_url}")
# 发送请求到服务器,获取音乐资源
m_resp = requests.get(get_music_url, headers=headers1)
# 服务器回应的数据 --保存数据
name = m_list[int(s) - 1].get('FileName') + '.mp3'
# 打开一个文件 写入二进制数据 并输出提示信息
with open(name, 'wb') as f: # wb 写进二进制
f.write(m_resp.content)
if s in num:
showinfo('成功', f"第{s}首已经OK啦!")
f.close()
try:
move(name, 'Music_Download')
except Error:
pass
if s == num[-1]:
showinfo('成功', f"\n您挑选的{len(num)}首音乐,已经全部下载完成!")
def open_MMC_Paly_File():
play_window = UI.Window()
play_window.TitleText.config(text="播放")
play_window.configure(background='gray')
play_window.geometry("500x150")
play_window.resizable(width=False, height=False)
play_window=play_window.Widget
music_name = listdir('Music_Download')
try:
selected_music = StringVar(value=music_name[0])
except:
showerror('错误', '没有找到')
else:
music_dropdown = ttk.Combobox(play_window, textvariable=selected_music, values=music_name,state='readonly', width = 60)
music_dropdown.place(x=20,y=20)
def play_m():
mixer.init()
mixer.music.set_volume(0.1)
music_name_in_def = music_dropdown.get()
mixer.music.load('Music_Download/' + music_name_in_def)
mixer.music.play(2)
while mixer.music.get_busy():
pass
def pause_m():
mixer.init()
mixer.music.pause()
def start_pause():
mixer.init()
global fadeout_TF
mixer.music.unpause()
#创建多线程,以防应用未响应
def start_tread_1():
thread = threading.Thread(target = pause_m, args = ())
thread.setDaemon(True)
thread.start()
def start_tread_2():
thread = threading.Thread(target = play_m, args = ())
thread.setDaemon(True)
thread.start()
def start_tread_3():
thread = threading.Thread(target = start_pause, args = ())
thread.setDaemon(True)
thread.start()
def val_set():
mixer.init()
val = askfloat('音量设置', '请输入音量大小(0-1之间的小数)')
if val > 1 or val < 0:
showerror('错误', '请输入一个小于1或大于0的数')
else:
mixer.music.set_volume(val)
play_button = Button(play_window, text="开始播放", command=start_tread_2,overrelief='sunken',bd=4,cursor='hand2')
play_button.place(x=50, y=60)
pause_button = Button(play_window, text="暂停", command=start_tread_1,overrelief='sunken',bd=4,cursor='hand2')
pause_button.place(x=400, y=60)
unpause_button = Button(play_window, text="继续播放", command=start_tread_3,overrelief='sunken',bd=4,cursor='hand2')
unpause_button.place(x=200, y=60)
val_button = Button(play_window, text = "声音设置", command = val_set, overrelief='sunken',bd=4,cursor='hand2')
val_button.place(x = 300, y = 60)
play_window.mainloop()
def info():
showinfo('关于', '网络歌曲来源于酷狗音乐,仅供学习交流使用,请勿用于商业用途\n主要开发肝肝甘甘的野生电脑\nGUI修改以及美化一只悠然自得\n版本MyOS Special Edition V1(β2)')
#初始化
init()
#创建窗口对象
main_window = UI.Window()
main_window.resizable(False, False)
#窗口大小
main_window.geometry('202x50')
#窗口标题
main_window.TitleText.config(text="MMC")
#--菜单栏
#搜索网络歌曲选项
into_search = Button(main_window.Widget, text = '搜索网络歌曲', command = search)
into_search.grid(row = 0, column = 0)
#播放歌曲选项
into_play = Button(main_window.Widget, text = '播放本地歌曲', command = open_MMC_Paly_File)
into_play.grid(row = 0, column = 1)
#关于选项
into_play = Button(main_window.Widget, text = '关于', command = info)
into_play.grid(row = 0, column = 2)
#定义背景颜色
main_window.Widget.configure(background='gray')
#显示窗口
main_window.Widget.mainloop()