Ticket #2026: checkFinishedTransmissionDownloads.2.sh

File checkFinishedTransmissionDownloads.2.sh, 3.4 KB (added by gaa, 12 years ago)

Version 1.1

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