首页 > 其他分享 >初试tkinter

初试tkinter

时间:2023-01-31 21:24:23浏览次数:53  
标签:初试 tkinter get url text data headers root

才学了tkinter,感觉好炫酷的样子。但做的有点简陋,还有很多需要改进的地方。大家一起学习,一起进步呀!

from tkinter import *
from prettytable import PrettyTable
import os
import re
import requests
import json
import time
import hashlib
from urllib.parse import quote

root = Tk()
root.title('开心听音乐')
root.geometry('720x520')

lb = Label(root, text='亲!务必选择下载接口哦!', font=('楷体', 52), fg='red')
lb.place(relx=0.3, rely=0.1, relheight=0.2, relwidth=0.2)
lb.pack()

title = Label(root, text='请输入歌名或歌手:', font=('楷体', 12), fg='red')
title.place(relx=0.29, rely=0.22, relheight=0.055, relwidth=0.1)
txt = Text(root)
txt.place(relx=0.25, rely=0.3, relheight=0.4, relwidth=0.5)
txt2 = Text(root)
txt2.place(relx=0.25, rely=0.8, relheight=0.07, relwidth=0.5)
which = Label(root, text='要下载哪一首歌:', font=('楷体', 12), fg='red')
which.place(relx=0.29, rely=0.725, relheight=0.042, relwidth=0.1)
downs = Entry(root)
downs.place(relx=0.4, rely=0.725, relheight=0.04, relwidth=0.05)
inp = Entry(root)
inp.place(relx=0.4, rely=0.22, relheight=0.05, relwidth=0.2)

def search():
    dic = { 1: '酷我音乐', 2: '酷狗音乐'}
    data = dic.get(var.get())
    if data == '酷我音乐':
        name = inp.get()
        namecode = quote(name)
        url = f'https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={namecode}&pn=1&rn=30&httpsStatus=1'
        referer = f'https://kuwo.cn/search/list?key={namecode}'
        headers = {'csrf': 'FIAFFQIJH9G',
                   'Cookie': '_ga=GA1.2.1423499245.1668256225; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668256135,1669972665,1669985480,1670163002; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1670163017; kw_token=FIAFFQIJH9G',
                   'Host': 'kuwo.cn',
                   'Referer': f'{referer}',
                   'user-agent': 'Mozilla/5.0 (Windows NTme/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'}
        respose = requests.get(url=url, headers=headers).text
        dic2 = json.loads(respose)['data']['list']
        table = PrettyTable()
        for s, li in enumerate(dic2):
            singer = li['artist']
            name_ = li['name']
            nnn = re.sub(r'[&,;$]', '', name_)
            sss = re.sub(r'[&,;]', '', singer)
            table.field_names = ['序号', '歌名', '歌手']
            table.add_row([s + 1, nnn.replace('nbsp', ' '), sss.replace('nbsp', ' ')])
        txt.insert(END, '{}'.format(table))
    elif data == '酷狗音乐':
        name = inp.get()
        text = [
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
            "appid=1014",
            "bitrate=0",
            "callback=callback123",
            "clienttime={}".format(int(time.time() * 1000)),
            "clientver=1000",
            "dfid=4XSnWz14ZQos2PYFIl2MiDLH",
            "filter=10",
            "inputtype=0",
            "iscorrection=1",
            "isfuzzy=0",
            "keyword={}".format(name),
            "mid=8a6709b0f4f0674f12dabeb3a710313a",
            "page=1",
            "pagesize=30",
            "platform=WebFilter",
            "privilege_filter=0",
            "srcappid=2919",
            "token=",
            "userid=0",
            "uuid=8a6709b0f4f0674f12dabeb3a710313a",
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        ]
        data = "".join(text)  # 变成字符串
        md5 = hashlib.md5(data.encode(encoding='utf-8')).hexdigest()  # md5加密
        url = "https://complexsearch.kugou.com/v2/search/song"
        headers = {
            "referer": "https://www.kugou.com/",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
        }
        params = {
            "callback": "callback123",
            "srcappid": "2919",
            "clientver": "1000",
            "clienttime": int(time.time() * 1000),
            "mid": "8a6709b0f4f0674f12dabeb3a710313a",
            "uuid": "8a6709b0f4f0674f12dabeb3a710313a",
            "dfid": "4XSnWz14ZQos2PYFIl2MiDLH",
            "keyword": f"{name}",
            "page": "1",
            "pagesize": "30",
            "bitrate": "0",
            "isfuzzy": "0",
            "inputtype": "0",
            "platform": "WebFilter",
            "userid": "0",
            "iscorrection": "1",
            "privilege_filter": "0",
            "filter": "10",
            "token": "",
            "appid": "1014",
            "signature": md5
        }
        lll = json.loads(requests.get(url=url, headers=headers, params=params).text[12:-2])
        kkk = lll['data']['lists']
        table = PrettyTable()
        for s, li in enumerate(kkk):
            table.field_names = ['序号', '歌名', '歌手']
            AlbumName = li['SongName']
            singername = li['SingerName']
            table.add_row([s + 1, AlbumName, singername])
        txt.insert(END, '{}'.format(table))
    else:
        txt.insert(END, '{}'.format('meiy'))


