Checking to see if it already exists.

This commit is contained in:
Dessalines 2018-10-04 22:25:52 -07:00
parent 98388dc3fa
commit 69b8241af7
3 changed files with 31 additions and 12 deletions

1
.gitignore vendored
View File

@ -0,0 +1 @@
run.out

View File

@ -1,6 +1,6 @@
# Checking arguments # Checking arguments
# Help line # Help line
torrents_csv_file="`pwd`/torrents.csv" torrents_csv="`pwd`/torrents.csv"
help="Run ./add_torrents.sh [TORRENTS_DIR] \nor goto https://gitlab.com/dessalines/torrents.csv for more help" help="Run ./add_torrents.sh [TORRENTS_DIR] \nor goto https://gitlab.com/dessalines/torrents.csv for more help"
if [ "$1" == "-h" ] || [ -z "$1" ]; then if [ "$1" == "-h" ] || [ -z "$1" ]; then
@ -54,7 +54,12 @@ for torrent_file in *.torrent; do
# Convert the created date # Convert the created date
date_string=$(grep -Po 'Created on: \K.*' <<< $show_text) date_string=$(grep -Po 'Created on: \K.*' <<< $show_text)
created_date=$(date -d "${date_string}" +"%s") created_date=""
if [[ "$date_string" == "Unknown" ]]; then
created_date=$(date +%s)
else
created_date=$(date -d "${date_string}" +"%s")
fi
# Scrape for seeder counts # Scrape for seeder counts
scrape_text=$(timeout 20 python -m torrent_tracker_scraper.scraper \ scrape_text=$(timeout 20 python -m torrent_tracker_scraper.scraper \
@ -71,20 +76,32 @@ for torrent_file in *.torrent; do
add_line="$infohash;$name;$size_bytes;$created_date;$seeders;$leechers;$completed;$scraped_date" add_line="$infohash;$name;$size_bytes;$created_date;$seeders;$leechers;$completed;$scraped_date"
# Only add the line if there are seeds, and the infohash doesn't already exist # Only add the line if there are seeds, and the infohash doesn't already exist
if (( $seeders > 0 )) && ! grep -q $infohash $torrents_csv_file; then if (( $seeders > 0 )); then
# TODO edit this logic to do a line replace where its already there, to pick up new seeder counts # If the infohash already exists, replace the line
found_line=$(grep -n $infohash $torrents_csv | cut -d : -f 1)
# Append the add lines to the torrents.csv file if [ ! -z $found_line ]; then
echo -e "\n$add_line" >> $torrents_csv_file sed -i "$found_line c$add_line" $torrents_csv
truncate -s -1 $torrents_csv_file # Removing last newline echo -e "Found $name, updating peers"
echo -e "Added $name to $torrents_csv_file" else
# head -n1 $torrents_csv_file && sort <(tail -n+2 $torrents_csv_file) > $torrents_csv_file # Sort it # 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
else else
echo -e "$name had no seeders, or was already added." echo -e "$name has no seeders."
fi fi
done done
# 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

View File

@ -14,9 +14,10 @@ else
infohash=$(echo -e "$line" | cut -d ';' -f1) infohash=$(echo -e "$line" | cut -d ';' -f1)
magnet_link="magnet:?xt=urn:btih:$infohash" magnet_link="magnet:?xt=urn:btih:$infohash"
name=$(echo -e "$line" | cut -d ';' -f2) name=$(echo -e "$line" | cut -d ';' -f2)
seeders=$(echo -e "$line" | cut -d ';' -f5)
# Construct the search result # Construct the search result
result="$name\n\t$magnet_link" result="$name\n\tseeders: $seeders\n\tlink: $magnet_link"
echo -e "$result" echo -e "$result"
done <<< "$search" done <<< "$search"
fi fi