This commit is contained in:
Ashton Harding 2019-01-24 23:13:19 -06:00
commit 2f9d336788
4 changed files with 91 additions and 0 deletions

24
README.md Normal file
View File

@ -0,0 +1,24 @@
# kashire's youtube query
kyq searches on youtube so you don't have to.
I only made it because I hate opening youtube
to find videos to watch on my desktop.
## how to use
`./kyq -h` for help
`./kyq -q [query]` for searching.
After searching just run it through your favorite video player.
`mpv $url`
You could also use the python script:
`python kyq.py $query` -- No help feature here.
## Speed difference
not the reason I made it but it's interesting to note
![speed_comparison.png](speed_comparison.png)
## Dependencies
* none for the shell script
* bs4 for the python script.

41
kyq Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
package="kyq"
youtubeURI="https://www.youtube.com/results?search_query="
if [ -z "$1" ]; then
echo 'You must specify flag. (e.g. -h)'
fi
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "$package - For returning a list of youtube videos."
echo " "
echo "$package [option] [argument]"
echo " "
echo "options:"
echo "-h, --help Shows this"
echo "-q, --query Search for specific query on youtube"
exit 0
;;
-q|--query)
query=$(echo ${@:2} | sed 's/ /%20/g' )
html=$(wget -qO- $youtubeURI$query)
echo $html >> tmp.html
title=$(xmllint --html --xpath '//h3/a[@href]/span' tmp.html)
url=$(xmllint --html --xpath '//h3/a/@href' tmp.html)
echo $title | sed 's/<\/span>/\n/g' | sed 's/^.*>//' >> A_TITLE.txt
echo $url | sed 's/href="/\n/g' | sed 's/&.*//' | sed 's/"//' | sed 's/^/https:\/\/www.youtube.com/' | tail -n +2 | sed '/user/d' >> A_URL.txt
# Removes duplicate URL
$(awk '!x[$1]++' A_URL.txt >> A_URL_SORTED.txt)
## Loop through both files and post them side by side.
# lol...
$(paste A_URL_SORTED.txt A_TITLE.txt > A_FINISHED.txt)
cat A_FINISHED.txt
$(rm A_FINISHED.txt A_TITLE.txt A_URL_SORTED.txt A_URL.txt tmp.html)
break
;;
esac
done

26
kyq.py Normal file
View File

@ -0,0 +1,26 @@
import urllib.request
from bs4 import BeautifulSoup
import sys
## Requirements
# pip3 install bs4 --user
## NOTE: This is a tempory solution for now.
## I really need this to work via bash
## or through the youtube API.
user_query = ""
for x in range(1, len(sys.argv)):
user_query += sys.argv[x]
user_query += " "
query = urllib.parse.quote(user_query)
url = "https://www.youtube.com/results?search_query=" + query
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
for vid in soup.findAll(attrs={'class':'yt-uix-tile-link'}):
if ("/user/" not in vid['href']) and ("&list" not in vid['href']):
vals = 30 - len(vid['href'])
spaces = vals * ' '
print(' https://www.youtube.com' + vid['href'] + spaces + vid['title'])

BIN
speed_comparison.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB