You are not logged in.

#2051 2013-04-16 16:31:14

Awesoham
Member
Registered: 2013-04-09
Posts: 4

Re: Post your handy self made command line utilities

illusionist wrote:

@awesoham
Why do you want us to remove our lock file ?

@illusionist
I often find myself having to delete db.lck as Yaourt says "Pacman is busy!" It sometimes happens when I exit Pacman with a Ctrl-C.

Offline

#2052 2013-04-16 17:44:39

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,506
Website

Re: Post your handy self made command line utilities

Awesoham wrote:

I often find myself having to delete db.lck as Yaourt says "Pacman is busy!" It sometimes happens when I exit Pacman with a Ctrl-C.

This concerns me. I use CTRL-C with pacman (and pacaur) often and I have never ever ever needed to delete the "db.lck" file. If you have to do that often, you may want to look into what the problem is. yikes

Last edited by drcouzelis (2013-04-16 17:45:45)

Offline

#2053 2013-04-16 17:55:58

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,039
Website

Re: Post your handy self made command line utilities

I don't know why one would 'regularly' have to interupt pacman with Ctrl-C - that's just a bad idea in the first place.  But if it is done while the database is (failing to) sync, then there is not yet a lock file.  This is the only time it seems reasonable to break it ... you don't want to wait for the network timeout, but a Ctrl-C and restart will work on some crappy networks (my university).

But if you Ctrl-C while it is actually updating packages, then there will be a lock file.  But this goes back to the beginning: why are you regularly interupting your package manager?


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#2054 2013-04-17 13:02:19

illusionist
Member
From: localhost
Registered: 2012-04-03
Posts: 498

Re: Post your handy self made command line utilities

Awesoham wrote:
illusionist wrote:

@awesoham
Why do you want us to remove our lock file ?

@illusionist
I often find myself having to delete db.lck as Yaourt says "Pacman is busy!" It sometimes happens when I exit Pacman with a Ctrl-C.

Do you know that db.lck is there for a reason ?


  Never argue with stupid people,They will drag you down to their level and then beat you with experience.--Mark Twain
@github

Offline

#2055 2013-04-17 18:03:37

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 19,092
Website

Re: Post your handy self made command line utilities

Ok, we are done with the lock thing; please let this thread (of all the threads on these boards) get back on the rails...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Online

#2056 2013-04-19 06:53:46

krakenfury
Member
Registered: 2013-04-18
Posts: 5

Re: Post your handy self made command line utilities

Came up with this today:

alias taur='tar -xvzf'

Thought it was clever. smile

Offline

#2057 2013-04-20 21:50:44

ResXaoC
Member
From: Nibiru
Registered: 2011-07-17
Posts: 15

Re: Post your handy self made command line utilities

Clone mirror using S-Lang.

#!/usr/bin/env slsh

require("socket");

private variable local = "/var/archrepo/";  % // # without trailing "/", last part will be used as prefix
private variable mirror = "http://mirror.nl.leaseweb.net/archlinux/";
private variable repo = ["core", "extra", "community", "multilib"];
private variable os = ["i686", "x86_64"];
private variable tw = 80;

public variable S_IFDIR = 0040000;
public variable S_IFREG = 0100000;

static define put_out(str) { () = fprintf(stdout, "%s", str); }
static define put_err(str) { () = fprintf(stderr, "\n\e[1;31mERR\e[0m %s\n", str); }

static define b2d(str, base) % // # base < 37
{
	str = strlow(str);
	variable a = [1, 0, 0, 0, strlen(str)];
	variable b = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
		      'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
		      'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
	loop (a[4])
	{ 
		while (a[2] != str[a[4] - a[0]]) { a[2] = b[a[1]]; a[1]++; } a[1]--;
		a[3] = a[3] + (a[1] * int(base ^ (a[0] - 1))); a[1] = 0; a[2] = 0; a[0]++;
	}
	return a[3];
}

