|
| 1 | +importrequests |
| 2 | +importos.path |
| 3 | +importwebbrowser |
| 4 | +importtime |
| 5 | +importtkinterastk |
| 6 | +fromfunctoolsimportpartial |
| 7 | +frombs4importBeautifulSoup |
| 8 | +fromseleniumimportwebdriver |
| 9 | +fromselenium.webdriver.chrome.optionsimportOptions |
| 10 | +fromselenium.webdriver.common.keysimportKeys |
| 11 | +fromwebdriver_manager.chromeimportChromeDriverManager |
| 12 | + |
| 13 | + |
| 14 | +classSONG_DOWNLOAD: |
| 15 | +def__init__(self): |
| 16 | +window_root=self.initialize_window() |
| 17 | +self.render_logo_and_greeting(window_root) |
| 18 | +self.render_song_search(window_root) |
| 19 | +window_root.mainloop() |
| 20 | + |
| 21 | +defrender_song_search(self,window_root): |
| 22 | +song_name=tk.Entry(window_root, |
| 23 | +width=25, |
| 24 | +bg='white', |
| 25 | +fg='black', |
| 26 | +border=0, |
| 27 | +font='Verdana 13') |
| 28 | + |
| 29 | +song_name.pack() |
| 30 | +download_button=tk.Button(window_root, |
| 31 | +text='DOWNLOAD', |
| 32 | +border=0, |
| 33 | +highlightbackground='blue', |
| 34 | +command=lambda:self.download_user_song(song_name,window_root) |
| 35 | + ) |
| 36 | + |
| 37 | +download_button.place(x=160,y=250) |
| 38 | + |
| 39 | +defdisplay_label(self,window_root,message,side="bottom",size=15,isBold=True): |
| 40 | +tk.Label(window_root, |
| 41 | +justify=tk.LEFT, |
| 42 | +padx=10, |
| 43 | +text=message.upper(), |
| 44 | +font='Verdana {} bold'.format(size)ifisBold |
| 45 | +else'Verdana {}'.format(size), |
| 46 | +fg='white', |
| 47 | +bg='tomato').pack(side=side) |
| 48 | + |
| 49 | + |
| 50 | +defrender_logo_and_greeting(self,window_root): |
| 51 | +self.logo=tk.PhotoImage(file="music_cutie.gif") |
| 52 | +tk.Label(window_root,image=self.logo ).pack(side='top') |
| 53 | +app_text='Download any song in seconds...' |
| 54 | +self.display_label(window_root,app_text,'top') |
| 55 | + |
| 56 | + |
| 57 | +definitialize_window(self): |
| 58 | +root=tk.Tk() |
| 59 | +root.title('SONGIFY') |
| 60 | +root.geometry('400x400') |
| 61 | +root.resizable(0,0) |
| 62 | +root.configure(background='tomato') |
| 63 | +returnroot |
| 64 | + |
| 65 | +defget_song_name(self): |
| 66 | +song_name=input('Enter a song name: ') |
| 67 | +returnsong_name |
| 68 | + |
| 69 | +defretrieve_song_results(self,song_name): |
| 70 | +song_results=requests.get('https://youtube.com/results?search_query='+song_name) |
| 71 | +song_results=song_results.content |
| 72 | +returnsong_results |
| 73 | + |
| 74 | + |
| 75 | +defretrieve_target_song(self,song_results): |
| 76 | +soup=BeautifulSoup(song_results,'html.parser') |
| 77 | +songs=soup.findAll('div', {'class':'yt-lockup-video'}) |
| 78 | +song=songs[0].contents[0].contents[0].contents[0] |
| 79 | +returnsong |
| 80 | + |
| 81 | + |
| 82 | +definitialise_chrome_driver(self): |
| 83 | +chrome_options=Options() |
| 84 | +chrome_options.add_argument('--headless') |
| 85 | +bot=webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options) |
| 86 | +returnbot |
| 87 | + |
| 88 | +defrun_chrome_driver(self,bot,target_song): |
| 89 | +song_link='https://www.youtube.com'+target_song['href'] |
| 90 | +youtube_to_mp3='https://ytmp3.cc/' |
| 91 | + |
| 92 | +bot.get(youtube_to_mp3) |
| 93 | +search_box=bot.find_elements_by_id('input')[0] |
| 94 | +search_box.send_keys(song_link) |
| 95 | +search_box.send_keys(Keys.RETURN) |
| 96 | + |
| 97 | +defcomplete_download(self,bot,window_root): |
| 98 | +time.sleep(2) |
| 99 | +links=bot.find_elements_by_tag_name('a') |
| 100 | +foriinlinks: |
| 101 | +ifi.get_attribute('text')=='Download': |
| 102 | +try: |
| 103 | +webbrowser.open(i.get_attribute('href')) |
| 104 | +time.sleep(3) |
| 105 | +returnbot.quit() |
| 106 | +except: |
| 107 | +returnbot.quit() |
| 108 | + |
| 109 | + |
| 110 | +defdownload_user_song(self,song_name,window_root): |
| 111 | +iflen(song_name.get())==0: |
| 112 | +returnself.display_label(window_root,'Please enter a valid song', |
| 113 | +'bottom',10,False) |
| 114 | +song_results=self.retrieve_song_results(song_name.get()) |
| 115 | +target_song=self.retrieve_target_song(song_results) |
| 116 | +bot=self.initialise_chrome_driver() |
| 117 | +self.run_chrome_driver(bot,target_song) |
| 118 | +self.complete_download(bot,window_root) |
| 119 | + |
| 120 | +song_download=SONG_DOWNLOAD() |