summaryrefslogtreecommitdiff
path: root/share/tsh/nyaa.sh
blob: 259dabd4a49768fcdc1482600cc8c08e0db31dc8 (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
#!/bin/sh

# shellcheck disable=SC2154
# (we expect the variables to be set)

# $1`: line number used to get link
get_magnet() { sed -n "${1}p" "$links"; }

get_torrents() {
	base_url="https://nyaa.si"
	page="/?q=$query&p=1"
	i=0
	while true; do
		i=$((i + 1))
		page="$(curl -s "$base_url$page" | tee -a "$html" | pup -p 'li.next a attr{href}')"
		if [ -z "$page" ]; then
			printf '%s.\n' "$i"
			break
		else
			>&2 printf '%s ' "$i"
		fi
	done
	# No results
	[ "$i" -eq 0 ] && return 1

	pup -f "$html" -p 'table.torrent-list tr td:nth-child(2) a:last-child attr{title}' >"$names"
	pup -f "$html" -p 'table.torrent-list tr td:nth-child(3) a:last-child attr{href}' >"$links"
	pup -f "$html" -p 'table.torrent-list tr td:nth-child(4) text{}' | tr -d ' ' >"$sizes"
	pup -f "$html" -p 'table.torrent-list tr td:nth-child(6) text{}' >"$seeds"

	paste "$sizes" "$seeds" "$names" >"$results"
}