def last():
    dic = {1: '酷我音乐', 2: '酷狗音乐'}
    data = dic.get(var.get())
    if data == '酷我音乐':
        name = inp.get()
        namecode = quote(name)
        url = f'https://kuwo.cn/api/www/search/searchMusicBykeyWord?key={namecode}&pn=1&rn=30&httpsStatus=1'
        referer = f'https://kuwo.cn/search/list?key={namecode}'
        headers = {'csrf': 'FIAFFQIJH9G',
                   'Cookie': '_ga=GA1.2.1423499245.1668256225; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1668256135,1669972665,1669985480,1670163002; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1670163017; kw_token=FIAFFQIJH9G',
                   'Host': 'kuwo.cn',
                   'Referer': f'{referer}',
                   'user-agent': 'Mozilla/5.0 (Windows NTme/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'}
        respose = requests.get(url=url, headers=headers).text
        dic2 = json.loads(respose)['data']['list']
        num = downs.get()
        kkop = dic2[int(num) - 1]['name']
        kkkop = re.sub(r'[&,;$]', ' ', kkop)
        kkkopp = kkkop.replace('nbsp', '')
        opp = dic2[int(num) - 1]['rid']
        urls = f'https://kuwo.cn/api/v1/www/music/playUrl?mid={opp}&type=mp3&httpsStatus=1&reqId=42ce60a1-743a-11ed-b07e-23cc131feebb&br=2000'
        music_ = requests.get(url=urls, headers=headers).text
        kkk = json.loads(music_)['data']['url']  # MP3下载地址
        date_ = requests.get(url=kkk).content
        path_ = 'D:/音乐/'
        if not os.path.exists(path_):
            os.makedirs(path_)
        with open(path_ + f'{kkkopp}.mp3', 'wb') as f:
            f.write(date_)
            txt2.insert(END, '{}下载成功啦下载成功啦!'.format(kkkopp))
    elif data == '酷狗音乐':
        name = inp.get()
        num = downs.get()
        text = [
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt",
            "appid=1014",
            "bitrate=0",
            "callback=callback123",
            "clienttime={}".format(int(time.time() * 1000)),
            "clientver=1000",
            "dfid=4XSnWz14ZQos2PYFIl2MiDLH",
            "filter=10",
            "inputtype=0",
            "iscorrection=1",
            "isfuzzy=0",
            "keyword={}".format(name),
            "mid=8a6709b0f4f0674f12dabeb3a710313a",
            "page=1",
            "pagesize=30",
            "platform=WebFilter",
            "privilege_filter=0",
            "srcappid=2919",
            "token=",
            "userid=0",
            "uuid=8a6709b0f4f0674f12dabeb3a710313a",
            "NVPh5oo715z5DIWAeQlhMDsWXXQV4hwt"
        ]
        data = "".join(text)  # 变成字符串
        md5 = hashlib.md5(data.encode(encoding='utf-8')).hexdigest()  # md5加密
        url = "https://complexsearch.kugou.com/v2/search/song"
        headers = {
            "referer": "https://www.kugou.com/",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
        }
        params = {
            "callback": "callback123",
            "srcappid": "2919",
            "clientver": "1000",
            "clienttime": int(time.time() * 1000),
            "mid": "8a6709b0f4f0674f12dabeb3a710313a",
            "uuid": "8a6709b0f4f0674f12dabeb3a710313a",
            "dfid": "4XSnWz14ZQos2PYFIl2MiDLH",
            "keyword": f"{name}",
            "page": "1",
            "pagesize": "30",
            "bitrate": "0",
            "isfuzzy": "0",
            "inputtype": "0",
            "platform": "WebFilter",
            "userid": "0",
            "iscorrection": "1",
            "privilege_filter": "0",
            "filter": "10",
            "token": "",
            "appid": "1014",
            "signature": md5
        }

        lll = json.loads(requests.get(url=url, headers=headers, params=params).text[12:-2])
        kkk = lll['data']['lists']
        ID = lll['data']['lists'][int(num) - 1]['EMixSongID']
        name = lll['data']['lists'][int(num) - 1]['SongName']
        urls = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback=jQuery191015294419033165485_1674051666168&dfid=4XSnWz14ZQos2PYFIl2MiDLH&appid=1014&mid=8a6709b0f4f0674f12dabeb3a710313a&platid=4&encode_album_audio_id={ID}&_=1674051666169'
        params = {
            "r": "play/getdata",
            "callback": "jQuery19101351666471912658_1674051302167",
            "dfid": "4XSnWz14ZQos2PYFIl2MiDLH",
            "appid": "1014",
            "mid": "8a6709b0f4f0674f12dabeb3a710313a",
            "platid": "4",
            "encode_album_audio_id": f"{ID}",
            "_": "1674051302168"
        }
        respons = json.loads(requests.get(url=urls, headers=headers, params=params).text[41:-2])
        last = respons['data']['play_url']
        downlode = requests.get(url=last, headers=headers).content
        with open('D:/音乐/' + f'{name}.mp3', 'wb') as sp:
            sp.write(downlode)
            txt2.insert(END, '{}下载成功啦下载成功啦!'.format(name))
    else:
        pass


