Ticket #2026: checkFinishedTransmissionDownloads.sh

File checkFinishedTransmissionDownloads.sh, 3.3 KB (added by gaa, 13 years ago)

Script that checks for finished downloads in Transmission and sends email to a specified user.

Line 
1#!/bin/sh
2
3# Script that checks for finished downloads in Transmission and
4# sends email to a specified user.
5# This code placed into public domain
6
7# Requires:
8#   GNU mailutils | bsd-mailx (does not work with heirloom-mailx)
9#   lockfile-progs
10#   transmission-cli
11
12# History:
13#----------------------------------------------------------------------------
14# Date        | Author <EMail>                  | Description               |
15#----------------------------------------------------------------------------
16# 04 May 2009 | A.Galanin <gaa.nnov AT mail.ru> | Creation                  |
17#----------------------------------------------------------------------------
18
19# default configuration options
20# put it into config file ~/.checkFinishedTransmissionDownloads/config
21HOST=localhost
22PORT=9091
23
24RPC_AUTH=1
25USER=username
26PASS=password
27
28MAILTO=root
29FROM=torrent-checker
30MAIL_CONTENT="Downloading of \"%s\" has been finished.\nGo to %s to make an approriate action.\n"
31
32#------------------------------------------------------------------------------
33
34# files
35FILEPATH="$HOME/.checkFinishedTransmissionDownloads"
36CONFIG_FILE="$FILEPATH/config"
37NOTIFY_FILE="$FILEPATH/notified"
38ALL_FILE="/tmp/checkFinishedTransmissionDownloads.all"
39TMP_FILE="/tmp/checkFinishedTransmissionDownloads.tmp"
40LOCK_FILE="/tmp/checkFinishedTransmissionDownloads"
41
42[ -f "$CONFIG_FILE" ] && . "$CONFIG_FILE"
43
44#------------------------------------------------------------------------------
45
46# Call transmission-remote with corresponding parameters
47callTransmission () {
48    if [ "$RPC_AUTH" -eq 0 ]
49    then
50        transmission-remote "$HOST":"$PORT" "$@"
51    else
52        transmission-remote "$HOST":"$PORT" -N "$TMP_FILE" "$@"
53    fi
54}
55
56# Remove lock and temporary files, exit with code $1
57exitAndClean () {
58    kill "$LOCK_PID"
59    lockfile-remove "$LOCK_FILE"
60    rm -f "$TMP_FILE" "$ALL_FILE"
61
62    exit "$1"
63}
64
65# initialization
66lockfile-create "$LOCK_FILE" || (echo "Unable to lock lockfile!"; exitAndClean 2)
67lockfile-touch "$LOCK_FILE" &
68LOCK_PID="$!"
69
70trap "exitAndClean 1" HUP INT QUIT KILL
71
72mkdir -p "$FILEPATH"
73touch "$NOTIFY_FILE" "$TMP_FILE"
74echo -n > "$ALL_FILE"
75chmod 600 "$TMP_FILE" "$ALL_FILE"
76# generate netrc file for RPC authorisation
77printf "machine %s\nlogin %s\npassword %s\n" "$HOST" "$USER" "$PASS" > "$TMP_FILE"
78
79if [ $# != 0 ]
80then
81    echo "$0: check for finished downloads in Transmission"
82    echo "USAGE: $0"
83    exit 1
84fi
85
86# main
87callTransmission -l | gawk '{
88    if ($1 != "Sum:" && $1 != "ID") {
89        print $1,$2
90    }
91}' | while read id percent
92do
93    reply="`callTransmission -t "$id" -i | grep -E '^  Name|^  Hash'`"
94
95    name="`echo "$reply" | grep '^  Name'  | cut -c 9-`"
96    hash="`echo "$reply" | grep '^  Hash'  | cut -c 9-`"
97
98    # check that notification is not yet sent
99    grep -q "$hash" "$NOTIFY_FILE"
100    if [ $? = 1 -a "$percent" = "100%" ]
101    then
102        printf "$MAIL_CONTENT" "$name" "http://$HOST:$PORT/" | \
103            mailx $MAILTO -s "Torrents info: $name" -a "From: $FROM"
104        echo "$hash" >> "$NOTIFY_FILE"
105    fi
106    echo "$hash" >> "$ALL_FILE"
107done
108
109# remove deleted torrents from sent notifications list
110sort "$NOTIFY_FILE" > "$TMP_FILE"
111mv "$TMP_FILE" "$NOTIFY_FILE"
112
113sort "$ALL_FILE" > "$TMP_FILE"
114mv "$TMP_FILE" "$ALL_FILE"
115
116comm -1 -2 "$NOTIFY_FILE" "$ALL_FILE" > "$TMP_FILE"
117mv "$TMP_FILE" "$NOTIFY_FILE"
118
119exitAndClean 0
120