summaryrefslogtreecommitdiff
path: root/urititle
blob: f6f93eab5904e203c93e33b4f91b5b92d6ac84ca (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
#!/usr/bin/env bash
scheme=$(printf "%s\n" "$1" | uricut -s)
path=$(printf "%s\n" "$1" | uricut -p)
qs=$(printf "%s\n" "$1" | uricut -q)
domain=$(printf "%s\n" "$1" | uricut -d)
port=$(printf "%s\n" "$1" | uricut -P)
if [ ! "$port" ];then
  if [ "$scheme" = "https" ];then
    port=443
    SSL=--ssl
  fi
  if [ "$scheme" = "http" ];then
    port=80
  fi
fi
if [ "$qs" ];then
  path="${path}?${qs}"
fi
case "$scheme" in
http*)
if [ "$port" ];then
    UA="Mozilla/5.0 (impersonator)"
#  content_type="$(printf "HEAD %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n" "$path" "$domain" "$UA" | ncat -4 $SSL "$domain" "$port" | grep -i '^Content-Type: ' | head -n1 | cut '-d ' -f2 | cut '-d;' -f1 | tr -d '\r\n')"
    a_header="$(curl -Lsi "$1" | head -c 10000 | egrep -ai '^Location: |^Content-Type: ' | head -n1 | tr -d '\r\n')"
    if printf "%s\n" "${a_header}" | grep -i '^Content-Type: ' 2>&1 >/dev/null 2>&1;then
      content_type="$(printf '%s\n' "${a_header}" | cut '-d ' -f2- | cut '-d;' -f1)"
    else
      content_type="redirect probably"
    fi
    if [ "${content_type}" = "text/html" -o "${content_type}" = "application/xhtml+xml" ];then
#    title="$(printf "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n" "$path" "$domain" "$UA" | ncat -4 $SSL "$domain" "$port" | head -c 10000 | tr -d '\n' | tr '<' '\n' | grep -A 10 '^title>' | grep -B 10 '^\/title>' | cut '-d>' -f2)"
      title="$(curl -si "$1" | head -c 1000000 | tr -d '\n' | tr '<' '\n' | grep -iA 10 '^title' | grep -iB 10 '^\/title>' | cut '-d>' -f2 | tr '\t' ' ' | sed 's/^ *//g' | sed 's/ *$//g' | grep .)"
      printf "title: %s\n" "$title" | html_entities_decode
    else
      printf "%s\n" "${a_header}"
    fi
  fi
  ;;
gemini)
  first=1
  gemini-get "$1" | while read -r line;do
    if [ "$first" ];then
      unset first
      if ! printf "%s\n" "$line" | grep '^[^ ]* text/gemini' 2>&1 >/dev/null;then
        printf "title: %s\n" "$(printf "%s\n" "$line" | tr '\t' ' ' | tr -s ' ' | cut '-d ' -f2-)"
      fi
    else
      printf "title: %s\n" "$(printf "%s\n" "$line" | grep '^#' | sed 's/^#* *//g')"
    fi
  done | head -n1
  ;;
magnet)
  printf "title: %s\n" "$(printf "%s\n" "$1" | tr '&' '\n' | grep ^dn= | cut -d= -f2- | uriunescape)"
  ;;
ssh)
  if [ ! "$port" ];then
    port=22
  fi
  printf "title: %s\n" "$(printf "" | nc "$domain" "$port" | head -n1)"
  ;;
*)
  printf "DONT KNOW HOW TO GET TITLE FOR THIS URL: %s\n" "$1"
esac