static define devide_url(url)
{
	variable port, host = "", path = "", a = strchop(url, ':', 0), b;
	switch (a[0]) { case "ftp" : port = 21; } { case "http" : port = 80; }
	b = strchop(a[1][[2:]], '/', 0); host = b[0];
	path = strcat("/", a[1][[3 + strlen(host):]]);
	return port, host, path;
}

static define fill(str, chr, len, dir)
{
	variable rstr = str;
	while (strlen(rstr) < len) { if (dir) rstr = strcat(chr, rstr); else rstr = strcat(rstr, chr); }
	return rstr;
}

static define shorten_name(str, len)
{
	variable a = [0, 0, strlen(str)], b; len++;
	if (a[2] > len)
	{
		while (a[0] != '.') { a[1]++; a[0] = str[a[2] - a[1] - 1]; if (a[1] + 2 > ceil(len / 2.0)) break; }
		b = sprintf("%s..%s", str[[0:len - a[1] - 3]], str[[a[2] - a[1]:]]);
	}
	else return str;
	return b;
}

static define shorten_size(size) % // # http://en.wikipedia.org/wiki/Binary_prefix
{
	variable a = ["B ", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"], b = 0;
	while (size > 9999) { size = double(size) / 1024; b++; }
	return sprintf("%.1f%s", size, a[b]);;
}

static define r_str(file)
{
	variable str, f = fopen(file, "r");
	if (f == NULL) return "-1"; else { () = fgets(&str, f); () = fclose(f); return str; }
}

static define file_size(url)
{
	variable port, host, path; (port, host, path) = devide_url(url);
	variable r = -1, size = -1;
	variable cl = "Content-Length:";
	variable str = "", c = " ";
	variable sp = socket(PF_INET, SOCK_STREAM, 0); connect(sp, host, port);
	variable buf = sprintf("HEAD %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
	if (write(sp, buf))
	{
		while (c[0] != '\n') { () = read(sp, &c, 1); str = strcat(str, c); }
		r = atoi(strchop(str, ' ', 0)[1]);
		if (r == 200)
		{
			str = "";
			while (read(sp, &c, 1))
			{
				if (c[0] == '\n')
				{
					if (strlen(str) > 0)
					{
						str = strchop(str, ' ', 0);
						if (str[0] == cl) { size = atoi(str[1]); break; }
					}
					else break;
					str = "";
				}
				else if (c[0] != '\r') str = strcat(str, c);
			}
		}
	}
	if (sp != NULL) () = close(sp);
	return r, size;
}

static define download_file(url, dst, pre, len)
{
	variable a, b, d, e;
	variable port, host, path;
	variable sp, fp, buf, s_size, s_speed, r = -1, size = -1, chunked = 0, err;
	variable te_chunked = "Transfer-Encoding: chunked";
	variable str = "", c =" ", msg = "", msg_len;
	variable t = [0, 0, 0];
	variable frag = 1024, wait = 5;
	if (pre != NULL) variable pre_len = strlen(pre);
	(r, size) = file_size(url); if (r != 200) return r;
	(port, host, path) = devide_url(url);
	buf = sprintf("GET %s HTTP/1.1\nHost: %s\n\n", path, host);
	sp = socket(PF_INET, SOCK_STREAM, 0);
	if (sp != NULL)
	{
		forever { try (err) { connect(sp, host, port); break; } catch SocketError: { continue; } }
		if (write(sp, buf))
		{
			fp = open(dst, O_WRONLY | O_CREAT | O_TRUNC);
			if (fp != NULL)
			{
				while (read(sp, &c, 1))
				{
					if (c[0] == '\n')
					{
						if (strlen(str) == 0) break;
						else if (str == te_chunked) chunked = 1;
						str = "";
					}
					else if (c[0] != '\r') str = strcat(str, c);
				}
				if (chunked)
				{
					c = ""; str = "";
					while (c[0] != '\n')
						{ () = read(sp, &c, 1); if (c[0] != '\r' && c[0] != '\n') str = strcat(str, c); }
					size = b2d(str, 16);
				}
				if (pre != NULL)
				{
					s_speed = "", t[1] = 0, t[2] = 0, t[0] = _time();
					if (size > 0) s_size = fill(shorten_size(size), " ", 8, 1); else s_size = "";
				}
				c = 0;
				while (c < size)
				{
					d = read(sp, &buf, frag);
					if (d == 0) { put_err(dst); () = close(sp); () = close(fp); exit(0); }
					c = c + d;
					() = write(fp, buf);
					if (pre != NULL)
					{
						t[1] = _time() - t[0];
						if (t[1] > wait)
						{
							if (t[1] - t[2] > 0) 
								s_speed = fill(strcat(shorten_size(c / t[1]), "/s"), " ", 10, 1);
							t[2] = t[1];
						}
						msg = sprintf("%s %s %s", s_speed, fill(shorten_size(c), " ", 8, 1), s_size);
						msg_len = strlen(msg);
						if (len - pre_len > msg_len) msg = fill(msg, " ", len - pre_len - 1, 1);
						if (pre_len + msg_len > len - 1) pre = shorten_name(pre, len - msg_len - 2);
						put_out(sprintf("\r%s %s", pre, msg));
					}
					if (c == size) break; else if (size - c < frag) frag = size - c;
				}
				if (pre != NULL)
				{
						put_out(sprintf("\r%s%s", pre, fill(string(s_size), " ", len - pre_len, 1)));
				}
				() = close(fp);
			}	
		}
		() = close(sp);
	}
	return r;
}

static define fetch_list(file, pre, len)
{
	variable a, b, c, d, e, f, g = {""}, msg;
	variable fp = open(file, O_RDONLY);
	if (pre != NULL) variable pre_len = strlen(pre);
	if (fp != NULL)
	{
		a = " "; b = "<a href=\""; c = "\""; d = strlen(b); e = 0; f = 0; g[f] = "";
		while (read(fp, &a, 1))
		{
			if (a[0] != '\n')
			{
				switch (a[0])
				{ case b[e] : e++; }
				{
					case c[0] : if (d == e)
					{
						f++; list_insert(g, "", f); e = 0;
						if (pre != NULL)
						{
							msg = fill("Fetching: ", " ", 9, 1);
							msg = sprintf(" %s%s", msg, fill(string(f), " ", 4, 1));
							msg = fill(msg, " ", len - pre_len, 1);
							put_out(sprintf("\r%s%s", pre, msg));
						}
					}
				}
				{ if (e == d) g[f] = strcat(g[f], a); else if (a[0] != b[e]) e = 0;  }
			}
		}
	}
	if (close(fp) == 0) () = remove(file);
	d = 0; f--; e = String_Type[f]; loop(f) { e[d] = g[d + 1]; d++; };
	return e;
}

define slsh_main()
{

	variable ml = sprintf("%slastsync", mirror);
	variable ll = sprintf("%slastsync", local);
	variable lm = sprintf("%smastsync", local);

	variable a, b, c, d, e, f, g, h, i, s;
	variable msg, msg_len, files, count, in, out, sym, rel, err;

	if (download_file(ml, lm, NULL, 0) == 200 && r_str(lm) > r_str(ll))
	{
		foreach a (os)
		{
			foreach b (repo)
			{
				if (not(a == "i686" && b == "multilib"))
				{
				 	c = sprintf("%s/os/%s/", b, a);
				 	d = sprintf("%s%s-%s.list", local, b, a);
				 	msg = sprintf("Trying to obtain file-list from %s:%s...", a, b);
				 	msg_len = strlen(msg);
					if (download_file(strcat(mirror, c), d, msg, tw) == 200)
					{
						
						files = fetch_list(d, msg, tw); count = length(files);
						if (b == repo[0] || b == repo[1]) d = "packages"; else d = "community";
						e = [sprintf("%spool/%s/", local, d), strcat(local, c)];
						d = [strcat(mirror, c), sprintf("../../../pool/%s/", d)];
						put_out(sprintf("\r%s%s\n┯\n", msg, fill("Done.", " ", tw - msg_len, 1)));
						for (c = 0; c < count; c++)
						{
							msg = sprintf("└─ %s %s", fill(string(c + 1), " ", 4, 1), files[c]);
							msg_len = strlen(msg);
							 in = strcat(d[0], files[c]);
							out = strcat(e[0], files[c]);
							rel = strcat(d[1], files[c]);
							sym = strcat(e[1], files[c]);
							foreach f (e)
							{
								g = 0; h = ""; i = strchop(f, '/', 0);
								while (stat_file(f) == NULL)
								{
									switch (h[g])
									{ case "" : if (g == 0) { h = "/"; g++; } }
									{ h = sprintf("%s%s/", h, i[g]); g++; }
									() = mkdir(h);
								}
							}
							f = stat_file(out);
							if (f != NULL)
							{
								if (f.st_mode & S_IFDIR)
								{
									if (not(rmdir(out)))
										{ put_err(strcat("rmdir ", out)); exit(1); }
								}
								else
								{
									if (f.st_mode & S_IFREG)
									{
										if (msg_len - tw < 3) msg = shorten_name(msg, tw - 4);
										s = sprintf("%s%s", msg, fill("- ", " ", tw - msg_len, 1));
										put_out(strcat("\r", s));
										if (not(f.st_mode & S_IWUSR))
											{ put_err(strcat("Can't write to ", out)); exit(1); };
										(g, h) = file_size(in);
										if (g == 200 && h == f.st_size) continue;
									}
								}
							}
							if (download_file(in, out, msg, tw) == 200)
							{
								s = stat_file(sym);
								if (s != NULL)
								{
								 	if (remove(sym) != 0)
								 	{
								 		msg = errno_string(errno);
								 		msg = sprintf("remove %s\n%s", out, msg);
								 		put_err(msg); exit(1);
								 	}
								}
								if (b == files[c][[0:strlen(b) - 1]])
								{
									if (rename(out, sym) != 0)
									{
										msg = errno_string(errno);
										msg = sprintf("rename %s > %s\n%s", out, sym, msg);
										put_err(msg); exit(1);
									}
								}
								else
								{
									if (symlink(rel, sym) != 0)
									{
										msg = errno_string(errno);
										msg = sprintf("symlink %s > %s\n%s", rel, sym, msg);
										put_err(msg); exit(1);
									}
								}
							}
						}
						msg = fill(sprintf("(%i files)", count), " ", tw, 0);
						put_out(sprintf("\r└─ Done. %s\n", msg));
					}
				}
			}
		}
		in = strcat(local, "mastsync"); out = strcat(local, "lastsync");
		if (rename(in, out) != 0)
		{
			msg = errno_string(errno);
			msg = sprintf("rename %s > %s\n%s", in, out, msg);
			put_err(msg); exit(1);
		}
	}
	else put_out("Is up to date.\n");
}

Last edited by ResXaoC (2013-05-11 14:24:22)

Offline

#2058 2013-04-21 14:39:13

Roken
Member
From: UK
Registered: 2012-01-16
Posts: 674

Re: Post your handy self made command line utilities

I've changed my fmusic script to accomodate changes to tracker and expand it a little. Now it takes as parameters:

None: Just search for matching tunes
A number: Play the corresponding track in the results
r: Play a random track from the results
rq: Play a random track from the results and don't display the results

The three parameters are, of course, mutually exclusive

Syntax:

fmusic searchterm (n,r,rq)

Fixed:

Better error handling
If the track name contains escaped/hex codes instead of characters they are converted
The results list is more human readable

Now uses "play" rather than mpg123. I found mpg123 had problems if the music was in a linked folder.

NB. If you don't use pulseaudio you can/should discard the bit at the end that restarts it.

The script:

#/!bin/bash
IFS="
"
TITLE=$1
PLAY=$2
file="/dev/shm/foundfiles"
dispfile="/dev/shm/foundmusic"

#Get music list
tracker-search -l 500 --disable-color -m $TITLE | sed -n -s '/file/p' | sed 's/file\:\/\/\///g'| sed 's/^[ \t]*\(.*\)/\/\1/' | sed -e 's/\%20/\\ /g' > $file
if [ `wc -l $file | awk '{printf $1}'` = 0 ]; then
	rm $file
	echo "Nothing found"
	exit
fi
sed -i '1,1d' $file
sed -i '$d' $file
FOUND=`wc -l $file | awk '{print $TITLE}' | awk '{print $1}'`
if [ $FOUND = 0 ]; then
	rm $file
	echo "Nothing found"
	exit
fi


#Create numbered list to display
prefix=1


#Clean up the files
while read -r line
do
	printf "${line//%/\x}\n"
done < $file > /dev/shm/foundfiles1

rm $file
file="/dev/shm/foundfiles1"

while read -r line
do
	newline="${prefix} $line"
	let prefix=$prefix+1
	echo $newline
done < $file > $dispfile
sed -i 's/\\//g' $dispfile
sed -i 's/\\//g' $file

#Display results
if [ ! $PLAY = "rq" ]; then
	cat $dispfile
fi
echo "Found $FOUND results"
#If set to play, play tune
if [ ! -z $PLAY ]; then
	if [ $PLAY = "r" ] || [ $PLAY = "rq" ]; then
# 		Random song selected
		PLAY=`echo "scale=6; $RANDOM/32767*$FOUND" | bc | sed 's/\..*//'`
	fi
	if [ $PLAY -gt $FOUND ]; then
		echo "Cannot play - not enough results"
	else
		song=`head -n$PLAY $file | tail -n1`
		play "$song"
	fi
fi

#Pulseaudio sometimes crashes - if it has, restart it
if [ ! `ps -ef | grep pulseaudio | grep -v grep` ]; then
	pulseaudio --start > /dev/null
fi

#cleanup
rm $dispfile
rm $file

[img=Speedtest]http://www.speedtest.net/my-result/5145583518[/img]

Nvidia GTX 670 2Gb, AMD Phenom II X4 (965BE) @ 3.6 Ghz (Overclocked) 8GB RAM
Linux user #545703

Offline

#2059 2013-04-22 12:41:43

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

hey guys, I wrote a script to download torrents from piratebay :-) but I am not sure if it's ok to post that here as it's bad ( you know what I am talking about ;-) ). Could a mod please confirm if I can post the script here ?

