2018-10-05 06:24:29 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-10-04 20:23:57 +00:00
|
|
|
# Checking arguments
|
|
|
|
# Help line
|
2018-10-05 05:25:52 +00:00
|
|
|
torrents_csv="`pwd`/torrents.csv"
|
2018-10-04 20:23:57 +00:00
|
|
|
|
2018-10-04 23:33:33 +00:00
|
|
|
help="Run ./add_torrents.sh [TORRENTS_DIR] \nor goto https://gitlab.com/dessalines/torrents.csv for more help"
|
2018-10-04 20:23:57 +00:00
|
|
|
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 transmission-cli >/dev/null 2>&1 ; then
|
|
|
|
echo "transmission-cli version: $(transmission-cli --version)"
|
|
|
|
else
|
|
|
|
echo "Install transmission-cli"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if command -v "python -m torrent_tracker_scraper.scraper" >/dev/null 2>&1 ; then
|
|
|
|
echo "torrent-tracker-scraper installed."
|
|
|
|
else
|
|
|
|
echo -e "Installing torrent_tracker_scraper:\npip install torrent-tracker-scraper --user \nhttps://github.com/ZigmundVonZaun/torrent-tracker-scraper"
|
|
|
|
pip install torrent-tracker-scraper --user
|
|
|
|
fi
|
|
|
|
|
|
|
|
if command -v "~/.local/bin/humanfriendly -c" >/dev/null 2>&1 ; then
|
|
|
|
echo "humanfriendly installed."
|
|
|
|
else
|
|
|
|
echo -e "Installing humanfriendly:\npip install humanfriendly --user"
|
|
|
|
pip install humanfriendly --user
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Loop over all torrents
|
|
|
|
cd $torrents_dir
|
|
|
|
for torrent_file in *.torrent; do
|
|
|
|
|
|
|
|
# Get fields from transmission
|
|
|
|
|
|
|
|
show_text=$(transmission-show "$torrent_file")
|
|
|
|
# echo "show text = $show_text"
|
|
|
|
|
|
|
|
name=$(grep -Po -m 1 'Name: \K.*' <<< $show_text)
|
2018-10-04 23:24:56 +00:00
|
|
|
name=$(sed 's/;/\\;/g' <<< $name) # Escape the commas for .csv
|
2018-10-04 20:23:57 +00:00
|
|
|
|
|
|
|
# Size: Unfortunately this will chop off some sigfigs
|
|
|
|
size=$(grep -Po 'Total Size: \K.*' <<< $show_text)
|
|
|
|
size_bytes=$(~/.local/bin/humanfriendly --parse-size="$size") # Convert to bytes
|
|
|
|
|
|
|
|
infohash=$(grep -Po 'Hash: \K.*' <<< $show_text)
|
|
|
|
|
|
|
|
# Convert the created date
|
|
|
|
date_string=$(grep -Po 'Created on: \K.*' <<< $show_text)
|
2018-10-05 05:25:52 +00:00
|
|
|
created_date=""
|
|
|
|
if [[ "$date_string" == "Unknown" ]]; then
|
|
|
|
created_date=$(date +%s)
|
|
|
|
else
|
|
|
|
created_date=$(date -d "${date_string}" +"%s")
|
|
|
|
fi
|
2018-10-04 20:23:57 +00:00
|
|
|
|
|
|
|
# Scrape for seeder counts
|
2018-10-04 21:05:50 +00:00
|
|
|
scrape_text=$(timeout 20 python -m torrent_tracker_scraper.scraper \
|
2018-10-04 20:23:57 +00:00
|
|
|
-i "$infohash" \
|
|
|
|
-t tracker.coppersurfer.tk -p 6969)
|
|
|
|
# -t tracker.internetwarriors.net -p 1337
|
|
|
|
# -t tracker.opentrackr.org -p 1337
|
|
|
|
seeders=$(grep -Po 'Seeds: \K[0-9]+' <<< $scrape_text)
|
|
|
|
leechers=$(grep -Po 'Leechers: \K[0-9]+' <<< $scrape_text)
|
|
|
|
completed=$(grep -Po 'Completed: \K[0-9]+' <<< $scrape_text)
|
|
|
|
scraped_date=$(date +%s)
|
|
|
|
|
2018-10-04 21:05:50 +00:00
|
|
|
# Construct add line
|
2018-10-04 23:24:56 +00:00
|
|
|
add_line="$infohash;$name;$size_bytes;$created_date;$seeders;$leechers;$completed;$scraped_date"
|
2018-10-04 20:23:57 +00:00
|
|
|
|
|
|
|
# Only add the line if there are seeds, and the infohash doesn't already exist
|
2018-10-05 05:25:52 +00:00
|
|
|
if (( $seeders > 0 )); then
|
|
|
|
|
|
|
|
# If the infohash already exists, replace the line
|
|
|
|
found_line=$(grep -n $infohash $torrents_csv | cut -d : -f 1)
|
|
|
|
if [ ! -z $found_line ]; then
|
|
|
|
sed -i "$found_line c$add_line" $torrents_csv
|
|
|
|
echo -e "Found $name, updating peers"
|
|
|
|
else
|
|
|
|
# Append the add lines to the torrents.csv file
|
|
|
|
echo -e "\n$add_line" >> $torrents_csv
|
|
|
|
truncate -s -1 $torrents_csv # Removing last newline
|
|
|
|
echo -e "Added $name"
|
|
|
|
fi
|
2018-10-04 21:05:50 +00:00
|
|
|
else
|
2018-10-05 05:25:52 +00:00
|
|
|
echo -e "$name has no seeders."
|
2018-10-04 20:23:57 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
|
2018-10-05 05:25:52 +00:00
|
|
|
# Sort the file, remove random newlines
|
|
|
|
sed -i '/^$/d' $torrents_csv
|
|
|
|
header=$(head -n1 $torrents_csv)
|
|
|
|
sed -i '1d' $torrents_csv
|
|
|
|
sort --field-separator=';' --key=5 -nr -o $torrents_csv $torrents_csv
|
|
|
|
sed -i "1i $header" $torrents_csv
|
|
|
|
truncate -s -1 $torrents_csv # Removing last newline
|
|
|
|
|
2018-10-04 21:05:50 +00:00
|
|
|
|
|
|
|
|