Skip to content

Commit eb57376

Browse files
Merge pull request #1116 from anshu189/master
youtube.py is now more efficient
2 parents 0337895 + a88feab commit eb57376

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

youtube.py

+21-36
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
'''
2-
Author: Abhinav Anand
3-
git: github.com/ab-anand
4-
mail: abhinavanand1905@gmail.com
5-
Requirements: requests, BeautifulSoupd
2+
Author: Anshu Saini
3+
GitHub: https://github.com/anshu189
4+
mail: anshusaini189381@gmail.com
5+
Requirements: Selenium (pip install selenium), webdriver (https://sites.google.com/a/chromium.org/chromedriver/downloads)
66
'''
7-
import webbrowser
87

9-
import requests
10-
from bs4 import BeautifulSoup
8+
from selenium import webdriver
9+
from time import sleep as s
1110

12-
'''
13-
headers = {
14-
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
15-
'''
16-
input_func = None
17-
try:
18-
input_func = raw_input('Enter the song to be played: ')
19-
input_func+=raw_input('Enter artist name: ')
20-
except NameError:
21-
input_func = input('Enter the song to be played: ')
22-
input_func+=input('Enter artist name: ')
11+
song_name = input("Enter your song name: ")
12+
artist_name = input("Enter the artist name(optional): ")
13+
add = song_name + artist_name
2314

24-
query = input_func.replace(' ', '+')
15+
link = "https://www.youtube.com/results?search_query=" + add
2516

26-
# search for the best similar matching video
27-
url = 'https://www.youtube.com/results?search_query=' + query
28-
source_code = requests.get(url, timeout=15)
29-
plain_text = source_code.text
30-
soup = BeautifulSoup(plain_text, "html.parser")
17+
driver_path = "C:/chromedriver.exe" # Your Chromedriver.exe path here
3118

32-
# fetches the url of the video
33-
songs = soup.findAll('div', {'class': 'yt-lockup-video'})
34-
song = songs[0].contents[0].contents[0].contents[0]
35-
# link = song['href']
36-
# webbrowser.open('https://www.youtube.com' + link)
19+
# <---For Brave Browser--->
20+
# brave_path = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe" # Your Brave.exe path here
21+
# option = webdriver.ChromeOptions()
22+
# option.binary_location = brave_path
3723

38-
try:
39-
link = song['href']
40-
webbrowser.open('https://www.youtube.com' + link)
41-
except KeyError:
42-
print("Can't find any song,check your network or try a new word")
24+
# driv = webdriver.Chrome(executable_path=driver_path, options=option)
25+
driv = webdriver.Chrome(driver_path)
4326

44-
# hey i'm learning git.
45-
# i welcome you too to come and learn
27+
driv.maximize_window()
28+
driv.get(link)
29+
s(0.5)
30+
driv.find_element_by_xpath("//*[@id='video-title']").click()

0 commit comments

Comments
 (0)