celaigia.py 11 KB

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