From 69b8241af7531cc1a422b66cf479bde90b1c2f60 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 4 Oct 2018 22:25:52 -0700 Subject: [PATCH] Checking to see if it already exists. --- .gitignore | 1 + add_torrents.sh | 39 ++++++++++++++++++++++++++++----------- find.sh | 3 ++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index e69de29..2172cfc 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +run.out diff --git a/add_torrents.sh b/add_torrents.sh index 529e5d5..168c146 100755 --- a/add_torrents.sh +++ b/add_torrents.sh @@ -1,6 +1,6 @@ # Checking arguments # 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" if [ "$1" == "-h" ] || [ -z "$1" ]; then @@ -54,7 +54,12 @@ for torrent_file in *.torrent; do # Convert the created date 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_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" # 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 - - # Append the add lines to the torrents.csv file - echo -e "\n$add_line" >> $torrents_csv_file - truncate -s -1 $torrents_csv_file # Removing last newline - echo -e "Added $name to $torrents_csv_file" - # head -n1 $torrents_csv_file && sort <(tail -n+2 $torrents_csv_file) > $torrents_csv_file # Sort it + # 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 else - echo -e "$name had no seeders, or was already added." + echo -e "$name has no seeders." fi 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 + diff --git a/find.sh b/find.sh index 6144eac..f7318ed 100755 --- a/find.sh +++ b/find.sh @@ -14,9 +14,10 @@ else infohash=$(echo -e "$line" | cut -d ';' -f1) magnet_link="magnet:?xt=urn:btih:$infohash" name=$(echo -e "$line" | cut -d ';' -f2) + seeders=$(echo -e "$line" | cut -d ';' -f5) # Construct the search result - result="$name\n\t$magnet_link" + result="$name\n\tseeders: $seeders\n\tlink: $magnet_link" echo -e "$result" done <<< "$search" fi