#!/bin/sh rc="${HOME}/.ezget" # default options directory="${HOME}" exclude='^$' # read configuration if [ -r "${rc}/config" ] ; then while read key value ; do case "${key}" in 'directory') directory="${value/#~/"${HOME}"}" ;; 'exclude') exclude="${value}" ;; esac done < "${rc}/config" fi # download new torrents (in random order) cat "${rc}/urls" \ | grep -v '^#' \ | shuf \ | while read url ; do # extract magnet links from this URL curl --silent --show-error "${url}" \ | sed -e 's/&/\&/g' \ | grep -o 'magnet:?[^"]*' \ | while read magnet ; do # parse each link xt=$(echo "${magnet}" | grep -o 'xt=urn:btih:[[:alnum:]]*') dn=$(echo "${magnet}" | grep -o 'dn=[^&]*') btih=${xt#xt=urn:btih:} name=${dn#dn=} outfile="${directory}/${btih}.torrent" # should we get this torrent? if grep -q "${btih}" "${rc}/done" 2> /dev/null ; then # already did continue elif echo "${name}" | grep -Eq "${exclude}" ; then # name matches exclude regex continue fi echo "Adding torrent ${name}…" echo "d10:magnet-uri${#magnet}:${magnet}e" > "${outfile}" echo "${btih}" >> "${rc}/done" done done