|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# coding=gbk
import time
import requests
import pygame
import tkinter as tk
from tkinter import messagebox
# 主程序
window=tk.Tk()
window.title('歌曲信息管理系统')
window.geometry('200x280')
var=tk.StringVar()
l=tk.Label(window,textvariable=var)
l.pack()
class System(object):
# 初始化数据
def __init__(self):
# name存储音乐名
self.name = []
# num存储歌手名
self.writer = []
# price存储发行时间
self.date = []
# 歌曲数量
self.kind = len(self.name)
try:
# 读取文件内容
with open("音乐数据.csv", mode='r') as file:
for i in file:
line = i.strip().split(',')
self.name.append(line[0])
self.writer.append((line[1]))
self.date.append(line[2])
# 创建默认内容
except FileNotFoundError:
self.name = ['够钟', '存在', '烟霞']
self.writer = ['周柏豪', '汪 峰 ', '容祖儿']
self.date = ['2009-07-10', '2011-10-17', '2014-07-29']
self.kind = len(self.name)
# stock负责显示功能
def button_stock():
print('音乐'.ljust(10), '歌手'.ljust(10), '发行时间'.ljust(10))
for i in range(0, self.kind):
print(str(self.name[i]).ljust(10), str(self.writer[i]).ljust(10), str(self.date[i]))
# find负责查找功能
def button_find():
look = input("请输入要查找的歌曲名:")
print("查找结果:")
print('音乐名'.ljust(8), '歌手 '.ljust(10), '发行时间'.ljust(10))
for i in range(0, self.kind):
if look in self.name[i]:
print(self.name[i].ljust(8), self.writer[i].ljust(10), self.date[i].ljust(10))
# add负责添加库存功能
def add():
exist = 0
while True:
name_new = input("请输入增加的歌曲名:")
if name_new == '':
print("歌曲名不能为空")
else:
break
try:
for i in range(0, self.kind):
if name_new == self.name[i]:
print("该歌曲已存在")
self.writer[i] = input("请输入更新后的作者:")
print("已更改")
exist = 1
if exist == 0:
self.name.append(name_new)
new_writer = input("请输入歌手:")
new_date = input("请输入发行时间:")
self.writer.append(new_writer)
self.date.append(new_date)
print("新歌曲添加完毕")
self.kind = len(self.name)
except ValueError:
print("请输入正确的数据")
return
# delete负责删除功能
def delete():
remove_nam = input("请输入要删除的歌曲名称:")
find = False
for i in range(0, self.kind):
if remove_nam == self.name[i]:
find = True
del self.name[i]
del self.writer[i]
del self.date[i]
print("删除成功")
self.kind = len(self.name)
if not find:
print("删除失败,未找到该歌曲")
def getMusic():
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
}
urls = [
"https://webfs.ali.kugou.com/202204132143/a61919df6b00cd4f6d4522e4a9d7af1c/KGTX/CLTX001/9b4b2af10f75da077b3fb4e74a22965d.mp3",
"https://webfs.ali.kugou.com/202204132141/f9d04bef735db3cb1c5b1edc8a7508c0/part/0/960131/KGTX/CLTX001/b5ac0f2b8941446c837d70df63a841d5.mp3",
"https://webfs.ali.kugou.com/202204132147/482a648edc466af050e349512715cd38/part/0/960050/G188/M08/04/15/XIcBAF5T1kCARIE8AE_G2s1cesE734.mp3"
]
name = ["断桥残雪", "千里之外", "光明"]
for i in range(3):
resp = requests.get(urls[i], headers=headers)
with open(f"{name[i]}.mp3", mode='wb') as file:
file.write(resp.content)
file.close()
print("爬取完成,音乐:《断桥残雪》,《千里之外》,《光明》")
# save负责保存数据
def save():
with open("歌曲数据.csv", mode='w') as file:
for i in range(0, self.kind):
file.write(f"{self.name[i]},{self.writer[i]},{self.date[i]}\n")
print("已写入至程序所在目录'歌曲数据.csv'")
# musicPlayer播放音乐,只能播放爬取到的音乐
def musicPlayer():
filepath = f'{input("请输入想播放的音乐:")}.mp3'
_time = int(input("请输入音乐播放时长:"))
pygame.mixer.init()
pygame.mixer.music.load(filepath)
pygame.mixer.music.play()
time.sleep(_time)
pygame.mixer.music.stop()
# play(filepath)
return
btn1=tk.Button(window,text='显示歌曲',command=button_stock)
btn2=tk.Button(window,text='查找歌曲',command=button_find)
btn3=tk.Button(window,text='增加歌曲',command=add)
btn4=tk.Button(window,text='删除歌曲',command=delete)
btn5=tk.Button(window,text='保存数据',command=save)
btn6=tk.Button(window,text='爬取歌曲',command=getMusic)
btn7=tk.Button(window,text='播放歌曲',command=musicPlayer)
btn8=tk.Button(window,text='退出系统')
btn1.pack()
btn2.pack()
btn3.pack()
btn4.pack()
btn5.pack()
btn6.pack()
btn7.pack()
btn8.pack()
window.mainloop()
|
|