vendredi 8 mai 2015

Creating comma seperated list of paths (allowing for spaces in path), and passing result as a variable to use again

I have come close to working this out myself but always seem to come up short.

Basically what I want to achieve is to sort out torrents using a command line program that can output paths (or hashes, names etc) based on certain criteria. I'd like to be able to only choose the torrents that satisfy that criteria but DO NOT share a path with another torrent. There is no way that I know of to know what these are beforehand. So command would have to be repeated a few times, passing the result of the previous call in as a formatted variable to the next call.

For example

rtcontrol ratio=+2 completed=+5d -qopath

will output

/home/user/path/name1
/home/user/path/name2
/home/user/path/name 3
/home/user/path/name 3
/home/user/path/name 4
/home/user/path/name5

This has the chance to produce duplicate paths which are not needed and should be filtered out by using something like uniq -u

I would then need to convert that list to a comma separated list of paths, but allowing for spaces in the path if they occur, e.g.

"/home/user/path/name1","/home/user/path/name2","/home/user/path/name 3","/home/user/path/name 4","/home/user/path/name5"

This will then have to be passed on as a variable to the same program again to get a list of torrents that have one of those paths. Once again there can be duplicates as output, e.g.

#Assuming the results of the previous called are stored in a variable named PATHS
rtcontrol path=$PATHS -qopath

with output of

/home/user/path/name1
/home/user/path/name2
/home/user/path/name 3
/home/user/path/name 3
/home/user/path/name 4
/home/user/path/name5

The reason for this is it is possible that a torrent DOES NOT satisfy the conditions or ratio=+2 completed=+5d, but has the same path as one that DOES. This is the reason for the second call rtcontrol path=$PATHS -qopath

Now what I would need to do is take the output of this as a variable and remove EVERY duplicate path again which should leave me with a list of torrents that satisfy both those conditions.

I would then pass this to one last call to get a list of torrents that fulfill the first requirement, don't share a path with a torrent that may or may not also fulfill those requirements like so, and perform an action on them.

#Assuming the results of the previous called are stored in a variable named PATHS2
rtcontrol path=$PATH2 --cull

I have tried different combinations of uniq, sed and awk as well as using pipes to pass along output. It should be noted that rtcontrol output can be piped out e.g. rtcontrol name="*Test* -qoname|uniq -u

I hope this is not confusing, and there is probably multiple ways to handle it. I'm just looking for the most efficient and flexible way to achieve it. I'm not sure it can be done in any less than the 3 main steps, due to what arguments the program can take, and what it can output. The specifics of each step and handling the output and input are what has me stumped.

This can be handled in pure bash, or with Python. There are also python libraries to interface with the torrent program that can perform similar functions, and even more advanced things that I am investigating.

Aucun commentaire:

Enregistrer un commentaire