Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- from tkinter import scrolledtext
- import vlc
- import requests
- import sys
- import datetime
- import os
- import re
- class RadioPlayerApp(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("Najeeb Radio Player")
- self.geometry("580x310")
- self.configure(bg="#336699")
- self.config = {
- "LATA": "https://stream.zeno.fm/87xam8pf7tzuv",
- "KK": "https://stream-29.zeno.fm/0ghtfp8ztm0uv?zs=bY_YMkyZTfaDxM_0V6n4NQ",
- "Rafi": "https://stream-22.zeno.fm/2xx62x8ztm0uv?zs=H0kYgOvASsybRex146D3FA",
- "Bolly": "https://stream-14.zeno.fm/a2gyqzwpwfeuv?zs=FZPPkfUPT1-FLq1lNTnc2A",
- "MIRCHI": "https://onlineradiofm.in/stations/mirchi",
- "Pak1": "https://samaapew107-itelservices.radioca.st/stream/1/",
- "Pak2": "http://cc.vmakerhost.com:8147/steam/1/",
- "Pak3": "https://s3.radio.co/s3b10a57ef/listen",
- "PakF": "http://162.244.80.118:3058/;stream.mp3",
- "FM": "http://74.91.125.187:8214/stream",
- "News": "https://jk3lz82elw79-hls-live.5centscdn.com/GEONEWS/3500ba09d0538297440ca620c9dd46bf.sdp/GEONEWS/Backup1/chunks.m3u8"
- # Add More Stations Here
- }
- self.create_widgets()
- def create_widgets(self):
- self.station_label = tk.Label(self, text="Station Name:")
- self.station_label.grid(row=0, column=0)
- self.station_entry = tk.Entry(self, width=60)
- self.station_entry.grid(row=0, column=1)
- self.volume_label = tk.Label(self, text="Speakers Volu:")
- self.volume_label.grid(row=1, column=0)
- self.volume_entry = tk.Entry(self, width=60)
- self.volume_entry.grid(row=1, column=1)
- self.volume_label = tk.Label(self, text="NAJEEB PAKISTANI AND INDIAN RADIO STATIONS AND RECORDER")
- self.volume_label.grid(row=2, column=1)
- self.play_button = tk.Button(self, text="Play Radio", command=self.play_station)
- self.play_button.grid(row=3, column=0, sticky="ew", padx=(2, 5), pady=5)
- self.show_stations_button = tk.Button(self, text="Show Stations", command=self.show_stations)
- self.show_stations_button.grid(row=3, column=1, sticky="ew", padx=5, pady=5)
- self.download_button = tk.Button(self, text="Record MP3", command=self.download_station)
- self.download_button.grid(row=3, column=2, sticky="ew", padx=(5, 0), pady=5)
- self.stations_text = scrolledtext.ScrolledText(self, wrap=tk.WORD, width=70, height=12)
- self.stations_text.grid(row=4, column=0, columnspan=3)
- def play_station(self):
- station_name = self.station_entry.get()
- volume = int(self.volume_entry.get())
- station_url = self.config.get(station_name)
- if not station_url:
- messagebox.showerror("Error", "Station not found in local database.")
- return
- instance = vlc.Instance("--quiet")
- player = instance.media_player_new()
- media = instance.media_new(station_url)
- player.set_media(media)
- player.audio_set_volume(volume)
- player.play()
- def show_stations(self):
- station_titles = list(self.config.keys())
- stations_text = "\n".join(station_titles)
- self.stations_text.delete('1.0', tk.END) # Clear the text area
- self.stations_text.insert(tk.END, stations_text)
- def download_station(self):
- station_name = self.station_entry.get()
- station_url = self.config.get(station_name)
- if not station_url:
- messagebox.showerror("Error", "Station not found in local database.")
- return
- self._download_station(station_name, station_url, disable_splitting=False)
- def _download_station(self, station_name, station_url, disable_splitting):
- headers = {"icy-metadata": "1"}
- start_time = datetime.datetime.now()
- newTitle = ""
- oldTitle = ""
- with requests.get(station_url, stream=True, headers=headers) as r:
- try:
- music_size = int(r.headers["icy-metaint"])
- except KeyError:
- print("Error!")
- print("Pyradio can not download this station!")
- sys.exit()
- if station_name.lower().startswith("http"):
- print("\nNow downloading: " + station_name)
- else:
- print("\nNow downloading: " + station_name + " from " + station_url)
- print("-> Ctrl+C to exit!", end="")
- station_name = self._process_foldername(station_name)
- if not os.path.exists(station_name):
- os.mkdir(station_name)
- os.chdir(station_name)
- textfile = open("titles.txt", "w")
- if disable_splitting:
- musicfile = open("Unnamed Stream.mp3", "wb")
- r.raw.read(music_size)
- try:
- while True:
- metadata_size = int.from_bytes(r.raw.read(1), byteorder="big") * 16
- metadata = r.raw.read(metadata_size)
- musicdata = r.raw.read(music_size)
- if metadata:
- try:
- newTitle = re.match("StreamTitle='([^;]*)';", metadata.decode("utf-8"))[1]
- except TypeError:
- pass
- if newTitle == "":
- newTitle = "Unnamed Stream"
- if newTitle != oldTitle:
- oldTitle = newTitle
- if disable_splitting:
- display_name = newTitle
- else:
- display_name = self._process_filename(newTitle)
- musicfile = open(display_name, "wb")
- info = (
- datetime.datetime.now().strftime("%H:%M:%S") +
- " (" + str(datetime.datetime.now() - start_time).split('.', 2)[0] + "): " +
- display_name + "\n")
- textfile.write(info)
- textfile.flush()
- musicfile.write(musicdata)
- musicfile.flush()
- except KeyboardInterrupt:
- print("")
- sys.exit()
- def _process_filename(self, filename):
- filename = self._illegal_symbol_filter(filename)
- if os.path.exists(filename + ".mp3"):
- cycles = 2
- while os.path.exists(filename + " (" + str(cycles) + ").mp3"):
- cycles += 1
- filename += " (" + str(cycles) + ")"
- return filename + ".mp3"
- def _process_foldername(self, foldername):
- foldername = self._illegal_symbol_filter(foldername)
- if os.path.exists(foldername):
- cycles = 2
- while os.path.exists(foldername + " (" + str(cycles) + ")"):
- cycles += 1
- foldername += " (" + str(cycles) + ")"
- return foldername
- def _illegal_symbol_filter(self, filter_string):
- filter_set = ["/", "\\", "<", ">", "|", ":", "*", "?", "\"", "\n"]
- if sys.platform != "win32":
- filter_set = ["/"]
- for to_replace in filter_set:
- if to_replace in filter_string:
- filter_string = filter_string.replace(to_replace, "")
- return filter_string
- def main():
- app = RadioPlayerApp()
- app.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement