Skip to content

Commit

Permalink
Daemon rewrite
Browse files Browse the repository at this point in the history
We don't need to implement a save mechanism: aria2c already has
--save-session.
  • Loading branch information
baskerville committed Mar 28, 2013
1 parent 7be447f commit 2dbbe4d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 127 deletions.
78 changes: 26 additions & 52 deletions README.md
@@ -1,77 +1,51 @@
![diana] (https://github.com/baskerville/diana/raw/master/preview/diana_logo.jpg)
![diana] (https://github.com/baskerville/diana/raw/master/logo/diana-logo.jpg)

SYNOPSIS
diana <action> [arguments]
## Usage

ACTIONS
list
Show the list of active downloads.
dad [-h|-d DOWNLOAD_DIR] start|stop
diana ACTION [ARGUMENTS]

paused
Show the list of paused downloads.
## Actions

stopped
Show the list of stopped downloads.
- `list` — Output the list of active downloads.

errors
Show the list of encountered errors.
- `paused` — Output the list of paused downloads.

stats
Show download bandwidth statistics.
- `stopped` — Output the list of stopped downloads.

sleep
Pause all the active downloads.
- `info [--select-file=...] GID ...` — Output informations regarding the given GIDs.

wake
Resume all the paused downloads.
- `files GID ...` — Output the files owned by the downloads corresponding to the given GIDs.

purge
Clear the list of stopped downloads and errors.
- `errors` — Output the list of errors.

clean
Stop seeding completed downloads.
- `stats` — Output download bandwidth statistics.

kill
Kill the daemon.
- `add [--select-file=...] ITEM ...` — Download the given items (local or remote URLs to torrents, etc.).

add [--select-file=...] ITEM ...
Download the given items (local or remote URLs to torrents, etc.).
- `remove GID ...` — Remove the downloads corresponding to the given GIDs.

remove GID ...
Remove the downloads corresponding to the given GIDs.
- `forcerm GID ...` — Forcibly remove the downloads corresponding to the given GIDs.

forcerm GID ...
Forcibly remove the downloads corresponding to the given GIDs.
- `pause GID ...` — Pause the downloads corresponding to the given GIDs.

pause GID ...
Pause the downloads corresponding to the given GIDs.
- `resume GID ...` — Resume the downloads corresponding to the given GIDs.

resume GID ...
Resume the downloads corresponding to the given GIDs.
- `preview [--select-file=...] GID ...` — Preview all the files from all the downloads corresponding to the given GIDs.

files GID ...
Show the files owned by the downloads corresponding to the given GIDs.
- `sleep` — Pause all the active downloads.

info [--select-file=...] GID ...
Retrieve informations regarding the given GIDs.
- `wake` — Resume all the paused downloads.

preview [--select-file=...] GID ...
Preview all the files from all the downloads corresponding to the given GIDs.
- `purge` — Clear the list of stopped downloads and errors.

- `clean` — Stop seeding completed downloads.

## Prerequisites

First launch the daemon which is called `dad`.

## Get the File Indexes

This function might help:

tl () {
aria2c -S "$@" | grep '\./'
}
## Environment Variables

- `DIANA_DOWNLOAD_DIR` — Used by the daemon.

## Dependencies

`aria2` and `python3`.
- aria2
- python3
93 changes: 55 additions & 38 deletions dad
@@ -1,50 +1,67 @@
#! /bin/sh

diana list > /dev/null 2>&1
usage() {
printf '%s [-h|-d DOWNLOAD_DIR] start|stop\n' "${0##*/}"
}

if [ $? -eq 0 ] ; then
printf '%s.\n' 'The daemon is already running' >&2
download_dir=${DIANA_DOWNLOAD_DIR:-${XDG_DOWNLOAD_DIR:-${HOME}}}

while getopts 'hd:' opt ; do
case "$opt" in
h)
usage
exit 0
;;
d)
download_dir=$OPTARG
;;
esac
done

shift $((OPTIND - 1))

if [ $# -ne 1 ] ; then
usage
exit 1
fi

DOWNLOAD_DIR=${1:-${XDG_DOWNLOAD_DIR:-${HOME}}}
daemon_pid=/tmp/diana-daemon.pid

aria2c -D --enable-rpc -j 13 -d "$DOWNLOAD_DIR"
case "$1" in
start)
if [ -e "$daemon_pid" ] ; then
printf '%s\n' 'The daemon is already running.' >&2
exit 1
fi