Last edited by vik_k (2013-04-22 12:42:57)


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2060 2013-04-22 13:40:32

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

vik_k wrote:

hey guys, I wrote a script to download torrents from piratebay :-) but I am not sure if it's ok to post that here as it's bad ( you know what I am talking about ;-) ). Could a mod please confirm if I can post the script here ?

See the Forum Etiquette.

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2061 2013-04-22 14:12:54

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

thanks, that's why I needed a clarification.


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2062 2013-04-22 14:41:33

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,073

Re: Post your handy self made command line utilities

vik_k wrote:

hey guys, I wrote a script to download torrents from piratebay :-) but I am not sure if it's ok to post that here as it's bad ( you know what I am talking about ;-) ). Could a mod please confirm if I can post the script here ?

If you know its illegal or breaking any laws, don't post it here.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#2063 2013-04-22 15:02:06

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

^^ well see, that's the issue. the script itself is not illegal in any way (afaik). it just downloads torrents from piratebay, there are legal torrents too there.
I guess you can understand my confusion, don't want to get into any controversy but just share my work. pls advice.


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2064 2013-04-22 15:39:02

theGunslinger
Member
Registered: 2011-05-20
Posts: 299

Re: Post your handy self made command line utilities

If it lets you choose the torrents, then it's ok to post it. If it automatically downloads known illegal stuff... Keep it to yourself.

