Skip to content

Instantly share code, notes, and snippets.

@koutoftimer
Created September 11, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koutoftimer/89948020f97b273732bf1e31ed360bb8 to your computer and use it in GitHub Desktop.
Save koutoftimer/89948020f97b273732bf1e31ed360bb8 to your computer and use it in GitHub Desktop.
Load and sort torrent list at fast-torrent.ru without waiting for page to fully load
#!/usr/bin/env bash
# Usage: fast-torrent-sorter <URL>
# or: fast-torrent-sorter
#
# Required software and tools:
# * `wget` or `curl` - to load torrent list
# * `xclip` - fallback in case if no arguments provided
# * `xdg-open` - to be more generic about default browser
#
# kind of standard:
# * `bash` - as interpreter
# * `uuidgen` - part of util-linux (as common as bash itself)
#
# Copyright 2021 Ruslan Kovtun
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Set URL variable
if [ $# -eq 1 ]; then
URL=$1
else
URL=$(xclip -o)
if [ -z "$URL" ]; then
cat >&2 <<EOF
Usage $0 [fast torrent url]
$0 # url accessed via `xclip`
EOF
exit 1
fi
fi
# Verify that URL contains link to fast-torrent.ru
if [[ "$URL" != http://fast-torrent\.ru* ]] && [[ "$URL" != https://fast-torrent\.ru* ]]; then
echo Link must start from http:// or https:// >&2
exit 1
fi
# Generate link to torrent list
if [[ "$URL" != *torrents\.html ]]; then
URL=${URL%\.html}/torrents.html
fi
# Extract title out of URL to prevent additional page loading
TITLE="$(basename ${URL%%\/torrents\.html})"
# Generate output filename. UUID used here to make sure that failed
# consecutive calls will not corrupt successfull. Failsafe reasons.
OUTPUT="/tmp/fast-torrent-$TITLE-$(uuidgen).html"
# Hide useless information in the $OUTPUT document
cat > "$OUTPUT" << EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$TITLE</title>
<style>
.upload_help, .upload-header, .hidden, .c1, .c19 { display: none; }
.upload1 {
display: flex;
flex-direction: row;
justify-content: space-around;
margin: 1em;
}
</style>
</head>
<body>
EOF
# Load torrent list
if command -v wget > /dev/null ; then
wget -qO- "$URL" >> "$OUTPUT"
elif command -v curl > /dev/null ; then
curl -s "$URL" >> "$OUTPUT"
fi
# Sort torrent list
cat >> "$OUTPUT" << EOF
<script>
// sort torrents by size
let rows = Array.prototype.slice.call(
document.querySelectorAll(".torrent-row"), 0)
const size = el => parseInt(el.attributes.size.value)
rows.sort((a, b) => size(a) - size(b))
let ordering = document.querySelector('.ordering')
rows.forEach(row => ordering.appendChild(row)) // appening element second time will reorder
// fix download links
for (let link of document.querySelectorAll('a.download-event')) {
let href = link.href.replace('file:///', 'http://fast-torrent.ru/')
link.href = href
}
</script>
</body>
</html>
EOF
# Open in default browser
xdg-open $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment