|
| 1 | +from pygame import mixer |
| 2 | +import random |
| 3 | +import asyncio |
| 4 | +import os |
| 5 | +from youtube_downloader import url_download, search_and_download |
| 6 | + |
| 7 | +# Initialize the mixer |
| 8 | +mixer.init() |
| 9 | + |
| 10 | +# Function to get all .mp3 files in a directory |
| 11 | +async def get_music_files(directory): |
| 12 | + try: |
| 13 | + return [os.path.join(directory, file) for file in os.listdir(directory) if file.endswith(".mp3")] |
| 14 | + except FileNotFoundError: |
| 15 | + print(f"Directory {directory} not found.") |
| 16 | + return [] |
| 17 | + |
| 18 | +# Used directories for different playlists |
| 19 | +directories = { |
| 20 | + "oldies": './assets/oldies', |
| 21 | + "democrazy": './assets/democrazy', |
| 22 | + "heavy": './assets/heavy', |
| 23 | + "downloads": './assets/downloads' |
| 24 | +} |
| 25 | + |
| 26 | +# Check and create directories if they don't exist |
| 27 | +for directory in directories.values(): |
| 28 | + os.makedirs(directory, exist_ok=True) |
| 29 | + |
| 30 | +# Function to get all playlists |
| 31 | +async def get_playlists(directories): |
| 32 | + playlists = {name: await get_music_files(directory) for name, directory in directories.items()} |
| 33 | + playlists["all"] = sum(playlists.values(), []) |
| 34 | + return playlists |
| 35 | + |
| 36 | +# Function to update the downloads playlist |
| 37 | +async def update_downloads_playlist(playlists): |
| 38 | + playlists["downloads"] = await get_music_files(directories["downloads"]) |
| 39 | + playlists["all"] = sum(playlists.values(), []) |
| 40 | + print("Updated downloads playlist:", [os.path.basename(file) for file in playlists["downloads"]]) |
| 41 | + |
| 42 | +# Function to choose a playlist |
| 43 | +def choice(playlists): |
| 44 | + while True: |
| 45 | + for name, files in playlists.items(): |
| 46 | + print(f"{name} = {[os.path.basename(file) for file in files]}") |
| 47 | + |
| 48 | + user_choice = input("What playlist do you want to play: ").strip().lower() |
| 49 | + if user_choice in playlists and playlists[user_choice]: |
| 50 | + return playlists[user_choice] |
| 51 | + else: |
| 52 | + print("Playlist is empty or invalid. Please choose a different playlist.") |
| 53 | + |
| 54 | +# Function to play music from the chosen playlist |
| 55 | +async def music_play(playlist, playlists): |
| 56 | + if not playlist: |
| 57 | + print("The playlist is empty.") |
| 58 | + return |
| 59 | + |
| 60 | + volume = 0.5 |
| 61 | + current_index = 0 |
| 62 | + |
| 63 | + while True: |
| 64 | + print(f"Loading file: {playlist[current_index]}") |
| 65 | + mixer.music.load(playlist[current_index]) |
| 66 | + mixer.music.play() |
| 67 | + mixer.music.set_volume(volume) |
| 68 | + |
| 69 | + while True: |
| 70 | + # Print options for user to control the music player |
| 71 | + print("Press 'p' to pause, 'r' to resume") |
| 72 | + print("Press '-' or '+' to lower or raise volume") |
| 73 | + print("Press 'c' to change playlist") |
| 74 | + print("Press 'n' to skip to the next song") |
| 75 | + print("Press 'b' to go back to the previous song") |
| 76 | + print("Press 's' to shuffle the playlist") |
| 77 | + print("Press 'e' to exit the program") |
| 78 | + print("Press 'd' to download music from YouTube") |
| 79 | + option = input(" ").strip().lower() |
| 80 | + |
| 81 | + # Handle user input for controlling the music player |
| 82 | + if option in ['p', 'pause']: |
| 83 | + mixer.music.pause() |
| 84 | + elif option in ['r', 'resume']: |
| 85 | + mixer.music.unpause() |
| 86 | + elif option in ['e', 'exit']: |
| 87 | + mixer.music.stop() |
| 88 | + return |
| 89 | + elif option == '-': |
| 90 | + volume = max(0, volume - 0.1) |
| 91 | + mixer.music.set_volume(volume) |
| 92 | + elif option == '+': |
| 93 | + volume = min(1, volume + 0.1) |
| 94 | + mixer.music.set_volume(volume) |
| 95 | + elif option in ['c', 'change']: |
| 96 | + playlist = choice(playlists) |
| 97 | + if not playlist: |
| 98 | + print("The new playlist is empty.") |
| 99 | + continue |
| 100 | + current_index = 0 |
| 101 | + break |
| 102 | + elif option in ['n', 'next']: |
| 103 | + current_index = (current_index + 1) % len(playlist) |
| 104 | + break |
| 105 | + elif option in ['b', 'back']: |
| 106 | + current_index = (current_index - 1) % len(playlist) |
| 107 | + break |
| 108 | + elif option in ['s', 'shuffle']: |
| 109 | + random.shuffle(playlist) |
| 110 | + current_index = 0 |
| 111 | + break |
| 112 | + elif option in ['d', 'download']: |
| 113 | + mixer.music.stop() |
| 114 | + use = input("Download from URL (Y or N): ").strip().lower() |
| 115 | + if use in ["y", "yes"]: |
| 116 | + url = input("Enter the URL of the video (n to break): ") |
| 117 | + if url.lower() not in ["n", "no"]: |
| 118 | + try: |
| 119 | + await url_download(url) |
| 120 | + except Exception as e: |
| 121 | + print(f"Error downloading from URL: {e}") |
| 122 | + else: |
| 123 | + try: |
| 124 | + await search_and_download() |
| 125 | + except Exception as e: |
| 126 | + print(f"Error searching and downloading: {e}") |
| 127 | + await update_downloads_playlist(playlists) |
| 128 | + playlist = playlists["downloads"] |
| 129 | + if not playlist: |
| 130 | + print("The updated playlist is empty.") |
| 131 | + continue |
| 132 | + current_index = 0 |
| 133 | + |
| 134 | +# Main function to start the music player |
| 135 | +async def main(): |
| 136 | + playlists = await get_playlists(directories) |
| 137 | + playlist = choice(playlists) |
| 138 | + await music_play(playlist, playlists) |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + asyncio.run(main()) |
0 commit comments