Offline

#2065 2013-04-22 15:45:05

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 13,167

Re: Post your handy self made command line utilities

BitTorrent is a fine tool that, like many tools, can be used to do bad things.  I've no issues with BitTorrent implementations and helper utilities. 

Now, as to PirateBay.  Please be extremely careful here; there may be legitimate things indexed there.  There is no question, however, that they are enablers when it comes to helping others violate copyright restrictions.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
You assume people are rational and influenced by evidence.  You must not work with the public much. -- Trilby
----
How to Ask Questions the Smart Way

Offline

#2066 2013-04-22 16:16:32

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

there is a lot of confusion here (and for me), I am just gonna go ahead and post the code. Mods, pls decide if it can stay or not. (sorry if it turns out not good)
(requires aria2c)

Usage

pb 'pattern'

Output will be a list of torrents matching the pattern, seeders sorted.

press n<enter> to go to the next page list
press b<enter> to go to the previous page list
press <enter> to exit from script
provide a number or a comma separated list of number to download

Code

#!/usr/bin/bash
#
# v-k 2013
#
###
            
errorout() { 
  echo "error: $*" >&2; 
  echo
  [[ -e "$tmp_dir" ]] && rm -r "$tmp_dir";
  exit 1;
}

message() { 
  echo "usage: ${0##*/} pattern"; exit 1; 
}