HASHES_FILE=${XDG_DATA_HOME:-${HOME}/.local/share}/diana.lst
BACKUP_HASHES=${HASHES_FILE}.bak
XDG_DATA_HOME=${XDG_DATA_HOME:-${HOME}/.local/share}
diana_session=${XDG_DATA_HOME}/diana.session

if [ ! -e "$HASHES_FILE" ] ; then
if [ -e "$BACKUP_HASHES" ] ; then
mv "$BACKUP_HASHES" "$HASHES_FILE"
else
exit 0
fi
fi
[ ! -e "$diana_session" ] && touch "$diana_session"

tmp_file=$(mktemp)
sort -u "$HASHES_FILE" > "$tmp_file"
cp "$tmp_file" "$HASHES_FILE"
rm "$tmp_file"

for line in $(cat "$HASHES_FILE") ; do
rhsh=${line%:*}
sel=${line#*:}
hsh=${rhsh#_}

for tor in "$DOWNLOAD_DIR"/*.torrent ; do
match=$(aria2c -S "$tor" | grep -m 1 "$hsh")
if [ -n "$match" ] ; then
printf '%s\n' "$tor"
gid=$(diana --select-file="$sel" add "$tor")
[ $? -ne 0 ] && exit 1
case $rhsh in
_*)
diana pause $gid
;;
esac
aria2c --daemon --enable-rpc --continue --dir "$download_dir" --input-file "$diana_session" --save-session "$diana_session"

pgrep -n -x aria2c > "$daemon_pid"

if [ $? -ne 0 ] ; then
printf '%s\n' 'The daemon failed to start.' >&2
rm "$daemon_pid"
exit 1
fi
done
done
;;
stop)
if [ ! -e "$daemon_pid" ] ; then
printf '%s\n' 'The daemon is not running.' >&2
exit 1
fi

kill $(cat "$daemon_pid") > /dev/null 2>&1

mv "$HASHES_FILE" "$BACKUP_HASHES"
if [ $? -ne 0 ] ; then
printf '%s\n' 'Could not stop the daemon.' >&2
exit 1
else
rm "$daemon_pid"
fi
;;
esac
38 changes: 1 addition & 37 deletions diana
Expand Up @@ -13,7 +13,6 @@ from getopt import getopt

MAX_NAME_LEN = 50
ELLIPSIS = '...'
HASHES_FILE = '{}/diana.lst'.format(os.getenv('XDG_DATA_HOME', '{}/.local/share'.format(os.getenv('HOME'))))
PREVIEW_COMMAND = 'open'
INFO_COMMAND = 'fileinfo'
BLACK = 30
Expand Down Expand Up @@ -200,36 +199,6 @@ def add_items(items, options={}):
else:
print(response['result'])

def save_kill():
hf = open(HASHES_FILE, 'w')
downloads = []
active = get_active()
paused = get_waiting()
if active:
downloads.extend(active['result'])
if paused:
downloads.extend(paused['result'])
for d in downloads:
if 'infoHash' in d:
gid = d['gid']
infoHash = d['infoHash']
status = d['status']
if status == 'paused':
infoHash = '_%s' % infoHash
response = call_func('getFiles', [gid])
if response:
files = response['result']
selection = []
for f in files:
index = f['index']
selected = f['selected']
if selected == 'true':
selection.append(index)
select_file_argument = ','.join(selection)
hf.write('%s:%s\n' % (infoHash, select_file_argument))
hf.close()
call_func('shutdown')

def pause_all():
call_func('pauseAll')

Expand Down Expand Up @@ -370,9 +339,6 @@ ACTIONS
clean
Stop seeding completed downloads.
kill
Kill the daemon.
add [--select-file=...] ITEM ...
Download the given items (local or remote URLs to torrents, etc.).
Expand Down Expand Up @@ -456,15 +422,13 @@ def main():
call_func('purgeDownloadResult')
elif action == 'clean':
clean()
elif action == 'kill':
save_kill()
else:
print('Unknown action: %s' % action, file=sys.stderr)
exit(1)
else:
usage()

NO_ARGS_ACTIONS = ('list', 'paused', 'stopped', 'errors', 'stats', 'sleep', 'wake', 'purge', 'clean', 'kill')
NO_ARGS_ACTIONS = ('list', 'paused', 'stopped', 'errors', 'stats', 'sleep', 'wake', 'purge', 'clean')
ARGS_ACTIONS = ('add', 'remove', 'forcerm', 'preview', 'pause', 'resume', 'files')

EXIT_CODES = {
Expand Down
Binary file added logo/diana-logo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed preview/diana_logo.jpg
Binary file not shown.

0 comments on commit 2dbbe4d

Please sign in to comment.