search (2916B)
1 #!/bin/sh 2 # 3 # ytsrch2.sh 4 # Created by Hayden Hamilton 5 # 6 # hayden@haydenvh.com 7 # Copyright (c) 2020 Hayden Hamilton 8 # 9 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 10 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 11 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 12 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 13 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 14 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 15 # SOFTWARE. 16 # 17 # This work is free. You can redistribute it and/or modify it under the 18 # terms of the Do What The Fuck You Want To Public License, Version 2, 19 # as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. 20 21 22 [ -z $1 ] 2>/dev/null && echo "usage: ytsrch2 <query> [number of items]" && exit 23 query="$1" 24 while ! [ $2 -gt 0 ] && [ ! -z $2 ] 25 do 26 query="$query $2" 27 shift 28 done 2>/dev/null 29 content=$(curl -H "Accept-Language: en-US" -qsL "https://www.youtube.com/results?search_query=$(echo "$query" | xxd -p | tr -d '\n' | sed 's/../%&/g')&hl=en") 30 31 # this is just fucking braindead obfuscation, fuck youtube 32 33 # titles=$(echo "$content" | sed 's/"}\],"accessibility"/\n/g' | grep -o '"width":336*,"height":188}]},"title":{"runs":\[{"text":".*' | sed 's~"width":336,"height":188}\]},"title":{"runs":\[{"text":"~~' | nl) 34 titles=$(echo "$content" | sed 's/"}\],"accessibility"/\n/g' | grep -o ',"title":{"runs":\[{"text":".*' | sed 's/,"title":{.*\[{"text":"//g' | nl) # nice fix from Nebula_W2081d 35 authors=$(echo "$content" | grep -o 'ownerText":{"runs":\[{"text":"[^"]*' | sed 's~ownerText":{"runs":\[{"text":"~~' | nl) 36 length=$(echo "$content" | grep -o '"lengthText":{"accessibility":{"accessibilityData":{"label":"[^"]*' | sed 's~"lengthText":{"accessibility":{"accessibilityData":{"label":"~~' | nl) 37 dates=$(echo "$content" | grep -o '"publishedTimeText":{"simpleText":"[^"]*"},"lengthText":' | sed 's~"publishedTimeText":{"simpleText":"~~;s~"},"lengthText":$~~' | nl) 38 views=$(echo "$content" | grep -o '"simpleText":"[^"]*"},"viewCountText":{"simpleText":"[0-9,]* views"},"navigationEndpoint":{"clickTrackingParams":"' | sed -E 's/.*"simpleText":"([0-9,]*) views".*/\1/' | nl) 39 uris=$(echo "$content" | grep -o '{"videoId":"[^"]*","thumbnail":{"thumbnails":\[{"url":"https://i.ytimg.com/vi/' | sed -E 's~.*"videoId":"([^"]*)",".*~https://www.youtube.com/embed/\1~' | nl) 40 41 getelem(){ 42 echo "$2" | awk -v "num=$1" '$1 == num' | sed 's/^[[:space:]]*[0-9]*[[:space:]]*//;s/\\u0026/\&/g' 43 } 44 45 for i in $(seq ${2:-20}) 46 do 47 printf "\nTitle: %s\nURI: %s\nAuthor: %s\nPublished: %s\nLength: %s\nViews: %s\n" \ 48 "$(getelem $i "$titles" )" \ 49 "$(getelem $i "$uris" )" \ 50 "$(getelem $i "$authors")" \ 51 "$(getelem $i "$dates" )" \ 52 "$(getelem $i "$length" )" \ 53 "$(getelem $i "$views" )" 54 done | less