summaryrefslogtreecommitdiff
path: root/hib-dlagent
blob: 1dfd64bbf7564bfea7e0f4dc6b45628c2c1d52a5 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash
set -e

VERSION=0.2
LOGIN_PAGE=https://www.humblebundle.com/login
HOME_PAGE=https://www.humblebundle.com/home
COOKIE_JAR=

FILE=
DESTINATION=
DOWNLOAD=1
USERNAME=
PASSWORD=
KEYS=()
STORAGE=

login() {
  curl -s --cookie-jar "$COOKIE_JAR" \
    --data "username=$USERNAME" --data "password=$PASSWORD" "$LOGIN_PAGE"
}

discover_url() {
  local LISTING_PAGE="$1"
  curl -s --cookie "$COOKIE_JAR" "$LISTING_PAGE" | grep "/$FILE?" | \
    sed -e "s/.* data-web='\([^']*\)'.*/\1/" | head -n 1
}

usage() {
  cat <<EOF
hib-dlagent $VERSION
Tool to download Humble Indie Bundle binaries by file name

Usage: $0 [OPTIONS] FILE
Options:
 -d <dir>   Directory for searching and saving files. The normal save location
            is still used, but will be a symbolic link
 -h         This help
 -k <key>   Search key's files. Use multiple times for multiple keys
 -o <file>  Name to use when saving file
 -p <pass>  Use pass to login. If specified multiple times, the last is used
 -s         Print URL to stdout instead of downloading. Incompatible with -d
 -u <user>  Use user to login. Search account's files. If specified multiple
            times, the last is used

If you specify -u, then all of that account's bundles are searched. If a key is
associated with a HIB account then you must use -u/-p, since that key only works
when logged into that account. It is not helpful to specify -k for bundles
associated with an account.
EOF
}

handle_download() {
  local SAVE_FILE="$1"
  local LISTING_PAGES=()

  COOKIE_JAR=$(mktemp)

  if [ -n "$USERNAME" ]; then
    login
    LISTING_PAGES+=("$HOME_PAGE")
  fi

  for KEY in "${KEYS[@]}"; do
    LISTING_PAGES+=("https://www.humblebundle.com/downloads?key=$KEY")
  done

  if [ -z "$LISTING_PAGES" ]; then
    echo "You must specify at least one of -u and -k" >&2
    exit 1
  fi

  local URL
  for LISTING_PAGE in "${LISTING_PAGES[@]}"; do
    URL=$(discover_url "$LISTING_PAGE")
    if [ -n "$URL" ]; then break; fi
  done

  # Cookie no longer necessary.
  rm "$COOKIE_JAR"

  if [ ! -n "$URL" ]; then
    echo "Could not find URL for file: $FILE" >&2
    exit 2
  fi

  if [ $DOWNLOAD -eq 0 ]; then
    echo "$URL"
  else
    curl -C - --retry 3 --retry-delay 3 -o "$SAVE_FILE" "$URL"
  fi
}

main() {
  if [ $# -eq 0 ]; then
    usage
    exit 1
  fi

  while getopts "hd:k:o:p:su:" opt; do
    case $opt in
      \?)
        exit 1
      ;;
      h)
        usage
        exit 1
      ;;
      d)
        STORAGE="$OPTARG"
      ;;
      k)
        KEYS+=("$OPTARG")
      ;;
      o)
        DESTINATION="$OPTARG"
      ;;
      p)
        PASSWORD="$OPTARG"
      ;;
      s)
        DOWNLOAD=0
      ;;
      u)
        USERNAME="$OPTARG"
      ;;
    esac
  done

  shift $(($OPTIND - 1))

  if [ $# == 0 ]; then
    echo "Missing argument FILE" >&2
    exit 1
  fi

  if [ $# != 1 ]; then
    echo "Unexpected argument: $2" >&2
    exit 1
  fi

  if [ $DOWNLOAD -eq 0 -a -n "$STORAGE" ]; then
    echo "-s and -d are incompatible" >&2
    exit 1
  fi

  FILE="$1"

  if [ "$FILE" != "$(basename "$FILE")" ]; then
    # We need a simple file name without any slashes. Instead of erroring, we
    # strip the file name out so that hib://filename is a valid argument.
    FILE=$(basename "$FILE")
    # Since we are doing something fancy, give the user a clue that we modified
    # their input.
    echo "Searching for $FILE" >&2
  fi

  if [ -z "$DESTINATION" ]; then
    DESTINATION="."
  fi

  if [ -d "$DESTINATION" ]; then
    DESTINATION="$DESTINATION/$FILE"
  fi

  if [ -z "$STORAGE" ]; then
    handle_download "$DESTINATION"
  else
    local STORAGE_FILE
    STORAGE_FILE=$(find "$STORAGE" -name "$FILE")
    if [ -z "$STORAGE_FILE" ]; then
      STORAGE_FILE="$STORAGE/$FILE"
      handle_download "$STORAGE_FILE"
    fi

    if [ ! "$STORAGE_FILE" -ef "$DESTINATION" ]; then
      ln -s "$STORAGE_FILE" "$DESTINATION"
      if [ ! "$STORAGE_FILE" -ef "$DESTINATION" ]; then
        # If DESTINATION is in a different directory than the CWD and STORAGE
        # is a relative path, then ln -s will produce a broken link. To produce
        # a valid link we resolve the path with readlink. This is unfortunate
        # because having relative links is nicer and we will undo any symlinks
        # in the path. Therefore, we detect when things have broken and only
        # use readlink when we must.
        STORAGE_FILE=$(readlink -f "$STORAGE_FILE")
        rm "$DESTINATION"
        ln -s "$STORAGE_FILE" "$DESTINATION"
      fi
    fi
  fi
}

main "$@"