from selenium import webdriver
from bs4 import BeautifulSoup
import requests
def music():
    
    driver = webdriver.Chrome("C:/Users/Akshay Dileep/Desktop/chromedriver")
    
    song_name = input("Enter song name: ")
    song_name = song_name.title()
    song_name = song_name.replace(" ","-")
    song_request = driver.get("https://example.com/mp3-download/"+song_name)
    song_query = driver.find_elements_by_class_name("mp3")
    song_details = driver.find_elements_by_tag_name("h3")
    song_duration = driver.find_elements_by_class_name("mp3-duration")
    
    play_button_x_path = ["//*[@id='mp3List']/div[1]/div[3]/ul/li[1]/div",
                          "//*[@id='mp3List']/div[2]/div[3]/ul/li[1]/div",
                          "//*[@id='mp3List']/div[3]/div[3]/ul/li[1]/div",
                          "//*[@id='mp3List']/div[4]/div[3]/ul/li[1]/div",
                          "//*[@id='mp3List']/div[5]/div[3]/ul/li[1]/div"]
    
    download_button_x_path = ["//*[@id='mp3List']/div[1]/div[3]/ul/li[3]/div",
                              "//*[@id='mp3List']/div[2]/div[3]/ul/li[3]/div",
                              "//*[@id='mp3List']/div[3]/div[3]/ul/li[3]/div",
                              "//*[@id='mp3List']/div[4]/div[3]/ul/li[3]/div",
                              "//*[@id='mp3List']/div[5]/div[3]/ul/li[3]/div"]
    if song_query is None:
        print("Your requested song isn't available")
    else:
        for i in range(5):
            print(str(i + 1)+".> ",song_details[i].text)
            print("duration = ",song_duration[i].text,"\n")
        index = int(input("Enter the index number of song you would like to download: "))
        index = index - 1
        choice = input("would you like to play the song or download the song: ")
        if choice == "play":
            driver.find_element_by_xpath(play_button_x_path[index]).click()
        elif choice == "download":
            driver.find_element_by_xpath(download_button_x_path[index]).click()
            handle = driver.window_handles
            driver.switch_to.window(handle[1])
            request = requests.get(driver.current_url)
            html_page = BeautifulSoup(request.content,"html.parser")
            song_quality = html_page.findAll("div", {"class":"rate"})
            song_size = html_page.findAll("div", {"class":"foot"})
            print("Available qualities are: ")
            for i in range(6):
                print(str(i + 1)+".>",song_quality[i].text)
                print("size:",song_size[i + 1].text,"\n")
            quality_choice = int(input("Which one would you like to download: "))
            if quality_choice == 1:
                driver.find_element_by_class_name("btr-320").click()
            elif quality_choice == 2:
                driver.find_element_by_class_name("btr-256").click()
            elif quality_choice == 3:
                driver.find_element_by_class_name("btr-192").click()
            elif quality_choice == 4:
                driver.find_element_by_class_name("btr-128").click()
            elif quality_choice == 5:
                driver.find_element_by_class_name("btr-64").click()
            elif quality_choice == 6:
                driver.find_element_by_class_name("btr-32").click()
            print("Your song will be downloaded")
music()