def quits():
    root.quit()


var = IntVar()
bt2 = Radiobutton(root, text='酷我音乐', variable=var, value=1)
bt2.place(relx=0.3, rely=0.1, relheight=0.1, relwidth=0.2)
bt3 = Radiobutton(root, text='酷狗音乐', variable=var, value=2)
bt3.place(relx=0.5, rely=0.1, relheight=0.1, relwidth=0.2)
btn4 = Button(root, text='搜索', fg='red', command=search)
btn4.place(relx=0.63, rely=0.22, relheight=0.05, relwidth=0.1)
btn5 = Button(root, text='下载', fg='red', command=last)
btn5.place(relx=0.48, rely=0.728, relheight=0.038, relwidth=0.1)
btn6 = Button(root, text='退出', fg='red', command=quits)
btn6.place(relx=0.6, rely=0.728, relheight=0.038, relwidth=0.1)
root.state('zoomed')
root.mainloop()

 

标签:初试,tkinter,get,url,text,data,headers,root
From: https://www.cnblogs.com/wxd501/p/17080790.html

相关文章

  • Python 内置界面开发框架 Tkinter入门篇 乙
    *以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s/2GFLTstDC7w6u3fTJxflNA本文大概 1685 个字,阅读需花 6 分钟......
  • SQLite 初试
    今天需要用到 SQLite统计数据,简单总结一下,SQLite是一个轻量级数据库,语法与mysql相似,不需要安装,解压即可使用。 下载地址:​​https://www.sqlite.org/download.html​......
  • Python 内置界面开发框架 Tkinter入门篇
    本文大概4158个字,阅读需花10分钟内容不多,但也花了一些精力如要交流,欢迎关注我然后评论区留言谢谢你的点赞收藏分享首先,今天先给大家拜个好年!新年快乐,恭喜发财!为了......
  • Python tkinter 示例
    运行结果如下:    codefromtkinterimport*fromtkinterimportmessageboxasmbimportjsonclassQuiz:def__init__(self):self.qno=0......
  • 迷宫机器人最短路径使用tkinter绘制
    起因我想要写一个玩家和机器对战的迷宫游戏。这个项目我没有写完,我实现了最短机器人路径并绘制在tkinter上,以及玩家移动的功能。更多的关于GUI的设计太花时间了我没有写完......
  • Tkinter模块的使用【转】
    1、Tkinter是什么Tkinter是使用python进行窗口视窗设计的模块。Tkinter模块(“Tk接口”)是Python的标准TkGUI工具包的接口。作为python特定的GUI界面,是一个图像的......
  • Python Tkinter教程
    1控件Tkinter模块提供了2种Toplevel控件和许多基本控件,目前已包括15种,每种控件都有很多属性  简单示例:1fromtkinterimport*#导入tkinter模块【必要步骤】2......
  • SRS视频流服务器初试
    目录1.关于协议理解2.快速开始1.SRS安装2.推流3.拉流注:1)方式一:SRS网页播放器(推荐)2)方式二:VLC播放器(需要下载,延迟高,不推荐)1.关于协议理解在前面的博客中,我简单的介绍了关于......
  • 初试高云FPGA
    前言之前一直眼馋Sipeed的Tang系列,正好遇到有工程需要高速控制并行总线,就买了NANO9K和Primer20K试试水买回来先拆的贵的20k,结果发现Sipeed设计师有奇怪的脑回路:核心......
  • Python Tkinter界面应用开发-黄棒清-专题视频课程
    PythonTkinter界面应用开发—1773人已学习课程介绍        如果你想在Python中构建图形用户界面应用程序,Tkinter是一个的选择.,TkInter应用程序几乎可以在任何桌面......