Skip to content

Instantly share code, notes, and snippets.

@xDeda
Created September 24, 2016 20:47
Show Gist options
  • Save xDeda/fe7c9b54cfbc5377781749fc438d3e29 to your computer and use it in GitHub Desktop.
Save xDeda/fe7c9b54cfbc5377781749fc438d3e29 to your computer and use it in GitHub Desktop.
python script to fetch first youtube result from search with arg string
#!/usr/bin/env python
import urllib
import urllib.request
import clipboard
import sys
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
from urllib.parse import quote
from bs4 import BeautifulSoup
Notify.init("Result")
textToSearch = sys.argv[1]
query = urllib.parse.quote(textToSearch)
with urllib.request.urlopen("https://www.youtube.com/results?search_query=" + query) as url:
html = url.read()
soup = BeautifulSoup(html, "html.parser")
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
clipboard.copy('https://www.youtube.com' + vid['href'])
print('Found!')
print('[[' + vid.string + ']] = https://www.youtube.com' + vid['href'])
Hello=Notify.Notification.new(vid.string, "https://www.youtube.com" + vid['href'], "face-wink")
Hello.show()
break
else:
print('Not found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment