Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python code for internet radio stream

import requests
import time
import datetime
print(datetime.datetime.now())
import re


url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'
encoding = 'latin1'
info = ''

radio_session = requests.Session()

while True:

    radio = radio_session.get(url, headers={'Icy-MetaData': '1'}, stream=True)

    metaint = int(radio.headers['icy-metaint'])

    stream = radio.raw

    audio_data = stream.read(metaint)
    meta_byte = stream.read(1)

    if (meta_byte):
        meta_length = ord(meta_byte) * 16

        meta_data = stream.read(meta_length).rstrip(b'')

        stream_title = re.search(br"StreamTitle='([^']*)';", meta_data)


        if stream_title:

            stream_title = stream_title.group(1).decode(encoding, errors='replace')

            if info != stream_title:
                print('Now playing: ', stream_title)
                info = stream_title
            else:
                pass

        else:
            print('No StreamTitle!')

    time.sleep(1)
Comment

python live radio

import socket
from io import BytesIO
from datetime import datetime

STREAM_URL = "us4.internet-radio.com"
STREAM_PATH = "/proxy/radioestiloleblon?mp=/stream"

BUFFER_SIZE = 1024
PORT = 443

def stream_reader(sock):
    sock.recv(BUFFER_SIZE) # http response
    while data := sock.recv(BUFFER_SIZE):
        print(f"[{datetime.now()}] Received data: {data[:10]}")
        yield data

def get_socket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((STREAM_URL, PORT))
    s.send(bytes(f"GET {STREAM_PATH}; HTTP/1.1
Host:{STREAM_URL}

", encoding="UTF-8"))
    return s

def write_buffer(filename, bytesio: BytesIO):
    buffer = bytesio.getbuffer()
    with open(filename, "wb") as file:
        file.write(buffer)
    print(f"[{datetime.now()}] Wrote buffer. Size: {buffer.nbytes} bytes")

def main():
    s = get_socket()
    with BytesIO() as bIO:
        try:
            for data in stream_reader(s):
                bIO.write(data)
        except KeyboardInterrupt: pass
        finally: write_buffer("stream.mp3", bytesio=bIO)

if __name__ == "__main__":
    main()
Comment

python code for internet radio stream

import vlc
import time

url = 'http://prem1.rockradio.com:80/bluesrock?9555ae7caa92404c73cade1d'

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

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()
Comment

python code for internet radio stream

>>> play.pause()  #pause play back
>>> player.play() #resume play back
>>> player.stop() #stop play back
Comment

PREVIOUS NEXT
Code Example
Python :: get n random numbers from x to y python 
Python :: python datetime last day of month 
Python :: playsound moudle python 
Python :: pyautogui pause in python 
Python :: python strftime utc offset 
Python :: split list in 3 part 
Python :: python list distinct 
Python :: check if number in range 
Python :: the month before python dateime 
Python :: openpyxl get last non empty row 
Python :: print random word python 
Python :: amazon cli on commadline 
Python :: how to print a float with only 2 digits after decimal in python 
Python :: how to subtract dates in python 
Python :: get all values of a dict python 
Python :: how to run python code on github 
Python :: python: select specific columns in a data frame 
Python :: pandas to pickle 
Python :: pandas load dataframe without header 
Python :: how to record the steps of mouse and play the steps using python 
Python :: find the determinant of a matrix in python 
Python :: write file with python 
Python :: default argument in flask route 
Python :: binomial coefficient python 
Python :: selenium webdriver python 
Python :: opencv skip video frames 
Python :: how to set background color of an image to transparent in pygame 
Python :: iqr in python 
Python :: how to remove first letter of a string python 
Python :: pyplot bar plot colur each bar custom 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =