Advertisement
Najeebsk

RADIOS-PAK.py

Mar 21st, 2024
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.64 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. from tkinter import scrolledtext
  4. import vlc
  5. import requests
  6. import sys
  7. import datetime
  8. import os
  9. import re
  10.  
  11. class RadioPlayerApp(tk.Tk):
  12.     def __init__(self):
  13.         super().__init__()
  14.         self.title("Najeeb Radio Player")
  15.         self.geometry("580x310")
  16.         self.configure(bg="#336699")
  17.         self.config = {
  18.             "LATA": "https://stream.zeno.fm/87xam8pf7tzuv",
  19.             "KK": "https://stream-29.zeno.fm/0ghtfp8ztm0uv?zs=bY_YMkyZTfaDxM_0V6n4NQ",
  20.             "Rafi": "https://stream-22.zeno.fm/2xx62x8ztm0uv?zs=H0kYgOvASsybRex146D3FA",
  21.             "Bolly": "https://stream-14.zeno.fm/a2gyqzwpwfeuv?zs=FZPPkfUPT1-FLq1lNTnc2A",
  22.             "MIRCHI": "https://onlineradiofm.in/stations/mirchi",
  23.             "Pak1": "https://samaapew107-itelservices.radioca.st/stream/1/",
  24.             "Pak2": "http://cc.vmakerhost.com:8147/steam/1/",
  25.             "Pak3": "https://s3.radio.co/s3b10a57ef/listen",
  26.             "PakF": "http://162.244.80.118:3058/;stream.mp3",
  27.             "FM": "http://74.91.125.187:8214/stream",
  28.             "News": "https://jk3lz82elw79-hls-live.5centscdn.com/GEONEWS/3500ba09d0538297440ca620c9dd46bf.sdp/GEONEWS/Backup1/chunks.m3u8"
  29.             # Add More Stations Here
  30.         }
  31.         self.create_widgets()
  32.  
  33.     def create_widgets(self):
  34.         self.station_label = tk.Label(self, text="Station Name:")
  35.         self.station_label.grid(row=0, column=0)
  36.         self.station_entry = tk.Entry(self, width=60)
  37.         self.station_entry.grid(row=0, column=1)
  38.  
  39.         self.volume_label = tk.Label(self, text="Speakers Volu:")
  40.         self.volume_label.grid(row=1, column=0)
  41.         self.volume_entry = tk.Entry(self, width=60)
  42.         self.volume_entry.grid(row=1, column=1)
  43.  
  44.         self.volume_label = tk.Label(self, text="NAJEEB PAKISTANI AND INDIAN RADIO STATIONS AND RECORDER")
  45.         self.volume_label.grid(row=2, column=1)
  46.  
  47.         self.play_button = tk.Button(self, text="Play Radio", command=self.play_station)
  48.         self.play_button.grid(row=3, column=0, sticky="ew", padx=(2, 5), pady=5)
  49.  
  50.         self.show_stations_button = tk.Button(self, text="Show Stations", command=self.show_stations)
  51.         self.show_stations_button.grid(row=3, column=1, sticky="ew", padx=5, pady=5)
  52.  
  53.         self.download_button = tk.Button(self, text="Record MP3", command=self.download_station)
  54.         self.download_button.grid(row=3, column=2, sticky="ew", padx=(5, 0), pady=5)
  55.  
  56.         self.stations_text = scrolledtext.ScrolledText(self, wrap=tk.WORD, width=70, height=12)
  57.         self.stations_text.grid(row=4, column=0, columnspan=3)
  58.  
  59.  
  60.  
  61.     def play_station(self):
  62.         station_name = self.station_entry.get()
  63.         volume = int(self.volume_entry.get())
  64.         station_url = self.config.get(station_name)
  65.         if not station_url:
  66.             messagebox.showerror("Error", "Station not found in local database.")
  67.             return
  68.         instance = vlc.Instance("--quiet")
  69.         player = instance.media_player_new()
  70.         media = instance.media_new(station_url)
  71.         player.set_media(media)
  72.         player.audio_set_volume(volume)
  73.         player.play()
  74.  
  75.     def show_stations(self):
  76.         station_titles = list(self.config.keys())
  77.         stations_text = "\n".join(station_titles)
  78.         self.stations_text.delete('1.0', tk.END)  # Clear the text area
  79.         self.stations_text.insert(tk.END, stations_text)
  80.  
  81.     def download_station(self):
  82.         station_name = self.station_entry.get()
  83.         station_url = self.config.get(station_name)
  84.         if not station_url:
  85.             messagebox.showerror("Error", "Station not found in local database.")
  86.             return
  87.         self._download_station(station_name, station_url, disable_splitting=False)
  88.  
  89.     def _download_station(self, station_name, station_url, disable_splitting):
  90.         headers = {"icy-metadata": "1"}
  91.         start_time = datetime.datetime.now()
  92.         newTitle = ""
  93.         oldTitle = ""
  94.  
  95.         with requests.get(station_url, stream=True, headers=headers) as r:
  96.             try:
  97.                 music_size = int(r.headers["icy-metaint"])
  98.             except KeyError:
  99.                 print("Error!")
  100.                 print("Pyradio can not download this station!")
  101.                 sys.exit()
  102.  
  103.             if station_name.lower().startswith("http"):
  104.                 print("\nNow downloading: " + station_name)
  105.             else:
  106.                 print("\nNow downloading: " + station_name + " from " + station_url)
  107.  
  108.             print("-> Ctrl+C to exit!", end="")
  109.  
  110.             station_name = self._process_foldername(station_name)
  111.             if not os.path.exists(station_name):
  112.                 os.mkdir(station_name)
  113.             os.chdir(station_name)
  114.  
  115.             textfile = open("titles.txt", "w")
  116.  
  117.             if disable_splitting:
  118.                 musicfile = open("Unnamed Stream.mp3", "wb")
  119.  
  120.             r.raw.read(music_size)
  121.  
  122.             try:
  123.                 while True:
  124.                     metadata_size = int.from_bytes(r.raw.read(1), byteorder="big") * 16
  125.                     metadata = r.raw.read(metadata_size)
  126.                     musicdata = r.raw.read(music_size)
  127.  
  128.                     if metadata:
  129.                         try:
  130.                             newTitle = re.match("StreamTitle='([^;]*)';", metadata.decode("utf-8"))[1]
  131.                         except TypeError:
  132.                             pass
  133.  
  134.                         if newTitle == "":
  135.                             newTitle = "Unnamed Stream"
  136.  
  137.                         if newTitle != oldTitle:
  138.                             oldTitle = newTitle
  139.  
  140.                             if disable_splitting:
  141.                                 display_name = newTitle
  142.                             else:
  143.                                 display_name = self._process_filename(newTitle)
  144.                                 musicfile = open(display_name, "wb")
  145.  
  146.                             info = (
  147.                                     datetime.datetime.now().strftime("%H:%M:%S") +
  148.                                     " (" + str(datetime.datetime.now() - start_time).split('.', 2)[0] + "): " +
  149.                                     display_name + "\n")
  150.  
  151.                             textfile.write(info)
  152.                             textfile.flush()
  153.  
  154.                     musicfile.write(musicdata)
  155.                     musicfile.flush()
  156.  
  157.             except KeyboardInterrupt:
  158.                 print("")
  159.                 sys.exit()
  160.  
  161.     def _process_filename(self, filename):
  162.         filename = self._illegal_symbol_filter(filename)
  163.         if os.path.exists(filename + ".mp3"):
  164.             cycles = 2
  165.             while os.path.exists(filename + " (" + str(cycles) + ").mp3"):
  166.                 cycles += 1
  167.             filename += " (" + str(cycles) + ")"
  168.         return filename + ".mp3"
  169.  
  170.     def _process_foldername(self, foldername):
  171.         foldername = self._illegal_symbol_filter(foldername)
  172.         if os.path.exists(foldername):
  173.             cycles = 2
  174.             while os.path.exists(foldername + " (" + str(cycles) + ")"):
  175.                 cycles += 1
  176.             foldername += " (" + str(cycles) + ")"
  177.         return foldername
  178.  
  179.     def _illegal_symbol_filter(self, filter_string):
  180.         filter_set = ["/", "\\", "<", ">", "|", ":", "*", "?", "\"", "\n"]
  181.         if sys.platform != "win32":
  182.             filter_set = ["/"]
  183.         for to_replace in filter_set:
  184.             if to_replace in filter_string:
  185.                 filter_string = filter_string.replace(to_replace, "")
  186.         return filter_string
  187.  
  188. def main():
  189.     app = RadioPlayerApp()
  190.     app.mainloop()
  191.  
  192. if __name__ == "__main__":
  193.     main()
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement