Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
5.6 kB
4
Indexable
Never
from PIL import ImageTk, Image
import re
import requests
import time
import urllib.parse
import urllib.request
from uuid import UUID
import coverpy
import vlc
from PIL import ImageTk, Image
from mttkinter import *
from twitchAPI import CustomRewardRedemptionStatus
from twitchAPI.pubsub import PubSub
from twitchAPI.twitch import Twitch, AuthScope

import config
import pafy

coverpy = coverpy.CoverPy()
limit = 1

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

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


def on_update():
    txt1['text'] = trackW
    photo2 = ImageTk.PhotoImage(Image.open("album.jpg"))
    img.configure(image=photo2)
    img.image = photo2


def callback_whisper(uuid: UUID, data: dict) -> None:
        print('Foi resgatado um reward ' + str(RedempPubsub))
        print(data['data']['redemption']['reward']['id'])
        print(data['data']['redemption']['reward']['title'])
        print(data['data']['redemption']['user_input'])

        redemid = data['data']['redemption']['reward']['id']
        redemprionid = data['data']['redemption']['id']
        redemtitle = data['data']['redemption']['reward']['title']
        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()

        try:
            result = coverpy.get_cover(trackstrip2, limit)
            resultadonome = result.name
            resultadoartista = result.artist

            print("Musica tocando = " + resultadonome + resultadoartista)

            txt3.configure(text=resultadonome + " - " + resultadoartista)

            img_data = requests.get(result.artwork(250)).content
            with open('album.jpg', 'wb') as handler:
                handler.write(img_data)

            filehtml = open("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:300%;' behavior='scroll' direction='left' scrollamount='10'><h1 "
                "style='font-size: 70px;'>" + resultadonome + "</h1><h2 class='mt-'3' style='font-size: 50px;'>" +
                resultadoartista + "</h2></marquee>\n</div>\n</div>\n</body>\n</html>")
            filehtml.close()
        except:
            print('Sem Album Art')

        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)
        h, m, s = video.duration.split(':')
        duration = int(h) * 3600 + int(m) * 60 + int(s)
        mili = duration * 1000
        videolink = video.getbestaudio()
        media = vlc.MediaPlayer(videolink.url)
        media.play()
        
        twitch.update_redemption_status(
            broadcaster_id="779823875",
            reward_id=redemid,
            redemption_ids=redemprionid,
            status=CustomRewardRedemptionStatus("FULFILLED"),
        )

        media.stop()

songW = open("CurrentSong.txt", "r", encoding='utf-8')
trackW = songW.read()

pubsub = PubSub(twitch)
pubsub.start()
RedempPubsub = pubsub.listen_channel_points('779823875', callback_whisper)

app.grid_columnconfigure(1, weight=11)
photo = ImageTk.PhotoImage(Image.open("album.jpg"))
img = mtTkinter.Label(app, image=photo)
img.grid(rowspan=4, column=0)

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

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

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

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

app.configure(background="#202020")
app.mainloop()