summaryrefslogtreecommitdiff
path: root/ytuh
blob: b2023d120a48908295960e1fa5b448133e53152c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash

usage(){
	echo "Usage: ytuh [-rf] <ARGS>" 
	echo "options:"
	echo "     Default: Returns channel ID"
	echo "r    Reverse: Returns channel name when a channel ID is given (can be used with -f)"
	echo "f    Feed: Returns RSS feed URL"
	exit
}

# Print usage info if no arguments/options
if [ $# == 0 ]; then
	usage
fi

r="0"
f="0"

# Get ID from name (still called when -r specified, will clean this up later)
function getId(){
	if [[ $r == "0" ]]; then
		id=$(curl -s "https://validator.w3.org/feed/check.cgi?url=https://youtube.com/@$1" | grep "Validator Results" | sed -n 's/.*id=\(.*\)<\/title>.*/\1/p')
	fi
}

# Get name from ID (-r specified)
function reverse() {
	name="$(curl -Ls https://youtube.com/channel/$1 | grep "canonicalBaseUrl" | sed 's/.*canonicalBaseUrl":"\/@\([^"]*\)".*/\1/')"
}

# Get feed url (-f specified)
feed(){
	feedUrl="https://youtube.com/feeds/videos.xml?channel_id=$id"
}

while getopts 'r:f:' OPTION; do 
	case "$OPTION" in
		r)
			r="1"
			id="$2"
			reverse "$id"
			;;
		f)
			f="1"
			getId "$OPTARG"
			feed "$id"
			;;
		*)
			usage
			;;
	esac
done

if [ $r == "1" ] && [ $f == "0" ]; then
	output="$name"
elif [ $r == "0" ] && [ $f == "0" ]; then
	getId "$1"
	output="$id"
else
	output="$feedUrl"
fi

echo $output