Hi, im trying to set a radio and youtube playlist player

my code works with radio and single youtube link but with playlist it says that the link is unrecognized, can somoene help?

import os
from os import path
from random import randrange
from tkinter.ttk import Combobox
import pafy
import requests
import vlc
import time
from tkinter import *
from tkinter import messagebox,PhotoImage

from bs4 import BeautifulSoup

root = Tk()

global phplay,phpause,phstop
global pausevar
pausevar = ""
phplay = PhotoImage(file=r'img/play.png')
phpause = PhotoImage(file=r'img/pause.png')
phstop = PhotoImage(file=r'img/stop.png')


frmradio = LabelFrame(root, text="Radio Player", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
frmradio.grid(row=0, column=0, sticky=NW, pady=2)



def getradiolist():
    var1 = open("Confs/lstradios.txt", "r").readlines()
    data = []

    for line in var1:
        if len(line) > 1:
            estacao, url, = line.split('<=>')
            data.append(estacao)
    return data

valradio = StringVar()
imp_radio = Combobox(frmradio, textvariable=valradio, height=10, width=47)
imp_radio['values'] = getradiolist()
imp_radio['state'] = 'readonly'
imp_radio.grid(row=1, column=0,columnspan=5, pady=2, sticky="ew")

# define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
# Define VLC player
player = instance.media_player_new()
instance.log_unset()


def startplayer(pausevar):
    if pausevar == "sim":
        pausevar=""
        player.pause()


    player.stop()
    esta = imp_radio.get()
    if len(esta)>1:
        var1 = open("Confs/lstradios.txt", "r").readlines()
        for line in var1:
            if len(line) > 1:
                if esta in line:
                    estacao, url, = line.split('<=>')
                    break

        if "youtube" in estacao:
            print(url)
            playlist = pafy.get_playlist2(url.strip())
            items = playlist["items"]# getting playlist items

            def loop_play():
                item = items[randrange(len(items))]
                i_pafy = item['pafy']
                y_url = i_pafy.watchv_url
                video = pafy.new(y_url)
                best = video.getbest()

                media = instance.media_new(best.y_url.strip())
                player.set_media(media)
                frmradio.config(text=str(item["title"]))
                player.play()

            loop_play()
        else:
            media = instance.media_new(url.strip())
            player.set_media(media)
            player.play()


        frmradio.config(text=str("Radio Player : Playing: " + estacao))


def stopplauyer():
    player.stop()
    frmradio.config(text=str("Radio Player"))

def pauseplauyer():
    global pausevar
    pausevar = "sim"
    player.pause()
    frmradio.config(text=str("Radio Player : Pause!"))



Button(frmradio, width="150",height="28", text="Play", image=phplay, command=lambda pausevar=pausevar: startplayer(pausevar)).grid(row=0, column=0, sticky=N + S + E + W)
Button(frmradio, width="100",height="28", text="Pause", image=phpause, command=lambda: pauseplauyer()).grid(row=0, column=1, sticky=N + S + E + W)
Button(frmradio, width="100",height="28", text="Stop", image=phstop,command=lambda: stopplauyer()).grid(row=0, column=2, sticky=N + S + E + W)





if __name__ == '__main__':
    root.mainloop()

Listaradios.txt is

youtube <=> https://www.youtube.com/playlist?list=PLr5JVJSLVg79UpgS6gdrcINWt86npzXkz

Mega Hits <=> http://19553.live.streamtheworld.com:80/MEGA_HITS_SC

Im getting the error in playlist2
pafy.util.GdataError: Youtube Error 403: The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.

I have also tryied with playlist and playlist2

Recommended Answers

All 7 Replies

Code looks fine. But Google does have quotas and you find you can exceed those. Fix? I can only write about Google Maps. After we hit our quota we had to pay Google for more data.

fix the quota with youtube api key, now i get
TypeError: '<' not supported between instances of 'str' and 'int'
in line items = playlist["items"]# getting playlist items

The error is well discussed but I'd want to know which line number it occurred on to comment further.

it apears in line items = playlist["items"]# getting playlist items

commented: I don't work hard at this. Asking for line number from above so I don't have find it. +16
commented: So, line 71. Please say so in the future. +15

According to this post, the solution is to operate on playlist directly rather than extracting the "items" sub-dictionary.

commented: Excellent as always +3

i add them to a list but the list add them like this: [Pafy object: w0N4twV28Mw [Ini Kamoze - Here Comes The Hotstepper (HQ)..]] how do i just get the w0N4twV28Mw?

got it y_url = item.watchv_url

now i get AttributeError: 'YtdlStream' object has no attribute 'y_url' in line 80
player = vlc.MediaPlayer(best.y_url.strip())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.