aboutsummaryrefslogtreecommitdiff
path: root/1337x
blob: 6f3d8a360d1764bdf93b2b1be152f3d53d1a34a1 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh

url_1337x="https://1337x.to"
menu="bemenu"

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

	command -v notify-send > /dev/null &&
		notify-send "󰎁  1337x" "$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_1337x()
{
	: "${1:?}"
	dep_check curl

	curl --silent --compressed "${url_1337x}/$1"
}

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

	get_1337x "search/$(echo "$1" | tr ' ' '+')/1/"
}

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

	get_1337x "torrent/${1}/" |
		grep -om1 'magnet:?xt=urn:btih:.*announce'
}

parse_table()
{
	intbody=
	link=
	sneed=
	leech=
	size=

	while read -r line; do
		case "$line" in
		"<tbody>")
			intbody=1
			;;
		"</tbody>")
			exit 0
			;;
		"<td class=\"coll-1 name"*)
			line=${line%/\">*}
			link=${line##*href=\"/}
			;;
		"<td class=\"coll-2 seeds"*)
			sneed=${line#*>}
			sneed=${sneed%%<*}
			;;
		"<td class=\"coll-3 leeches"*)
			leech=${line#*>}
			leech=${leech%%<*}
			;;
		"<td class=\"coll-4 size"*)
			size=${line#*>}
			size=${size%%<*}
			;;
		"</tr>")
			[ "$intbody" = 1 ] &&
				printf "%4d   %4d   %8s  %s\n" \
					"$sneed" "$leech" "$size" \
					"$(echo "${link##*/}" | tr '-' ' ')"
			;;
		esac
	done | "$menu" -p "󰎁 " -il 20 | sed 's/.*  //g' | tr ' ' '-'

	unset intbody name link sneed leech size
}

main()
{
	query=
	data=
	name=
	page=
	magnet=

	[ -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

	data="$(search_1337x "$query")"
	[ -z "$data" ] &&
		die "search failed, check your internet connection"

	name="$(echo "$data" | parse_table)"
	[ -z "$name" ] &&
		exit 1

	page="$(echo "$data" | grep -om1 "[0-9]*/${name}")"
	magnet="$(magnet_1337x "$page")"
	[ -z "$magnet" ] &&
		die "magnet fetch failed, contact the maintainer"

	if [ "$out" = true ]; then
		echo "$magnet"
	else
		dep_check "pirowatch"
		pirowatch "$magnet"
	fi
}

main "$@"