aboutsummaryrefslogtreecommitdiff
path: root/yts
blob: 1d29306fc076a2b17f9a38db808d267aed4012f6 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh

url_yts="https://yts.mx"
menu="bemenu"

die()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "󰎁  yts" "$1"

	printf "\033[31;1merr: %b\033[0m\n" "$1"
	exit "${2:-1}"
}

dep_check()
{
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

get_yts()
{
	: "${1:?}"
	dep_check "curl"

	curl --silent -H 'Accept-Encoding: gzip,deflate, br' --compressed "${url_yts}/$1"
}

search_yts()
{
	: "${1:?}"

	get_yts "browse-movies/$(echo "$1" | sed 's/ /%20/g')/all/all/0/latest/0/all" |
		grep browse-movie-link | sed -e 's/^.*movies\///g' -e 's/".*$//g'
}

id_yts()
{
	: "${1:?}"
	fetch=
	select=

	fetch="$(get_yts "movies/$1" | grep -Eo "value=\"[A-Z0-9]{40}\">[a-zA-Z0-9 ]*")"
	select="$(echo "$fetch" | cut -d'>' -f2 | "$menu" -il 4 -p " ")"
	[ -n "$select" ] &&
		echo "$fetch" | grep "$select" | cut -d'"' -f2

	unset select
	unset fetch
}

main()
{
	query=
	fetch=
	id=
	out=

	[ -z "$WAYLAND_DISPLAY" ] &&
		menu="dmenu"
	if [ "$#" -gt 0 ] && [ "$1" = "-o" ]; then
		out=true
		shift
	fi

	dep_check "$menu"

	query="${*:-$(printf "" | "$menu" -p "󱇒 ")}"
	[ -z "$query" ] &&
		exit 1

	fetch="$(search_yts "$query")"

	id="$(id_yts "$(echo "$fetch" | tr '-' ' ' | "$menu" -il 20 -p "󰎁 " | tr ' ' '-')")"
	[ -z "$id" ] &&
		exit 1

	if [ "$out" = true ]; then
		echo "${url_yts}/torrent/download/${id}"
	else
		dep_check "pirowatch"
		pirowatch -s "${url_yts}/torrent/download/${id}"
	fi
}

main "$@"