search_pb() {
  local str cntr
  str="$1"
  cntr=$2

  [[ -f "$tmp_dir/tpb.1.test.$cntr" ]] || curl -s "http://thepiratebay.se/search/${str// /%20}/$cntr/7/0" > "$tmp_dir/tpb.1.test.$cntr"
  sed -n '/class="detName"/ {p;n;n;p;n;p;n;n;p;n;p;}' "$tmp_dir/tpb.1.test.$cntr" > "$tmp_dir/tpb.2.test"
  sed -n '/class="detName"/ s/.*>\(.*\)<\/a>/\1/p; n; s/^<a href="\(.*80\)" .*/\1/p; n; s/.*Uploaded \(.*\)\&nbsp\;\(.*\), Size \(.*\)\&nbsp\;\(.*\),.*/\1 \2|\3\4/gp; n; s/[^0-9]//gp; n; s/[^0-9]//gp;' "$tmp_dir/tpb.2.test" > "$tmp_dir/tpb.result"
  sed -i '1,${N;N;N;N;s/\n/|/g}' "$tmp_dir/tpb.result"
}

display_res() {
  width=$(cut -d"|" -f1 "$tmp_dir/tpb.result" | wc -L)
  divider="$(printf "%-$(($width+53))s" " ")"
  i=1
  printf "\n $(tput bold)$(tput setaf 3)%2.2b | %-${width}.${width}s | %11.11b | %9.9b | %8.8b | %6.6b |$(tput sgr0) \n" "# " "NAME (Pg #$((pg_cntr+1)))" "DATE" "SIZE" "SEEDS(!)" "LEECHS"
  echo "${divider// /=}+"
  while read NAME LINK DATE SIZE SEED LEECH; do
    [[ ${#DATE} -ne 11 ]] && DATE="${DATE// /  }"
    printf " %2.2b | %-${width}.${width}s | %11.11b | %9.9b | $(tput setaf 2)%8.8b$(tput sgr0) | $(tput setaf 1)%6.6b$(tput sgr0) | \n" "$i" "$NAME" "$DATE" "$SIZE" "$SEED" "$LEECH"
    ((i++))
  done < "$tmp_dir/tpb.result"
  echo "${divider// /=}+"
}

download() {
  IFS=","
  for i in $sel; do
    link="$(head -n $i "$tmp_dir/tpb.result" | tail -n 1 | cut -d"|" -f2)"
    torr="$(head -n $i "$tmp_dir/tpb.result" | tail -n 1 | cut -d"|" -f1)" 

    echo $link >> "$tmp_dir/links"
  done
  IFS="$IFS_BAK"
}

# main starts here
[[ -f /usr/bin/aria2c ]] || errorout "aria2c is needed for getting metadata from magnet links"

[[ $# -eq 1 ]] || message
tmp_dir="$(mktemp -d)"
cmd_arg="$1"
pg_cntr=0

while true; do
  clear

  search_pb "$cmd_arg" $pg_cntr

  IFS_BAK="$IFS"
  IFS="|"
  display_res
  IFS="$IFS_BAK"

  echo
  echo -n "Enter selection: "
  read sel

  [[ -z $sel ]] && errorout "Invalid/Empty choice !! exiting !!!"
  [[ $sel = n ]] && { ((pg_cntr++)); continue; }
  [[ $sel = b ]] && { ((pg_cntr--)); continue; }

  download

  echo 

  aria2c -q --bt-metadata-only --bt-save-metadata -i "$tmp_dir/links" &
  pid=$!

  while kill -0 $pid &> /dev/null; do
    printf "[.   ] Downloading torrents !!\r"
    sleep .4s
    printf "[ .  ] Downloading torrents !!\r"
    sleep .4s
    printf "[  . ] Downloading torrents !!\r"
    sleep .4s
    printf "[   .] Downloading torrents !!\r"
    sleep .4s
    printf "[  . ] Downloading torrents !!\r"
    sleep .4s
    printf "[ .  ] Downloading torrents !!\r"
    sleep .4s
  done

  printf "[DONE] Downloading torrents !!\n\n"
  break
done

rm -r "$tmp_dir" 

Screenshot


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2067 2013-04-22 16:22:41

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,506
Website

Re: Post your handy self made command line utilities

vik_k wrote:

(requires aria2c)

Does this script use aria2 to convert the magnet links into torrent files? yikes

Offline

#2068 2013-04-22 16:27:53

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

drcouzelis wrote:
vik_k wrote:

(requires aria2c)

Does this script use aria2 to convert the magnet links into torrent files? yikes

yup. I tried one python script also to do that, got it from somewhere on the internet but it was calculating the checksum wrong and hence the torrents were not getting registered on the tracker and so, not getting any progress. Thus shifted to aria2c.

And, actually rtorrent is sometimes very slow in getting metadata from torrent links. sad

Last edited by vik_k (2013-04-22 16:30:31)


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2069 2013-04-22 17:10:43

theGunslinger
Member
Registered: 2011-05-20
Posts: 299

Re: Post your handy self made command line utilities

vik_k wrote:
drcouzelis wrote:
vik_k wrote:

(requires aria2c)

Does this script use aria2 to convert the magnet links into torrent files? yikes

yup. I tried one python script also to do that, got it from somewhere on the internet but it was calculating the checksum wrong and hence the torrents were not getting registered on the tracker and so, not getting any progress. Thus shifted to aria2c.

And, actually rtorrent is sometimes very slow in getting metadata from torrent links. sad

I'm really glad you decided to post it. It's by far the best one I've tried so far. Going to use it from now on thanks!

Offline

#2070 2013-04-22 17:54:49

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

theGunslinger wrote:

I'm really glad you decided to post it. It's by far the best one I've tried so far. Going to use it from now on thanks!

I am happy you loved it smile


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2071 2013-04-22 20:26:57

siepet
Member
Registered: 2012-11-16
Posts: 2

Re: Post your handy self made command line utilities

Hello, it's my first script that does something usefull, i made it so my mother could scan files by just clicking on icon on the desktop and it's pretty cool, at least for me smile

#! /bin/bash
# Simple script for scanning
# Author: siepet7 at gmail dot com

# DEVICE URI FROM HP-SCAN
DEV="hpaio:/net/Deskjet_3520_series?ip=192.168.0.12"

# DESTINATION FOLDER
DES="/home/$USER/Scan/"

# CHECKS IF THE FOLDER /home/$USER/Scan exists
if [ ! -d "$DES" ]
	then
	mkdir /home/$USER/Scan
fi
# COLOR MODE FOR SCANNING
# MODES: color, gray, lineart
MOD="color"

# FILE NAME
NAME="Img-$(date +%e-%m-%y-%S).jpg"

	scan () {
		 hp-scan -d "$DEV" -m "$MOD" -o "$DES""$NAME" > /dev/null 2>&1  && fecho && thunar "$DES"
}
trap scan EXIT


fecho(){
echo
echo "Scanning file is done!"
echo "Destination folder: "$DES""
echo "Filename: "$NAME""
echo
}

Offline

#2072 2013-04-23 02:16:56

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,506
Website

Re: Post your handy self made command line utilities

vik_k wrote:

I wrote a script to download torrents...

And thanks to your script, I now have a handy self made command line utility. big_smile

Convert magnet links to torrent files:

alias magnet-to-torrent="aria2c -q --bt-metadata-only --bt-save-metadata"

("-q" is for quiet mode)

This would have been so useful a few months ago! I would have been able to find a magnet link on the web browser on my phone, SSH into my computer at home, convert the link into a torrent file at the command line, and then use rtorrent to begin downloading it remotely.

Offline

#2073 2013-04-23 06:46:08

ResXaoC
Member
From: Nibiru
Registered: 2011-07-17
Posts: 15

Re: Post your handy self made command line utilities

siepet wrote:

Hello, it's my first script that does something usefull, i made it so my mother could scan files by just clicking on icon on the desktop and it's pretty cool, at least for me smile

Proud of it, really?!

Last edited by ResXaoC (2013-04-23 06:46:24)

Offline

#2074 2013-04-23 07:18:27

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

drcouzelis wrote:

Convert magnet links to torrent files:

alias magnet-to-torrent="aria2c -q --bt-metadata-only --bt-save-metadata"

("-q" is for quiet mode)

This would have been so useful a few months ago! I would have been able to find a magnet link on the web browser on my phone, SSH into my computer at home, convert the link into a torrent file at the command line, and then use rtorrent to begin downloading it remotely.

My setup is almost same. From phone I send magnet links to pc but if I am on pc itself, then this handy script converts magnet links to torrent files and puts them in rtorrent watch directory. smile


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#2075 2013-04-23 15:54:03

jgreen1tc
Member
From: St. Louis
Registered: 2011-05-16
Posts: 247

Re: Post your handy self made command line utilities

tlawson wrote:

this is a little chunk of code to extract the next window ID in line, after the current window, that matches the name you pass to it.  requires wmctrl.

#!/bin/bash

ACTIVE=`xprop -root _NET_ACTIVE_WINDOW | awk '{print $5}' | sed 's/0x//' | tail -1`
FIRST=`wmctrl -l -x | grep $1 | awk '{print $1}' | head -1`
AFTER=`wmctrl -l -x | grep $1 | awk '{print $1}' | grep -A 1 $ACTIVE | head -2 | tail +2`
echo $AFTER $FIRST | awk '{print $1}'

then you bind a key (mine is W-x) to the command

bash -c "wmctrl -i -a `windowafter urxvt` || urxvt"

and pressing W-x cycles through all instances of urxvt, or opens a new one if none exists.  binding several (editor+urxvt+browser) = no more hunting with alt-tab trying to figure out if the right terminal was 2, 3, 4 windows ago.

also handy to bind W-C-x to force a new terminal open.

Ah man, if only Ratpoison had support for wmctrl... this is exactly what I need. I might use it for when I tmpwm into Openbox.

Offline

Board footer

Powered by FluxBB