Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
7.6 kB
2
Indexable
Never
## SoundRequestByRedeem
from ast import If
import re
import os
from tracemalloc import stop
from typing_extensions import Self
import PIL
import time
import requests
import urllib.parse
import urllib.request
from uuid import UUID
import coverpy
import vlc
from PIL import ImageTk, Image
from twitchAPI import CustomRewardRedemptionStatus
from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch, AuthScope
from tkinter import *
import tkinter as tk
import config
import pafy

coverpy = coverpy.CoverPy()
limit = 1

twitch = Twitch(config.CLIENTID,
                config.CLIENTSECRET,
                target_app_auth_scope=[AuthScope.USER_EDIT])

scope = [AuthScope.CHANNEL_READ_REDEMPTIONS]
scope1 = [AuthScope.CHANNEL_MANAGE_REDEMPTIONS]

twitch.set_user_authentication(
    config.PASS,
    scope + scope1,
    'refresh_token')


app = tk.Tk()
app.title('Album Art')
app.geometry("700x255")
app.resizable(True, False)

def pub():
    global RedempPubsub
    pubsub = PubSub(twitch)
    pubsub.start()
    RedempPubsub = pubsub.listen_channel_points('779823875', callback_whisper)

def callback_whisper(uuid: UUID, data: dict) -> None:

        global media, trackstrip2, redemid, redemprionid, redemuserimput
        
        redemid = data['data']['redemption']['reward']['id']
        redemprionid = data['data']['redemption']['id']
        redemuserimput = data['data']['redemption']['user_input']

        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()

        print(trackstrip2 + ' Recebeu pedido')
        albumart(trackstrip2)


def albumart(trackstrip2):

    try:
        result = coverpy.get_cover(trackstrip2, limit)
        artista = result.artist
        musica = result.name
        
        albumart = result.artwork(250)
        
        txt3.configure(text=musica + " - " + artista)

        img_data = requests.get(albumart).content
        with open('Source/SRBR WORKING DIRECTORY/src/album.jpg', 'wb') as handler:
            handler.write(img_data)

        time.sleep(3)

        photo = ImageTk.PhotoImage(PIL.Image.open("Source/SRBR WORKING DIRECTORY/src/album.jpg"))
        img = tk.Label(app, image=photo)
        img.configure(image=photo)



        filehtml = open("Source/SRBR WORKING DIRECTORY/src/AlbumArt.html", "w")
        filehtml.write(
            "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<title>ALBUM ART</title>\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</head>\n<body>\n<div "
            "class='row align-items-center' id='scroll-container' style='width: 100%;background:rgba(0, 0, 0, "
            "0.714); height:auto;'>\n<div class='col-xs-6 ' >\n<img src='album.jpg' class='rounded-circle m-5 "
            "img-responsive' style='height:200px; margin-left:-15px;' />\n</div>\n<div class='col-xs-6'>\n<marquee "
            "style='color: white; width:auto;' behavior='scroll' direction='left' scrollamount='10'><h1 "
            "style='font-size: 70px;'>" + musica + "</h1><h2 class='mt-'3' style='font-size: 50px;'>" +
            artista + "</h2></marquee>\n</div>\n</div>\n</body>\n</html>")

        filehtml.close()

        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
        duration = video.length
        RequestPlayer(url, duration)

        return url, duration

    except:

        print('Sem Album Art')

        txt3.configure(text=trackstrip2)
        photo2 = ImageTk.PhotoImage(PIL.Image.open("Source/SRBR WORKING DIRECTORY/src//AlbumDisc.jpg"))
        img = tk.Label(app, image=photo2)
        img.configure(image=photo2)

        filehtml = open("Source/SRBR WORKING DIRECTORY/src/AlbumArt.html", "w")
        filehtml.write(
            "<html>\n<head>\n<meta http-equiv='refresh' content='30'>\n<title>ALBUM ART</title>\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</head>\n<body>\n<div "
            "class='row align-items-center' id='scroll-container' style='width: 100%;background:rgba(0, 0, 0, "
            "0.714); height:auto;'>\n<div class='col-xs-6 ' >\n<img src='AlbumDisc.jpg' class='rounded-circle m-5 "
            "img-responsive' style='height:200px; margin-left:-15px;' />\n</div>\n<div class='col-xs-6'>\n<marquee "
            "style='color: white; width:auto;' behavior='scroll' direction='left' scrollamount='10'><h1 "
            "style='font-size: 70px;'>" + str(trackstrip2) + "</h1><h2 class='mt-'3' style='font-size: 50px;'>""</h2></marquee>\n</div>\n</div>\n</body>\n</html>")

        filehtml.close()

        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
        duration = video.length

        return url, duration

class RequestPlayer():

    def __init__(self, url, duration):

        self.media_player = vlc.MediaPlayer(url) 
        self.media_player.play() 
        
        time.sleep(3)


        print('Duração')
        print(duration)

        while True:
            playing = self.media_player.is_playing()
            if playing == 1:
                time.sleep(duration)
                self.media_player.stop()
                break
        
        print('Saiu do loop')

link = albumart(0)
dur = albumart(1)

print(link)
print(dur)

def close ():
    app.destroy()
    

app.after(1000, pub())

app.grid_columnconfigure(1, weight=11)
photo = ImageTk.PhotoImage(PIL.Image.open("Source/SRBR WORKING DIRECTORY/src/album.jpg"))

img = tk.Label(app, image=photo)
img.grid(rowspan=4, column=0)

btn = tk.Button(app, text="Skip", bg="#4c4c4c", fg="#fff", font=('Seoge UI', 14))
btn.grid(row=2, column=1, padx=5, sticky='W' + "E")

btn2 = tk.Button(app, text="Quit", bg="#4c4c4c", fg="#fff", font=('Seoge UI', 14),command=close)
btn2.grid(row=3, column=1, padx=5, sticky='W' + "E")

txt2 = tk.Label(app, text="Musica", bg="#202020", fg="#fff", font=('Seoge UI', 10))
txt2.grid(row=0, column=1, padx=5, sticky='W' + "E")

txt3 = tk.Label(app, text="", bg="#4c4c4c", fg="#fff", font=('Seoge UI', 14))
txt3.grid(row=1, column=1, padx=5, sticky='W' + "E")


app.configure(background="#202020")

app.mainloop()