summaryrefslogtreecommitdiff
path: root/enqueue_download.sh
blob: e9be4a56b55fc1128ad92ac577b19dff456452b6 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
DBG="" # DBG=1
UAGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'
[ -z "$1" ] && printf "Pass at least one URL\n" && read g && exit 1
OUT_F=/tmp/dl.sh
OUTD="/dev/shm"
enqueue_vid_choose_fmt() {
  export PICKER=fzy
  f1=$(yt-dlp -Fq $1 |
    awk 'NR > 2 && $2 != "mhtml" && (/360p/ || /480p/ || /720p/ || /1280/ || /1080p/ || /1920/)' |
    $PICKER |
    awk '{print $1}')
	ytdlp="yt-dlp --no-playlist -f"
  [ -n "$f1" ] && printf "%s '%s' '%s'\n" "$ytdlp" $f1 $1 >> $OUT_F
	#[ -n "$DBG" ] && printf "f1_type = %s\n" "$f1_type"
  #printf "%s %s %s # %s\n" "$ytdlp" $f1 $1 "$title" >> $OUT_F
	#case $f1_type in
		#combo|single-audio)
			#printf "%s %s %s # %s\n" "$ytdlp" $f1 $1 "$title" >> $OUT_F
			#;;
		#"")
      #;;
		#*) # f1 is single video, need audio
			#f2=$(invidious-dl.sh -l -u $1 | grep single-audio | $PICKER)
			#f2=${f2%% *}
			#printf "%s %s+%s %s # %s\n" "$ytdlp" $f1 $f2 $1 "$title" >> $OUT_F
			#;;
	#esac
}
enqueue_direct_link() {
	printf "Adding direct URL with curl: %s\n" "$1"
	printf "Use Tor (y/[n])? "
	read tor && printf "\n"
	[ "$tor" = "y" ] && {
		printf "Using Tor proxy\n"
		proxy="socks5h://192.168.2.1:9100"
	}
	printf "curl -L -C - --retry 5 -x \"%s\" --output-dir %s -A '%s' -O '%s'\n" \
    "$proxy" $OUTD "$UAGENT" "$1" >> $OUT_F
}
enqueue_direct_link_wav() {
	author=${SFEED_FEED_PATH##*/}
	fname=${1##*/}
	fname="$OUTD/"${fname%%.*}".opus"
	printf "curl %s | opusenc --artist %s - %s\n" $1 $author $fname >> $OUT_F
}
open_img() {
  imgurl="$1"
  curl --output-dir $OUTD -s -C - -O $imgurl
  img2sixel $OUTD/${imgurl##*/}
  rm $OUTD/${imgurl##*/}
  read _
}
for url in $@; do
	[ -n "$DBG" ] && printf "processing url: %s\n" "$url"
	protocol="${url%%:*}"
	[ -n "$DBG" ] && printf "protocol: %s\n" "$protocol"
	case $protocol in
		http|https|magnet|gemini|/*)
			;;
		*)
			printf "unknown protocol\n"
			read grb
			continue
			;;
	esac
	case "$url" in
    https://archlinux.org/releng/releases/*/)
      mag="$(curl -s $url | grep -oE --color=never 'magnet:.*\.iso')"
      transmission-remote -a "$mag"
      ;;
		# this is "glob" matching - negation is !, not ^
		*.jpg|*.JPG*|*.jpeg|*.png|*.webp|*.gif)
			open_img "$url"
			;;
		*.wav)
			u=$(printf "%s" "$1"|grep -Eo "http[s]?://.*.wav")
			enqueue_direct_link_wav "$u"
			;;
		*.)
			u=$(printf "%s" "$1"|grep -Eo "http[s]?://.*.wav")
			enqueue_direct_link_wav "$u"
			;;
		*.gz|*.opus*|*.mp3*|*.ogg*|*.mp4*|*.webm*|*.epub)
			# Clean up URL from &... or ?... stuff at the end
			u=$(printf "%s" "$url" |
        grep -Eo "http[s]?://.*(gz|opus|mp3|ogg|mp4|webm|epub)")
			enqueue_direct_link "$u"
			;;
		magnet:\?*|*.torrent)
			[ -n "$DBG" ] && printf "Adding torrent with transmission: %s\n" "$url"
			transmission-remote 127.0.0.1 --add $url
			printf "Added to torrents with RC = %s, press any key\n" "$?"
			read v
			;;
		https://doc.rt.com/*)
      mp4u=$(curl -s -A "$UAGENT" "$url"|grep -Eo 'https://.*/download/.*\.mp4')
      printf "curl -L -C - -A '%s' -O '%s'\n" "$UAGENT" "$mp4u" >> $OUT_F
      ;;
		*tube.cthd.icu*|*invidio.xamh.de*|*youtu.be*|*youtube-nocookie.com*|*youtube.com/*|*yewtu.be*|*ytprivate.com*|*invidious*|*au.ytprivate.com*|*y.com.cm*|*inv.riverside.rocks*|*youtube.076.ne.jp*|https://rutube.ru/video/*|https://vk.com/video*|https://vimeo.com/*|https://smotrim.ru/*)
			[ -n "$DBG" ] && printf "Adding youtube link: '%s'\n" "$url"
			enqueue_vid_choose_fmt "$url"
			;;
		https://xkcd.com/*)
			imgurlx=$(curl -s ${url}info.0.json|jq -r '.img')
			alttext=$(curl -s ${url}info.0.json|jq -r '.alt')
			printf "%s\n" "$alttext"
			open_img "$imgurlx"
			;;
		https://github.com*)
      #https://github.com/hrsh7th/nvim-cmp/releases/tag/v0.0.1
      tag=${url##*/}
      repo=${url%%/releases*}
      dir=${repo##*/}-$tag
      git clone --branch $tag --depth 1 $repo.git /tmp/${dir}
      rm -rf /tmp/$dir/.git*
      ;;
		https://oper.ru/*)
			mp3link=$(scrape_mp3_links_from_URL.lua "$url")
			[ -n "$mp3link" ] && enqueue_direct_link "$mp3link" || {
				printf "Got no mp3 link from scraper\n"
				read v
			}
			;;
		*)
			[ -n "$SFEED_FEED_PATH" ] && { # link opened from sfeed_curses
				case "$url" in
				  https://*phoronix.com*)
				    proxychains -q links -dump $url|awk 'NR>26'|sed '/^\s*IFrame/q'|less
  				exit 0
			    ;;
				  https://*habr.com*)
				    proxychains -q links -dump $url|grep -vE '^\s*Link:|\[IMG\]'|less
  				exit 0
			    ;;
				  http*thesaker.is*)
				    links -dump $url |
              awk 'f;/^\s*Saker Message:$/{f=1}' |
              sed '/Leave a Reply --Link to Moderat.*/q' | less
  				exit 0
			    ;;
			    *)
    				proxychains -q links -dump $url|less
    				exit 0
  				;;
  			esac
			}
			printf "Fall-through case, opening with \$BROWSER: %s\n" "$url"
			$BROWSER $url
			exit 0
			;;
	esac
done