torrents.csv/scripts/scan_torrents.sh

79 lines
2.1 KiB
Bash
Raw Normal View History

#!/bin/bash
# Checking arguments
# Help line
cd ..
torrents_csv="`pwd`/torrents.csv"
2018-11-26 17:08:18 +00:00
scanned_out="`pwd`/infohashes_scanned.txt"
tmp_torrent_dir="`pwd`/tmp_torrents"
names_out="`pwd`/names.out"
health_out="`pwd`/health.out"
2018-12-21 21:31:21 +00:00
touch $scanned_out
touch $names_out
touch $health_out
2018-12-21 21:31:21 +00:00
help="Run ./scan_torrents.sh [TORRENTS_DIR] \nor goto https://gitlab.com/dessalines/torrents.csv for more help"
if [ "$1" == "-h" ] || [ -z "$1" ]; then
echo -e $help
exit 1
fi
torrents_dir="$1"
echo "Torrents dir=$torrents_dir"
# Check dependencies
if command -v "torrent-tracker-health" >/dev/null 2>&1 ; then
echo "torrent-tracker-health installed."
else
echo -e "Installing torrent-tracker-health:\nnpm i -g dessalines/torrent-tracker-health \nhttps://github.com/dessalines/torrent-tracker-health\n"
npm i -g install dessalines/torrent-tracker-health
fi
# Loop over all torrents
pushd $torrents_dir
2018-12-21 21:31:21 +00:00
# for torrent_file in *.torrent; do
# Copy the unscanned torrent files to a temp dir
mkdir $tmp_torrent_dir
2018-12-21 21:31:21 +00:00
find `pwd` -name "*.torrent" | sort -n | grep -vFf $scanned_out | while read torrent_file ; do
cp "$torrent_file" "$tmp_torrent_dir"
echo $(basename "$torrent_file" .torrent) >> $names_out
done
2019-01-26 08:25:07 +00:00
# Delete null torrents from the temp dir
find $tmp_torrent_dir -name "*.torrent" -size -2k -delete
if [ -z "$(ls -A $tmp_torrent_dir)" ]; then
echo "No new torrents."
else
2018-12-21 21:31:21 +00:00
# Scrape it
torrent-tracker-health --torrent "$tmp_torrent_dir" > $health_out
echo -e "$health_out"
2019-01-26 08:25:07 +00:00
# Convert the json results to csv format
results=$(jq -r '.results | map([.hash, .name, .length, (.created | .[0:16] | strptime("%Y-%m-%dT%H:%M") | mktime), .seeders, .leechers, .completed, (now | floor)] | join(";")) | join("\n")' $health_out)
# // "1970-01-01T00:00:00.000Z"
# If there are no results
if [ -z "$results" ]; then
echo "There were no results for some reason."
else
# Update the torrents.csv and infohashes scanned file
echo -e "$results" >> $torrents_csv
cat "$names_out" >> $scanned_out
popd
cd scripts
. prune.sh
fi
2018-11-26 17:08:18 +00:00
fi
2018-12-21 21:31:21 +00:00
# Remove the temp dir
rm -rf "$tmp_torrent_dir"
rm "$names_out"
rm "$health_out"