celaigia.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import pygame
  2. import threading
  3. import time
  4. import random
  5. import os
  6. import atexit
  7. import ntpath
  8. import json
  9. import webbrowser
  10. import subprocess
  11. import dearpygui.dearpygui as dpg
  12. from mutagen.mp3 import MP3
  13. from tkinter import Tk,filedialog, simpledialog, Button, OptionMenu, N, S , E, W, Label, StringVar
  14. import pytube
  15. dpg.create_context()
  16. dpg.create_viewport(title="celaigia, stai senza pensieri",large_icon="logo.ico",small_icon="logo.ico")
  17. pygame.mixer.init()
  18. #https://www.redhat.com/sysadmin/write-GUI-applications-python
  19. global state
  20. state=None
  21. _SONG_FILE = "data/songs.json"
  22. _NUM_YT_SEARCH_RESULTS = 5
  23. _DEFAULT_DOWNLOAD_PATH = 'data/music'
  24. _DEFAULT_MUSIC_VOLUME = 0.5
  25. pygame.mixer.music.set_volume(_DEFAULT_MUSIC_VOLUME)
  26. def bash(cmd):
  27. subprocess.call(['/bin/bash', '-c', cmd])
  28. def string_sanitizer(s: str)->str:
  29. s = ''.join(filter(lambda x: x in '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ', s))
  30. return s.replace(' ','_')
  31. def ismusic(filename: str)->bool:
  32. return filename.split('.')[-1] in ['mp3', 'ogg', 'flac', 'wav']
  33. def update_volume(sender, app_data):
  34. pygame.mixer.music.set_volume(app_data / 100.0)
  35. def load_database():
  36. songs = json.load(open(_SONG_FILE, "r+"))["songs"]
  37. for filename in songs:
  38. dpg.add_button(
  39. label=f"{ntpath.basename(filename)}",
  40. callback=play,
  41. width=-1,
  42. height=25,
  43. user_data=filename.replace("\\", "/"),
  44. parent="list"
  45. )
  46. dpg.add_spacer(height=2, parent="list")
  47. def update_database(filename: str):
  48. data = json.load(open(_SONG_FILE, "r+"))
  49. if filename not in data["songs"]:
  50. data["songs"] += [filename]
  51. dpg.add_button(
  52. label=f"{ntpath.basename(filename)}",
  53. callback=play,
  54. width=-1,
  55. height=25,
  56. user_data=filename.replace("\\", "/"),
  57. parent="list"
  58. )
  59. dpg.add_spacer(height=2, parent="list")
  60. json.dump(data, open(_SONG_FILE, "r+"), indent=4)
  61. def update_slider():
  62. global state
  63. while pygame.mixer.music.get_busy():
  64. dpg.configure_item(item="pos",default_value=pygame.mixer.music.get_pos()/1000)
  65. time.sleep(0.7)
  66. state=None
  67. dpg.configure_item("cstate",default_value=f"State: None")
  68. dpg.configure_item("csong",default_value="Now Playing : ")
  69. dpg.configure_item("play",label="Play")
  70. dpg.configure_item(item="pos",max_value=100)
  71. dpg.configure_item(item="pos",default_value=0)
  72. def play(sender, app_data, user_data):
  73. global state
  74. if user_data:
  75. pygame.mixer.music.load(user_data)
  76. audio = MP3(user_data)
  77. dpg.configure_item(item="pos",max_value=audio.info.length)
  78. pygame.mixer.music.play()
  79. thread=threading.Thread(target=update_slider,daemon=False).start()
  80. if pygame.mixer.music.get_busy():
  81. dpg.configure_item("play",label="Pause")
  82. state="playing"
  83. dpg.configure_item("cstate",default_value=f"State: Playing")
  84. dpg.configure_item("csong",default_value=f"Now Playing : {ntpath.basename(user_data)}")
  85. def play_pause():
  86. global state
  87. if state=="playing":
  88. state="paused"
  89. pygame.mixer.music.pause()
  90. dpg.configure_item("play",label="Play")
  91. dpg.configure_item("cstate",default_value=f"State: Paused")
  92. elif state=="paused":
  93. state="playing"
  94. pygame.mixer.music.unpause()
  95. dpg.configure_item("play",label="Pause")
  96. dpg.configure_item("cstate",default_value=f"State: Playing")
  97. else:
  98. song = json.load(open(_SONG_FILE, "r"))["songs"]
  99. if song:
  100. song=random.choice(song)
  101. pygame.mixer.music.load(song)
  102. pygame.mixer.music.play()
  103. thread=threading.Thread(target=update_slider,daemon=False).start()
  104. dpg.configure_item("play",label="Pause")
  105. if pygame.mixer.music.get_busy():
  106. audio = MP3(song)
  107. dpg.configure_item(item="pos",max_value=audio.info.length)
  108. state="playing"
  109. dpg.configure_item("csong",default_value=f"Now Playing : {ntpath.basename(song)}")
  110. dpg.configure_item("cstate",default_value=f"State: Playing")
  111. def stop():
  112. global state
  113. pygame.mixer.music.stop()
  114. state=None
  115. def add_files():
  116. data=json.load(open(_SONG_FILE,"r"))
  117. root=Tk()
  118. root.withdraw()
  119. filename=filedialog.askopenfilename(filetypes=[("Music Files", ("*.mp3","*.wav","*.ogg"))])
  120. root.quit()
  121. if filename.endswith(".mp3" or ".wav" or ".ogg"):
  122. if filename not in data["songs"]:
  123. update_database(filename)
  124. #dpg.add_button(label=f"{ntpath.basename(filename)}",callback=play,width=-1,height=25,,parent="list")
  125. #dpg.add_spacer(height=2,parent="list")
  126. def add_folder():
  127. data=json.load(open(_SONG_FILE,"r"))
  128. root=Tk()
  129. root.withdraw()
  130. folder=filedialog.askdirectory()
  131. root.quit()
  132. for filename in os.listdir(folder):
  133. if filename.endswith(".mp3" or ".wav" or ".ogg"):
  134. if filename not in data["songs"]:
  135. update_database(os.path.join(folder,filename).replace("\\","/"))
  136. #dpg.add_button(label=f"{ntpath.basename(filename)}",callback=play,width=-1,height=25,user_data=os.path.join(folder,filename).replace("\\","/"),parent="list")
  137. #dpg.add_spacer(height=2,parent="list")
  138. def search(sender, app_data, user_data):
  139. songs = json.load(open(_SONG_FILE, "r"))["songs"]
  140. dpg.delete_item("list", children_only=True)
  141. for index, song in enumerate(songs):
  142. if app_data in song.lower():
  143. dpg.add_button(label=f"{ntpath.basename(song)}", callback=play,width=-1, height=25, user_data=song, parent="list")
  144. dpg.add_spacer(height=2,parent="list")
  145. def add_download_choices(sender, app_data, user_data)->None:
  146. yt_urls = {string_sanitizer(r.title): r.watch_url for r in pytube.Search(app_data).results[:_NUM_YT_SEARCH_RESULTS]}
  147. for url_key in yt_urls:
  148. dpg.add_button(label=url_key, tag=url_key, callback= download ,width=-1, height=25, user_data = (url_key,yt_urls), parent="sidebar")
  149. dpg.add_spacer(height=2,parent="sidebar")
  150. def download(sender, app_data, user_data):
  151. url_key, yt_urls = user_data
  152. url = yt_urls[url_key]
  153. for tmp_url_key in yt_urls:
  154. dpg.delete_item(tmp_url_key)
  155. video_file = f'{_DEFAULT_DOWNLOAD_PATH}/{url_key}.mp4'
  156. audio_file = f"{video_file[:-4]}.mp3"
  157. stream = pytube.YouTube(url).streams.filter(only_audio=True).first()
  158. stream.download(filename=video_file, skip_existing=True)
  159. bash(f"ffmpeg -i {video_file} -vn -acodec mp3 -y {audio_file}")
  160. bash(f"rm {video_file}")
  161. update_database(audio_file)
  162. def removeall():
  163. songs = json.load(open(_SONG_FILE, "r"))
  164. songs["songs"].clear()
  165. json.dump(songs,open(_SONG_FILE, "w"),indent=4)
  166. dpg.delete_item("list", children_only=True)
  167. load_database()
  168. with dpg.theme(tag="base"):
  169. with dpg.theme_component():
  170. dpg.add_theme_color(dpg.mvThemeCol_Button, (130, 142, 250))
  171. dpg.add_theme_color(dpg.mvThemeCol_ButtonActive, (137, 142, 255, 95))
  172. dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (137, 142, 255))
  173. dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 3)
  174. dpg.add_theme_style(dpg.mvStyleVar_ChildRounding, 4)
  175. dpg.add_theme_style(dpg.mvStyleVar_FramePadding, 4, 4)
  176. dpg.add_theme_style(dpg.mvStyleVar_WindowRounding, 4, 4)
  177. dpg.add_theme_style(dpg.mvStyleVar_WindowTitleAlign, 0.50, 0.50)
  178. dpg.add_theme_style(dpg.mvStyleVar_WindowBorderSize,0)
  179. dpg.add_theme_style(dpg.mvStyleVar_WindowPadding,10,14)
  180. dpg.add_theme_color(dpg.mvThemeCol_ChildBg, (25, 25, 25))
  181. dpg.add_theme_color(dpg.mvThemeCol_Border, (0,0,0,0))
  182. dpg.add_theme_color(dpg.mvThemeCol_ScrollbarBg, (0,0,0,0))
  183. dpg.add_theme_color(dpg.mvThemeCol_TitleBgActive, (130, 142, 250))
  184. dpg.add_theme_color(dpg.mvThemeCol_CheckMark, (221, 166, 185))
  185. dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (172, 174, 197))
  186. with dpg.theme(tag="slider_thin"):
  187. with dpg.theme_component():
  188. dpg.add_theme_color(dpg.mvThemeCol_FrameBgActive, (130, 142, 250,99))
  189. dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (130, 142, 250,99))
  190. dpg.add_theme_color(dpg.mvThemeCol_SliderGrabActive, (255, 255, 255))
  191. dpg.add_theme_color(dpg.mvThemeCol_SliderGrab, (255, 255, 255))
  192. dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (130, 142, 250,99))
  193. dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 3)
  194. dpg.add_theme_style(dpg.mvStyleVar_GrabMinSize, 30)
  195. with dpg.theme(tag="slider"):
  196. with dpg.theme_component():
  197. dpg.add_theme_color(dpg.mvThemeCol_FrameBgActive, (130, 142, 250,99))
  198. dpg.add_theme_color(dpg.mvThemeCol_FrameBgHovered, (130, 142, 250,99))
  199. dpg.add_theme_color(dpg.mvThemeCol_SliderGrabActive, (255, 255, 255))
  200. dpg.add_theme_color(dpg.mvThemeCol_SliderGrab, (255, 255, 255))
  201. dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (130, 142, 250,99))
  202. dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 3)
  203. dpg.add_theme_style(dpg.mvStyleVar_GrabMinSize, 30)
  204. with dpg.theme(tag="songs"):
  205. with dpg.theme_component():
  206. dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 2)
  207. dpg.add_theme_color(dpg.mvThemeCol_Button, (89, 89, 144,40))
  208. dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (0,0,0,0))
  209. with dpg.font_registry():
  210. monobold = dpg.add_font("fonts/MonoLisa-Bold.ttf", 12)
  211. head = dpg.add_font("fonts/MonoLisa-Bold.ttf", 15)
  212. with dpg.window(tag="main",label="window title"):
  213. with dpg.child_window(autosize_x=True,height=45,no_scrollbar=True):
  214. dpg.add_text(f"Now Playing : ",tag="csong")
  215. dpg.add_spacer(height=2)
  216. with dpg.group(horizontal=True):
  217. with dpg.child_window(width=250, tag="sidebar"):
  218. dpg.add_text("Celaigia",color=(137, 142, 255))
  219. dpg.add_text("phuturemachine")
  220. dpg.add_spacer(height=2)
  221. dpg.add_button(label="Support",width=-1,height=23,callback=lambda:webbrowser.open(url="gitgitigitigiti"))
  222. dpg.add_spacer(height=5)
  223. dpg.add_separator()
  224. dpg.add_spacer(height=5)
  225. dpg.add_button(label="Add File",width=-1,height=28,callback=add_files)
  226. dpg.add_button(label="Add Folder",width=-1,height=28,callback=add_folder)
  227. dpg.add_button(label="Remove All Songs",width=-1,height=28,callback=removeall)
  228. dpg.add_spacer(height=5)
  229. dpg.add_separator()
  230. dpg.add_spacer(height=5)
  231. dpg.add_text(f"State: {state}",tag="cstate")
  232. dpg.add_spacer(height=5)
  233. dpg.add_separator()
  234. dpg.add_input_text(hint="search youtube",width=-1,callback=add_download_choices, on_enter=True)
  235. dpg.add_spacer(height=5)
  236. with dpg.child_window(autosize_x=True,border=False):
  237. with dpg.child_window(autosize_x=True,height=50,no_scrollbar=True):
  238. with dpg.group(horizontal=True):
  239. dpg.add_button(label="Play",width=65,height=30,tag="play",callback=play_pause)
  240. dpg.add_button(label="Stop",callback=stop,width=65,height=30)
  241. dpg.add_slider_float(tag="volume", width=120,height=15,pos=(160,19),format="%.0f%.0%",default_value=_DEFAULT_MUSIC_VOLUME * 100,callback=update_volume)
  242. dpg.add_slider_float(tag="pos",width=-1,pos=(295,19),format="")
  243. with dpg.child_window(autosize_x=True,delay_search=True):
  244. with dpg.group(horizontal=True,tag="query"):
  245. dpg.add_input_text(hint="search for a song locally",width=-1,callback=search)
  246. dpg.add_spacer(height=5)
  247. with dpg.child_window(autosize_x=True,delay_search=True,tag="list"):
  248. load_database()
  249. dpg.bind_item_theme("volume","slider_thin")
  250. dpg.bind_item_theme("pos","slider")
  251. dpg.bind_item_theme("list","songs")
  252. dpg.bind_theme("base")
  253. dpg.bind_font(monobold)
  254. def safe_exit():
  255. pygame.mixer.music.stop()
  256. pygame.quit()
  257. atexit.register(safe_exit)
  258. dpg.setup_dearpygui()
  259. dpg.show_viewport()
  260. dpg.set_primary_window("main",True)
  261. dpg.maximize_viewport()
  262. dpg.start_dearpygui()
  263. dpg.destroy_context()