Untitled
unknown
python
2 years ago
12 kB
3
Indexable
Never
import re import sys import PIL import json import time import threading from click import command, progressbar from numpy import imag import requests import urllib.parse import urllib.request from uuid import UUID import coverpy import vlc import pafy from PIL import Image, ImageTk from twitchAPI import CustomRewardRedemptionStatus from twitchAPI.pubsub import PubSub from twitchAPI.twitch import Twitch, AuthScope from tkinter import * import customtkinter import datetime from typing import List coverpy = coverpy.CoverPy() c = open('src/auth/auth.json') data = json.load(c) USERID = data['USERID'] TOKEN = data['TOKEN'] CLIENTID = data['CLIENTID'] CLIENTSECRET = data['CLIENTSECRET'] twitch = Twitch(CLIENTID, CLIENTSECRET, target_app_auth_scope=[AuthScope.USER_EDIT]) scope = [AuthScope.CHANNEL_READ_REDEMPTIONS] scope1 = [AuthScope.CHANNEL_MANAGE_REDEMPTIONS] twitch.set_user_authentication( TOKEN, scope + scope1, 'refresh_token') customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark customtkinter.set_default_color_theme("dark-blue") # Themes: blue (default), dark-blue, green app = customtkinter.CTk() app.title('SongRequestByRedeem by GG-TEC') app.geometry('400x500') app.resizable(True, False) iconload = PIL.Image.open("src/icon.ico") icon = ImageTk.PhotoImage(iconload) app.iconphoto(True , icon) height = 250 width = 250 photonone = ImageTk.PhotoImage(PIL.Image.open("src/AlbumDisc.png").resize((width, height)).convert("RGBA")) photoresult = ImageTk.PhotoImage(PIL.Image.open("src/album.jpg")) imagealbumvalue = True def pub(): global RedempPubsub, pubsub pubsub = PubSub(twitch) pubsub.start() filehtml = open("src/AlbumArt.html", "w") filehtml.write( "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<link rel='stylesheet'" "href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css'>\n" "<script src='https://code.jquery.com/jquery-3.6.0.slim.min.js'></script>\n<link " "rel='stylesheet' href='theme.css'></link>\n</head>\n<body>\n<div class='row " "align-items-center background d-flex flex-nowrap'>\n<div class='col-xs-6 ' >\n" "<img src='AlbumDisc.png' class='rounded-circle m-5 img-responsive img'" "/></div>\n<div class='col-xs-6 animador'>\n<marquee behavior='scroll' " "direction='left' scrollamount='10'>\n<h1>Sem Musica</h1>\n<h2 " "class='mt-'3'>""</h2>\n</marquee>\n</div>\n</div>\n</body>\n</html>") filehtml.close() RedempPubsub = pubsub.listen_channel_points(USERID, callback_whisper) def callback_whisper(uuid: UUID, data: dict) -> None: global trackstrip2, redemid, redemprionid, redemuserimput redemid = data['data']['redemption']['reward']['id'] cid = data['data']['redemption']['channel_id'] redemprionid = data['data']['redemption']['id'] redemuserimput = data['data']['redemption']['user_input'] twitch.update_redemption_status( broadcaster_id= cid, reward_id=redemid, redemption_ids=redemprionid, status=CustomRewardRedemptionStatus("FULFILLED"), ) trackstrip = redemuserimput.replace(redemuserimput[redemuserimput.find("("):redemuserimput.find(")") + 1], '').strip() trackstrip1 = trackstrip.replace(trackstrip[trackstrip.find("["):trackstrip.find("]") + 1], '').strip() trackstrip2 = trackstrip1.replace(trackstrip1[trackstrip1.find('"'):trackstrip1.find('"') + 1], '').strip() albumart(trackstrip2) requestsearch(redemuserimput) def albumart(trackstrip2): global artista,musica try: result = coverpy.get_cover(trackstrip2, 1) artista = result.artist musica = result.name albumart = result.artwork(250) txt3.configure(text=musica + " - " + artista) img_data = requests.get(albumart).content with open('src/album.jpg', 'wb') as handler: handler.write(img_data) photoresult = ImageTk.PhotoImage(PIL.Image.open("src/album.jpg")) img.configure(image=photoresult) img.image = photoresult imagealbumvalue = True filehtml = open("src/AlbumArt.html", "w") filehtml.write( "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<link rel='stylesheet'" "href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css'>\n" "<script src='https://code.jquery.com/jquery-3.6.0.slim.min.js'></script>\n<link " "rel='stylesheet' href='theme.css'></link>\n</head>\n<body>\n<div class='row " "align-items-center background d-flex flex-nowrap'>\n<div class='col-xs-6 ' >\n" "<img src='album.jpg' class='rounded-circle m-5 img-responsive img'" "/></div>\n<div class='col-xs-6 animador'>\n<marquee behavior='scroll' " "direction='left' scrollamount='10'>\n<h1>" + musica + "</h1>\n<h2 " "class='mt-'3'>" + artista + "</h2>\n</marquee>\n</div>\n</div>\n</body>\n</html>") filehtml.close() except: txt3.configure(text=trackstrip2) img.configure(image=photonone) img.image = photonone imagealbumvalue = False filehtml = open("src/AlbumArt.html", "w") filehtml.write( "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<link rel='stylesheet'" "href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css'>\n" "<script src='https://code.jquery.com/jquery-3.6.0.slim.min.js'></script>\n<link " "rel='stylesheet' href='theme.css'></link>\n</head>\n<body>\n<div class='row " "align-items-center background d-flex flex-nowrap'>\n<div class='col-xs-6 ' >\n" "<img src='AlbumDisc.png' class='rounded-circle m-5 img-responsive img'" "/></div>\n<div class='col-xs-6 animador'>\n<marquee behavior='scroll' " "direction='left' scrollamount='10'>\n<h1>" + str(trackstrip2) + "</h1>\n<h2 " "class='mt-'3'>""</h2>\n</marquee>\n</div>\n</div>\n</body>\n</html>") filehtml.close() def requestsearch(redemuserimput): query_string = urllib.parse.urlencode({"search_query": redemuserimput}) formaturl = urllib.request.urlopen("https://www.youtube.com/results?" + query_string) search_results = re.findall(r"watch\?v=(\S{11})", formaturl.read().decode()) clip2 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[0]) video = pafy.new(clip2) videolink = video.getbestaudio() url = videolink.url classerequest = RequestPlayer() classerequest.player_vlc(url) class RequestPlayer(threading.Thread): def __init__(self): super().__init__() self.vlc_instance= vlc.Instance() self.vlc_instance.log_unset() self.player: vlc.MediaPlayer = self.vlc_instance.media_player_new() def player_vlc(self,url): media: vlc.Media = self.vlc_instance.media_new(url) self.player.set_media(media) self.player.play() btnplay.configure(command=self.play) btnstop.configure(command=self.parar) time.sleep(2) valuel = self.player.get_length() timesound = datetime.datetime.fromtimestamp(valuel / 1000).strftime('%M:%S') txtl1.configure(text=str(timesound)) while True: playing = self.player.is_playing() if playing == 1: valuepos = self.player.get_position() valueatct = self.player.get_time() timeactual = datetime.datetime.fromtimestamp(valueatct/ 1000).strftime('%M:%S') txtl0.configure(text=timeactual) progress.set(valuepos) time.sleep(1) else: break def parar(self): self.player.stop() img.configure(image=imgstoreinit) txt3.configure(text="Sem Musica") txtl1.configure(text='00:00') txtl0.configure(text='00:00') filehtml = open("src/AlbumArt.html", "w") filehtml.write( "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<link rel='stylesheet'" "href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css'>\n" "<script src='https://code.jquery.com/jquery-3.6.0.slim.min.js'></script>\n<link " "rel='stylesheet' href='theme.css'></link>\n</head>\n<body>\n<div class='row " "align-items-center background d-flex flex-nowrap'>\n<div class='col-xs-6 ' >\n" "<img src='AlbumDisc.jpg' class='rounded-circle m-5 img-responsive img'" "/></div>\n<div class='col-xs-6 animador'>\n<marquee behavior='scroll' " "direction='left' scrollamount='10'>\n<h1>Sem Musica</h1>\n<h2 " "class='mt-'3'>""</h2>\n</marquee>\n</div>\n</div>\n</body>\n</html>") filehtml.close() def progressbarloop(self): valuepos = self.player.get_position() valueatct = self.player.get_time() timeactual = datetime.datetime.fromtimestamp(valueatct/ 1000).strftime('%M:%S') print(timeactual) txtl0.configure(text=timeactual) progress.set(valuepos) def play(self): playing = self.player.is_playing() if playing == 0: self.player.play() print('Play') btnplay.configure(image=pauseimginit) txt3.configure(text=musica + " - " + artista) if imagealbumvalue: print('Com Album Art') img.configure(image=photoresult) img.image = photoresult else: print('Sem Album Art') img.configure(image=photonone) img.image = photonone while True: playing = self.player.is_playing() if playing == 1: self.progressbarloop() time.sleep(1) else: break else: self.player.pause() time.sleep(2) print(playing) btnplay.configure(image=playimginit) txt3.configure(text='Pausado...') def close(): app.destroy() pubsub.stop() sys.exit() player_id = RequestPlayer().start() app.after(1000, pub()) height = 250 width = 250 imginit = PIL.Image.open("src/AlbumDisc.png").resize((width, height)).convert("RGBA") imgstoreinit = ImageTk.PhotoImage(imginit) img = customtkinter.CTkLabel(app, image=imgstoreinit) img.grid(row=0, column=1, pady=20,) app.grid_columnconfigure(0, weight=0) txt3 = customtkinter.CTkLabel(app, text="", text_font='20', width=100) txt3.grid(row=1, columnspan=3, padx=5, pady=20, sticky='W' + "E") playimg = PIL.Image.open("src/play.png").resize((32, 32)).convert("RGBA") playimginit = ImageTk.PhotoImage(playimg) pauseimg = PIL.Image.open("src/pause.png").resize((32, 32)).convert("RGBA") pauseimginit = ImageTk.PhotoImage(pauseimg) stopimg = PIL.Image.open("src/stop.png").resize((32, 32)).convert("RGBA") stopimginit = ImageTk.PhotoImage(stopimg) btnplay = customtkinter.CTkButton(app, text="",image=pauseimginit, text_font='20',width=45,height=45) btnplay.grid(row=2, column=1, sticky='E',padx=(5, 80),) btnstop = customtkinter.CTkButton(app, text="",image=stopimginit, text_font='20',width=45,height=45) btnstop.grid(row=2, column=1, sticky='W' ,padx=(80, 5)) progress = customtkinter.CTkProgressBar(app,width=350) progress.grid(row=4,columnspan=3) txtl0 = customtkinter.CTkLabel(app, text="00:00", text_font='20',width=60) txtl0.grid(row=3, column=0, pady=20, padx=5 , sticky='w') txtl1 = customtkinter.CTkLabel(app, text="00:00", text_font='20',width=60) txtl1.grid(row=3, column=2, pady=20, padx=5, sticky='e') app.protocol('WM_DELETE_WINDOW', close) app.mainloop()