You are not logged in.

#551 2009-09-29 06:12:26

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

-

Last edited by dmz (2009-09-29 06:13:40)

Offline

#552 2009-09-29 06:13:24

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

scj wrote:
dmz wrote:

If some dude have a solution for this part;

    NAME="$(echo $GENRE|tr A-Z a-z|sed 's/[Pp]sychadelic/psychedelic/').m3u"

I'd be very happy to hear about it since my solution is terrible (the sed part).

Fix your tags.

#!/bin/bash

library=$HOME/media/audio/Music
IFS=$'\n'
for file in $(mpc list filename genre Psychadelic); do
  id3 -2v -g Psychedelic "$library/$file"
done
mpc update

The script uses id3 (in [extra]) to set the id3v2 genre tag and update the mpd library.

No, that'll break the checksums.

Offline

#553 2009-09-29 07:55:57

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

movietime - disable powersaving (screensaver, dpms, suspend) to watch a movie in KDE 4.

#!/bin/bash
# movietime - disables display power management to watch movies (KDE 4).

# Tests for X server blanking / Monitor blanking
dpmstest=$(xset -q | grep "  DPMS is Enabled")

# Save dpms values
xset -q | grep -o "Standby: [0-9]*[0-9]" | sed -e "s/Standby: //" \
> /tmp/dpmsvalues
xset -q | grep -o "Suspend: [0-9]*[0-9]" | sed -e "s/Suspend: //" \
>>/tmp/dpmsvalues
xset -q | grep -o "Off: [0-9]*[0-9]" | sed -e "s/Off: //" >> /tmp/dpmsvalues
sed -i -e ':b;N;s/\n/ /;bb' /tmp/dpmsvalues # replace newlines with spaces

if [[ -n "$dpmstest" ]]; then
  # Turn off X blanking, display power management (also disables screensaver)
  xset s off; xset -dpms
  # Turn off X blanking, turn off display after three hours
  # xset s off; xset dpms 0 0 10800
  # Inhibit suspend
  echo '#!/bin/bash'  >  /tmp/inhibit-suspend
  echo 'while :'      >> /tmp/inhibit-suspend
  echo 'do'           >> /tmp/inhibit-suspend
  echo 'qdbus org.freedesktop.PowerManagement /org/freedesktop/PowerManagement \
  org.freedesktop.PowerManagement.Inhibit.Inhibit "movieview" "Playing movie"' \
                      >> /tmp/inhibit-suspend
  echo 'sleep 119'    >> /tmp/inhibit-suspend
  echo 'done'         >> /tmp/inhibit-suspend
  chmod u+x /tmp/inhibit-suspend
  nohup "/tmp/inhibit-suspend" &> /dev/null &
  echo " - Disabled screensaving, and suspend"; else
  # Resume display power management
  xset +dpms
  # Resume display power management with previous values
  # if [ -f /tmp/dpmsvalues ]; then
  #   xset dpms `cat /tmp/dpmsvalues` && rm /tmp/dpmsvalues
  # fi
  pkill inhibit-suspend
  echo " + Enabled screensaving, and suspend"
fi

# Notes:
#  On resume X blanking is ignored.

Last edited by Gen2ly (2009-09-29 07:58:06)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#554 2009-09-29 14:36:34

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

vik_k wrote:

first thing first, I'm not that much good in scripting like you people & I found above script much useful.
but if u have got "[" in your song title then it adds that song again to the good songs list.
so i am proposing a way to avoid this & might be there is another way to achieve same.
changes only in the "case" statement where script gets called without argument ie adding songs

*)          mpc | grep -q playing || exit 1
              song="$(mpc | head -n1)"
              tmp="$(echo "$song" | sed -e 's/\[/\\[/g')"
              grep -qx "$tmp" "$list" || echo "$song" >> "$list" ;;

hey thanks for pointing out this issue.  tho, i've been finding recently that bash is perfectly fine for something like this, no sed needed!

here's my fix:

grep -qx "${song//[/\\[}" "$list" || echo "$song" >> "$list" ;;

you can see how it works here:

> echo $testword
Some Artist - Some [obscure] track
> echo ${testword//[/\\[}
Some Artist - Some \[obscure] track

might need to escape other regex characters too huh? i guess i've just been lucky so far.

Last edited by brisbin33 (2009-09-29 15:38:06)

Offline

#555 2009-09-29 15:00:14

scj
Member
From: Sweden
Registered: 2007-09-23
Posts: 158

Re: Post your handy self made command line utilities

dmz wrote:
scj wrote:
dmz wrote:

If some dude have a solution for this part;

    NAME="$(echo $GENRE|tr A-Z a-z|sed 's/[Pp]sychadelic/psychedelic/').m3u"

I'd be very happy to hear about it since my solution is terrible (the sed part).

Fix your tags.

#!/bin/bash

library=$HOME/media/audio/Music
IFS=$'\n'
for file in $(mpc list filename genre Psychadelic); do
  id3 -2v -g Psychedelic "$library/$file"
done
mpc update

The script uses id3 (in [extra]) to set the id3v2 genre tag and update the mpd library.

No, that'll break the checksums.

Fix your checksums.

If you got bad metadata, the correct solution is always to correct it.

Offline

#556 2009-09-29 16:23:21

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

Re: Post your handy self made command line utilities

grep -qx "${song//[/\\[}" "$list" || echo "$song" >> "$list" ;;

you can see how it works here:

> echo $testword
Some Artist - Some [obscure] track
> echo ${testword//[/\\[}
Some Artist - Some \[obscure] track

might need to escape other regex characters too huh? i guess i've just been lucky so far.

thanks for that change, It saved that extra variable.
will checkout if there are other issues.

btw I've checked your other scripts too & they are awesome


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

Offline

#557 2009-09-29 20:17:07

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: Post your handy self made command line utilities

I wonder if someone could help me make a little script that prompts "yes/no" before pasting in shell, if the clipboard has >x number of lines??

Offline

#558 2009-09-29 20:18:00

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

That doesn't sound too hard. Use xclip + wc + read/dialog

Offline

#559 2009-09-30 09:06:10

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Here you go, colbert.  Haven't tried it:

#!/bin/bash
# clippaste - Paste the contents of the clipboard to shell output

# Variables

# paste command (middle and right click clipboards)
#paste=$(xclip -out -selection primary)
paste=$(xclip -out -selection clipboard)

# count number of characters
clipcount=$($paste | wc -c)

# character limit
charlim=160

# paste clipboard output only if below character limit
if [ $clipcount -le $charlimit ]; then
  $paste; else
  echo " Output is over two lines"
  exit
fi

Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#560 2009-09-30 19:11:01

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: Post your handy self made command line utilities

Thanks Gen2ly! I tried it and got:

└─> ./clippaste
./clippaste: line 11: #!/bin/bash: No such file or directory
./clippaste: line 17: [: 0: unary operator expected
Output is over two lines!

Offline

#561 2009-09-30 20:10:57

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Make charlim and charlimit match each other, and double quote the variables inside [

Offline

#562 2009-09-30 23:32:37

nightchill
Member
Registered: 2009-03-25
Posts: 16

Re: Post your handy self made command line utilities

I'm thinking of putting this into AUR, but still not sure should I do that since it's a relatively small script.

In short, it's a cli script that communicates with twitter via api and posts your tweets. It also has gui mode of work.

clitter

#!/bin/bash

# Name: clitter v0.2
# Author: Night <nightcooki3[at]gmail[dot]com>
# Contributor(s): onemyndseye
# License: GPL v2+
# Description: A bash script for Twitter that utilizes cURL
# Dependencies: curl, dialog, zenity

# Debug
#set -x

# Source for functions
source functions.bash

# Script startup

# Check for command line arguments
while getopts "g" opt; do
  case "$opt" in

  # Only available argument for now, -g (gui)
  # Make sure you have zenity on your system
  # or the script will crash (cba to write a 
  # zenity check atm).
  g)
    printf "Using zenity instead of dialog...\n\n" >&2
    sleep 1
    dep_check
    core_zen
    goodbye_zen
    exit
    ;;

  # Invalid option > proceed as cli
  \?)
    printf "Invalid option: -$OPTARG.\n\n"
    sleep 1
    printf "Starting as cli...\n\n" >&2
    sleep 1
    ;;
  esac
done

# No option > proceed as cli
dep_check

# Load the core
core_cli

# Say goodbye
goodbye_cli
exit 0

functions.bash

#!/bin/bash
#
# Name: functions.bash
# Description: Functions used by clitter
# Author: Night <nightcooki3[at]gmail[dot]com>
# Contributor(s): onemyndseye
# License: GPL v2+
# Dependencies: dialog, cURL
# Optional dependencies: zenity

###################### DEPS
dep_check () {

printf "Welcome to clitter, a bash script utilizing cURL to communicate with Twitter\n\n"
sleep 1
printf "Please wait while the script checks for dependencies...\n\n"
sleep 1

# Check for cURL
printf "Checking for curl...\n\n"
if [ -f "$(which curl )" ]; then
  POST_METHOD=curl
else
  printf "Curl not found! Checking for wget...\n\n"
  if [ -f "$(which wget )" ]; then
    POST_METHOD=wget
  else
    printf "Curl or wget not found! Aborting...\n\n"
    exit 1
  fi 
fi

printf "Will use post method: "$POST_METHOD"\n\n"

}

###################### SCRIPT CORE

###################### CLI DEFINES
core_cli () {

# Define Log file with mktemp
LOGFILE=$(mktemp)

# Let check for account file
ACNT_FILE=~/.clitter/account
[ -f "$ACNT_FILE" ] && . "$ACNT_FILE"

# Check to see if LOGIN is set
if [ -z "$LOGIN" ]; then

  # Login info empty - Lets ask for it
  # Create dialogs and gather user info

  # Grab user input
  user=$(dialog --inputbox "Username / e-mail" 8 40 2>&1 >/dev/tty) || exit
  pass=$(dialog --passwordbox "Password" 8 40 2>&1 >/dev/tty) || exit

  # Ask to save it (dialog needs errorlevel to do this)
  dialog --yesno "Save account info?" 8 40 2>&1 >/dev/tty  
  answer=$?

  # 0 fpr yes
  if [ "$answer" -eq 0 ]; then

    # Store username & password
    store

  # 1 for no
  else

    # Dont store - just make a string
    LOGIN="$user:$pass"
  fi
fi

# Ask for tweet
tweet=$(dialog --inputbox "Tweet (max 140 characters, everything over 140 characters will be cropped to fit the max size allowed)" 8 80 2>&1 >/dev/tty) || exit


if [ "$POST_METHOD" = "curl" ]; then
  post_curl 
else
  post_wget
fi

}

###################### ZEN DEFINES
core_zen () {

# Define Log file with mktemp
LOGFILE=$(mktemp)

# Let check for account file
ACNT_FILE=~/.clitter/account
[ -f "$ACNT_FILE" ] && . "$ACNT_FILE"

# Check to see if LOGIN is set
if [ -z "$LOGIN" ]; then

  # Login info empty - Lets ask for it
  # Create dialogs and gather user info

  # Grab user input
  user=$(zenity --entry --title="Username / e-mail" --text="Your twitter username or e-mail goes in the field below") || exit
  pass=$(zenity --entry --hide-text --title="Password" --text="Your twitter password goes in the field below") || exit
  
  # Ask to save it (dialog needs errorlevel to do this)
  zenity --question --title="Save account info?" --text="If you answer yes, your username and password will be stored in a file and you will not be prompted to enter them again"  
  answer=$?

  # 0 fpr yes
  if [ "$answer" -eq 0 ]; then

    # Store username & password
    store

  # 1 for no
  else

    # Dont store - just make a string
    LOGIN="$user:$pass"
  fi
fi

# Ask for tweet
tweet=$(zenity --entry --title="Tweet" --text="Max 140 characters, everything over 140 characters will be cropped to fit the max size allowed") || exit


if [ "$POST_METHOD" = "curl" ]; then
  post_curl 
else
  post_wget
fi

}

###################### CURL
post_curl() {

# Put user info in cURL, POST HTTP request 
# and log it for the current session
## Change curl to read new LOGIN
curl --basic --user $LOGIN --data status="$tweet" http://twitter.com/statuses/update.xml > $LOGFILE 2>&1
}

###################### WGET
post_wget() {
# Put user info in wget, POST HTTP request 
# and log it for the current session

## Mangle LOGIN for wget
wget_user="$(echo $LOGIN |sed '{s|:| |}'|awk '{print $1}')"
wget_pass="$(echo $LOGIN |sed '{s|:| |}'|awk '{print $2}')"


# Use wget to post
wget --keep-session-cookies --http-user=$wget_user --http-password=$wget_pass --post-data="status=$tweet" http://twitter.com:80/statuses/update.xml >> $LOGFILE
}

###################### LOG SETTINGS
format_log() {


cat <<EOF > "$LOGFILE".tmp
Reply from twitter:
----------------------------------------------

User info:
         Name: $(cat $LOGFILE |grep "<name>" |sed '{s|<name>|| ; s|</name>||; s/^ *//;s/ *$//}')
  Screen Name: $(cat $LOGFILE |grep "<screen_name>" |sed '{s|<screen_name>|| ; s|</screen_name>||; s/^ *//;s/ *$//}')


        Tweet: $(cat $LOGFILE |grep "<text>" |sed '{s|<text>|| ; s|</text>||; s/^ *//;s/ *$//}')

    Followers: $(cat $LOGFILE |grep "<followers_count>" |sed '{s|<followers_count>|| ; s|</followers_count>||; s/^ *//;s/ *$//}')
EOF

mv "$LOGFILE".tmp "$LOGFILE"
}

###################### STORE LOGIN DATA
store () {

mkdir -p ~/.clitter
echo "LOGIN=$user:$pass" > "$ACNT_FILE"
chmod 700 "$ACNT_FILE"
. "$ACNT_FILE"

}

###################### END DIALOG
goodbye_cli() {
clear

# Reformat the log file for viewing
format_log

# Grab the log and display it in dialog
dialog --title "Status log" --textbox $LOGFILE 22 70 2>&1 >/dev/tty

# Remove the temp log
rm $LOGFILE
clear

# Print the goodbye screen
printf "\nThanks for using the script, if you didn't mistype your username and/or password you should've seen the dialog box with your tweeter account info. Report all bugs to nightcookie@gmail.com"
sleep 1

} 

###################### END ZENITY
goodbye_zen() {
clear

# Reformat the log file for viewing
format_log

# Grab the log and display it in dialog
cat $LOGFILE | zenity --title "Status log" --text-info

# Remove the temp log
rm $LOGFILE
clear

# Print the goodbye screen
zenity --title="Goodbye" --info --text="Thanks for using the script, if you didn't mistype your username and/or password you should've seen the dialog box with your tweeter account info. Report all bugs to nightcookie@gmail.com"
sleep 1

}

Needless to say, you have to have both clitter and functions.bash in same directory in order for script to work. Drop comments and suggestions on how and what to improve. smile

Offline

#563 2009-10-01 13:56:09

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

clitter *snicker*





oh come on, you were all thinking the same thing  wink

Offline

#564 2009-10-01 18:11:49

Lich
Member
Registered: 2009-09-13
Posts: 437

Re: Post your handy self made command line utilities

#!/usr/bin/env python
# -*- coding: latin-1 -*-
import urllib2, sys
if (len(sys.argv) > 1):
    page = urllib2.urlopen('http://www.rssweather.com/wx/' + sys.argv[1] + '/wx.php')
    lines = page.read()
    line = lines.find("Temperature:")
    endline = lines.find("summaryText");
    temperature = lines[line:line + (endline - line - 18)]
    temperature = temperature.replace('°F', '°F') 
    temperature = temperature.replace('°C', '°C') 
    print temperature
else:
    print 'Usage  : ' + sys.argv[0] + ' < rssweather.com url identification >'
    print 'Example: ' + sys.argv[0] + ' us/ak/anchorage'

Simple weather script. Nothing fancy, it's more like a Python primer for me (haven't done any of it in years)
Planning on writing another one that gets weather and other stats using some form of international ID (obviouslly will use another site as this is rssweather.com dependant)

Last edited by Lich (2009-10-01 18:19:23)


Archlinux | ratpoison + evilwm | urxvtc | tmux

Offline

#565 2009-10-01 21:05:25

nightchill
Member
Registered: 2009-03-25
Posts: 16

Re: Post your handy self made command line utilities

steve___ wrote:

clitter *snicker*





oh come on, you were all thinking the same thing  wink

yep, cli-twitter big_smile big_smile

Offline

#566 2009-10-02 07:50:44

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

while sleep 1 ; do echo -ne '\033]2;'`date`'\007'; done &

turns your xterm/urxvt window title into a clock


This silver ladybug at line 28...

Offline

#567 2009-10-02 23:30:00

VictorVictorVictor
Member
Registered: 2007-05-08
Posts: 5

Re: Post your handy self made command line utilities

As KDE4 is becoming more awesome and stable, there are still times when the Plasma settings get corrupted and you're forced to wipe out your plasma config and start over completely from scratch. I'm the type of person who likes to have my deskop completely littered with 15 different plasmoids including a bunch that I've built from AUR, so needless to say, I've had to do this alot.

This is a little script I wrote to back up Plasma's config files into a datestamped directory where I can easily recover any previous working configurations, should something go wrong. This is meant to be run every day, and there's instructions in the script on how to make crond run this every day for you, so you never have to worry about it.

I hope someone finds this useful! It's saved me HOURS of time and inconvenience smile

#!/bin/bash

# Set these first, according to your system:

BACKUPDIR=~/plasma-backup/                   # Where to store your backups
KDE4CONFIG_DIR=~/.kdemod4/share/config/         # Where your KDE4 config files are located

echo Plasma Backup 0.1
echo By Vikt0r Nova
echo 
echo Makes a date-stamped backup of all plasma config files,
echo so that if plasma crashes or gets corrupted, which tends
echo to happen now and again, you can easily revert back to an 
echo old configuration that works by copying the plasma files
echo back into your KDE4 config directory.
echo
echo To have cron run this script automatically, type
echo "'"crontab -e"'"
echo and enter something like this:
echo
echo "30 13 * * * /path/to/plasmaBackup.sh"
echo
echo which would run this script every day at 1:30pm
echo
echo Type  "'"man crontab"'"  for more info on using the cron daemon to automate tasks

# Copies plasma config files to a backup directory
DATE=`date +%F`
mkdir $BACKUPDIR/$DATE
cp $KDE4CONFIG_DIR/plasma* $BACKUPDIR/$DATE

# Checks to see if plasmarc now exists in the new location, assumes the others are there too, and tells you if it worked
if [ -s $BACKUPDIR/$DATE/plasmarc ]
then
    kdialog --title "Plasma backup successful! =)" --passivepopup 'Your plasma files were successfully backed up to '$BACKUPDIR $DATE'/'
    exit 0
else kdialog --title "Plasma backup failed. =(" --error "There was an error backing up your plasma files. Run plasmaBackup.sh from the commandline for more information. Sorry dude."
exit 1
fi

Last edited by VictorVictorVictor (2009-10-02 23:31:53)


Pink Noise - Amazing 80s new wave music being made today

www.pinknoiseboys.com
Even rockstars use Linux!

Offline

#568 2009-10-03 09:27:06

seiichiro0185
Member
From: Leipzig/Germany
Registered: 2009-04-09
Posts: 226
Website

Re: Post your handy self made command line utilities

Here's one I wrote a while back: fileregexp I use it to strip suff like md5sums from downloaded animes. It works on all files and folders in the current directory. Have a look at the included help for more infos:

#!/bin/bash

function usage {

  echo -en "fileregexp v0.1 - (C) seiichiro0185 2005\n"
  echo -en "manipulate groups of files using regexp's\n"
  echo -en "usage: `basename $0` [options] 'regexp' ['replace'] [-d] \n"
  echo -en "       or\n"
  echo -en "       `basename $0` modeswitch [-d]\n\n"
  echo -en "If replace is present regexp will be replaced by it.\n"
  echo -en "regexp and replace have to be enclosed in single quotes\n"
  echo -en "to prevent them from being expanded by the shell.\n"
  echo -en "by default fileregexp will change nothing,\n"
  echo -en "only with the option -d it will change the files!\n"
  echo -en "!!fileregexp will work on ALL files in the current directory!!\n\n" 
  echo -en "possible options:\n"
  echo -en "-d  actually do the changes\n"
  echo -en "-h  show this help screen\n\n"
  echo -en "possible modeswitches:\n"
  echo -en "--tolow     set all letters to lowercase\n"
  echo -en "--spacetou  replace whitespace with underline\n"
  echo -en "--utospace  replace underline with whitespace\n"
  exit 0

}

DRY=1
unset MODE

if [ -z $1 ]
then
  echo -en "ERROR: no parameters given!\n\n"
  usage
  exit 1
fi

case $1 in

-h)
  usage
  exit 1
  ;;

-d)
  DRY=0
  shift
  ;;
--help)
  usage
  exit 1
  ;;
    
--tolow)
  MODE="tolow"
  ;;
  
--spacetou)
  MODE="spacetou"
  ;;
  
--utospace)
  MODE="utospace"
  ;;
esac


if [ -z $MODE ]
then
  if [  -z "$1" ]
  then
    echo -en "ERROR: no regexp found!\n\n"
    usage
    exit 1
  fi

  REGEXP=$1
  shift
  if [ -z $1 ]
  then
    MODE="strip"
  else
    MODE="replace"
    REPLACE=$1
  fi
else
  shift
  if [ "$1" = "-d" ]
  then
    DRY=0
  fi  
fi

case $MODE in 
strip)
  
  for file in *
  do
    if [ $DRY -eq 1 ]
    then
      echo "$file --> `echo "$file" | sed -e "s/$REGEXP//g"`"
    else
      mv -v "$file" "`echo "$file" | sed -e "s/$REGEXP//g"`"
    fi
  done
  ;;

replace)
  for file in *
  do
    if [ $DRY -eq 1 ]
    then
      echo "$file --> `echo "$file" | sed -e "s/$REGEXP/$REPLACE/g"`"
    else
      mv -v "$file" "`echo "$file" | sed -e "s/$REGEXP/$REPLACE/g"`"
    fi
  done
  ;;

spacetou)
  for file in *
  do
    if [ $DRY -eq 1 ]
    then
      echo "$file --> `echo "$file" | sed -e 's/ /_/g'`"
    else
      mv -v "$file" "`echo "$file" | sed -e 's/ /_/g'`"
    fi
  done
  ;;
  
utospace)
  for file in *
  do
    if [ $DRY -eq 1 ]
    then
      echo "$file --> `echo "$file" | sed -e 's/_/ /g'`"
    else
      mv -v "$file" "`echo "$file" | sed -e 's/_/ /g'`"
    fi
  done
  ;;

tolow)
  for file in *
  do
    if [ $DRY -eq 1 ]
    then
      echo "$file --> `echo "$file" | sed -e'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`"
    else
      mv -v "$file" "`echo "$file" | sed -e'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`"
    fi
  done
  ;;
esac

exit 0

My System: Dell XPS 13 | i7-7560U | 16GB RAM | 512GB SSD | FHD Screen | Arch Linux
My Workstation/Server: Supermicro X11SSZ-F | Xeon E3-1245 v6 | 64GB RAM | 1TB SSD Raid 1 + 6TB HDD ZFS Raid Z1 | Proxmox VE
My Stuff at Github: github
My Homepage: Seiichiros HP

Offline

#569 2009-10-03 12:06:04

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Post your handy self made command line utilities

Show memory usage using piechart (AUR)

( awk '/^(MemFree|Buffers|Cached):/{total+=$2} END{print "free", total}' /proc/meminfo
ps -Ao comm,rss | sort -nrk 2,2 | head -n 8
) | piechart -T X -t "Total: $(awk '/MemTotal/{OFMT="%d"; print $2/1024}' /proc/meminfo) MB"

memorychart.th.png

After 9 entries the colors recycle. Store a png with ... | piechart -T png ... > memory_chart.png

Offline

#570 2009-10-03 15:24:11

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Hey, pretty cool, Procyon.  Haven't really seen a memory chart in Linux before.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#571 2009-10-03 17:16:37

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

i just finished this up now so its not extensively tested.  it's a variation on something lolilolicon posted for getting album art in conky.  its basically a general purpose albumart fetcher.

i tried to handle spaces in filenames everywhere (people seem to love this in Music folders specifically...) but my system has none so it might break things (im particularly worried about the linking function..)

the script does three things one after the other.  first it searches $mdir (~/Music by default) and builds a list as "filename artist album" using id3info.  second, it fetches the album art using lolilolicon's method and saves the file to $cdir/Artist_Album.jpg (~/.covers by default).  finally, it goes back down the list and symlinks $cdir/Artist_Album.jpg -> $mdir/.../album/$covr (which is cover.jpg by default).  now you have a valid image at album/cover.jpg wherever possible.

some things to note:

the script assumes your music is $mdir/.../album/tracks, album can really be anything i guess, but covers are linked to [that folder]/cover.jpg so keep that in mind.

the script uses a list to ensure that no duplicate effort is expended on multiple runs; add new music, run it again and it should only act on what's new.  it prints messages to screen only when it a) /does/ things or b) /fails to do/ things.  (i.e. reading tags, checking urls, checking for file existence means no messages).  the first run should print a ton of info as it does the whole collection.  thereafter you'll only see messages when a cover can't be found, or new music is processed.  it should also get significatly faster each time it's used.

zomg undo!

assuming that $cdir is ~/.covers, $mdir is ~/Music, and $covr is cover.jpg, undoing is as simple as

> rm -r ~/.covers
> find ~/Music -name cover.jpg -delete

this also assumes that you didn't have any cover.jpg's in your music files beforehand.

this is why i like the script, it keeps all your covers neatly under one directory, then places sanely named symlinks that can be easily found and managed later.

ok so the script:

#!/bin/bash
#
# pbrisbin 2009
#
#
# three steps:
#
#   1. find and query your music tags to build a list:
#      "/path/to/album/file.mp3 Artist Album"
#
#   2. fetch cover art
#      store jpg's under ~/.covers/artist_album.jpg
#
#   3. symlink those covers to ~/Music/.../album/cover.jpg
#
# filenames etc can be customized in script ofcourse
#
# NOTE: always use absolute paths
#       i.e. $HOME expands to /home/user NOT ~
#
# dependencies:
#       id3info
#       curl
#
###

# adjust these
mdir="$HOME/Music"       # where your music
cdir="$HOME/.covers"     # where to keep the actual covers
list="$cdir/listing.txt" # used to prevent duplicate work
covr="cover.jpg"         # filename of linked covers, some apps want front.jpg

create_list() {
  for song in $(find "$mdir" -type f); do
    # have we not processed this already 
    if ! grep -q "^${song//[/\\[} "; then
      echo -n "$(basename "$song") is new... "

      tag="$(id3info $song | grep "TPE1\|TALB" | awk -F ':' '{print $2}' | xargs -d '\n' | grep -E ".*[a-zA-Z].*" | sed -s 's/\ \ /\ /g')"

      # did we get a good tag
      if [ -n "$tag" ]; then
        echo tag found
      else
        echo tag NOT found
      fi

      echo "$song $tag" >> $list
    fi
  done

  sort $list | uniq > /tmp/list.$$
  mv /tmp/list.$$ $list
}

fetch_cover () {
  cat $list | cut -d ' ' -f 2- | uniq | while read album; do
    # do we have a valid album name
    if [ -n "$album" ]; then
      file="$cdir/${album// /_}.jpg"
      # have we not fetched this already
      if [ ! -f "$file" ] ; then
        echo -n "$(basename $file) is not in covers... "
        url="http://www.albumart.org/index.php?srchkey=${album// /+}&itempage=1&newsearch=1&searchindex=Music"
        cover_url=$(curl -s "$url" | awk -F 'src=' '/zoom-icon.jpg/ {print $2}' | cut -d '"' -f 2 | head -n1)
        # is it available
        if [ -n "$cover_url" ]; then
          echo retrieved
          wget -q -O "$file" "$cover_url"
        else
          echo NOT available 
        fi
      fi
    fi
  done
}

# links $cdir/artist_album.jpg to $mdir/.../album/$covr
link_them() {
  while read line; do
    line=( $line )
    file="${line[0]}"
    album="$(dirname "$file")"
    tag="${line[@]:1}"
    cover="$cdir/${tag// /_}.jpg"
    # do we have a valid cover
    if [ -f "$cover" ]; then
      # have we not processed this already
      if [ ! -L "$album/$covr" ]; then
        echo -n "$(basename "$album")/$covr not present... "
        ln -s "$cover" "$album/$covr"
        echo "linked to $(basename "$cover")"
      fi
    fi
  done < $list
}

create_list
fetch_cover 
link_them

also, if you've used another GUI music player with cover support, you could already have some covers in ~/.covers.  if the filename and song tag matches to Artist_Album.jpg as my script expects, it would be used and not redownloaded.

the live version can always be wgetted from here i'll probably add better filename-spaces support and try to find a tag reader that works with more formats (any suggestions?).

thanks and enjoy!

Last edited by brisbin33 (2009-10-03 18:25:58)

Offline

#572 2009-10-03 20:15:44

Lich
Member
Registered: 2009-09-13
Posts: 437

Re: Post your handy self made command line utilities

Wrote a simple weather script, great practice when you're learning Python smile

#!/usr/bin/python
# -*- coding: latin-1 -*-
import sys
import urllib
import os

if (len(sys.argv) > 1):
    f = urllib.urlopen('http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=' + sys.argv[1])
    raw = f.read()
    f.close()
    start = raw.find('<title>Currently')
    end = raw.find('C</title>')
    print raw[start + 18:start + (end - start)] + '°C'
else:
    print 'Usage  : ' + sys.argv[0] + ' < LOC CODE >'
    print 'Example: ' + sys.argv[0] + ' "EUR|RO|RO010|BUCHAREST"'

Last edited by Lich (2009-10-03 20:17:23)


Archlinux | ratpoison + evilwm | urxvtc | tmux

Offline

#573 2009-10-04 03:51:36

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Nice. Good example for me. But I'm critical. tongue

For the sake of KISS,
1. You don't need to import os, I think.
2. You don't need raw[start + 18:start + (end - start)] instead of raw[start + 18:end]

For the sake of completeness,
Can you give a complete list of "LOC CODE" for easy reference? I can't seem to find one instead of the one-by-one lookup method.

Here's my 'port' which uses your code as template, but asks yahoo! weather for it

#!/usr/bin/python
# -*- coding: latin-1 -*-
import sys
import urllib

if (len(sys.argv) > 1):
    f = urllib.urlopen('http://weather.yahooapis.com/forecastrss?p=' + sys.argv[1] + '&u=c')
    raw = f.read()
    f.close()
    start = raw.find('<b>Current')
    end = raw.find('\n<br />\n')
    print raw[start:end].lower().replace('<br />','').replace('<b>','').replace('</b>','').title()
else:
    print 'Usage  : ' + sys.argv[0] + ' <LocationID>'
    print 'Example: ' + sys.argv[0] + ' "USAK0001"'
    print 'For your LocationID, check out:'
    print 'http://www.intellicast.com/FFC/SupportFiles/FFC_ForecastLocationList_US.txt'
    print 'http://www.intellicast.com/FFC/SupportFiles/FFC_ForecastLocationList_Intl.txt'

The print raw... line is a hack in nature.... I don't know how to do it better though.


This silver ladybug at line 28...

Offline

#574 2009-10-04 03:59:55

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

This burns iso's to a CD or DVD.  Detects first if the disk is blank, blanks it on prompt, and then burns the iso.  I may have wrote about it before, checks are better now.

#!/bin/bash
# burniso - burn iso's to a cd or dvd

# variables
iso=$@

# text color variables
bldred='\e[1;31m' # red - bold
bldblu='\e[1;34m' # blue
bldwht='\e[1;37m' # white
txtrst='\e[0m'    # text reset

# display usage if full argument isn't given
if [[ -z $iso ]]; then
    echo " burniso filename.iso - burn iso's to cd or dvd."
    exit
fi

# test if cd/dvd is blank, if not prompt to blank
if [[ -n `hal-device | grep volume_label_` ]]; then
  if [[ -n `hal-device | grep "volume.disc.type = 'dvd_plus_rw'"` ]] || \
  [[ -n `hal-device | grep "volume.disc.type = 'dvdrw'"` ]]; then
  echo -e " ${bldred}*${txtrst} DVD is not empty, blank it? ${bldwht}(y/n)${txtrst}"
  read dvd
    if [[ $dvd == [yY] ]]; then
      dvd+rw-format -f /dev/dvd; else
      echo " exiting"
      exit
    fi
  fi
  if [[ -n `hal-device | grep "volume.disc.type = 'cd_rw'"` ]]; then
    echo -e " ${bldred}*${txtrst} CD is not emplty, blank it? ${bldwht}(y/n)${txtrst}"
    read cd
    if [[ $cd == [yY] ]]; then
      cdrecord -v dev=/dev/dvd blank=fast; else
      echo " exiting"
      exit
    fi
  fi; else
  echo -e " ${bldblu}+${txtrst} CD/DVD is blank, continuing."
fi

# burn iso
echo -e " ${bldwht}*${txtrst} Disks written to after insertion will not be recogized as blank."
echo -e " ${bldblu}*${txtrst} Burn iso to disk? ${bldwht}(y/n)${txtrst}"
read burn
if [[ $burn == [yY] ]]; then
    cdrecord -dao -v $iso; else
    echo " exiting"
fi

# notes
#  - generic check for cd/dvd by volume label to see if blank

Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#575 2009-10-04 05:55:03

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

@brisbin,
Look, I think you should separate artist and album with something more unique:

$ id3info Love\ will\ tear\ us\ apart.mp3 | grep "TPE1\|TALB" | awk -F ':' '{print $2}' | xargs -d '\n' | grep -E ".*[a-zA-Z].*" | sed -s 's/\ \ /\ /g'
 Joy Division Heart And Soul

A better result may be something like ''Joy Division === Heart And Soul". The advantage is that if you want to search other sites which treat artist and album separately(e.g. last.fm uses the format of .../artist/album).

As for a wider format support, you can try audiotag, it's a simple perl script and it's in AUR. You can even port the list part to your needs, so you don't rely on it. For example:

$ audiotag -l 'Sunny Afternoon.mp3'
*** `Sunny Afternoon.mp3'
=== ALBUM: The Ultimate Collection (Disc 1)
=== ARTIST: The Kinks
=== GENRE: Rock
=== TITLE: Sunny Afternoon
=== TRACKNUM: 9
=== YEAR: 2002
$ audiotag -l  'Sunny Afternoon.mp3' | grep -E 'ALBUM:|ARTIST:' | sed 'N;s/\n/ /'
=== ALBUM: The Ultimate Collection (Disc 1) === ARTIST: The Kinks

Another thought: Why not just use mpd.db to produce the list? Great file format support. Good as an alternative way.

So... Here's a script I've just written for cover fetching from last.fm. It reads mpd.db for Artist and Album, just for convenience.

#!/bin/bash
###:: cover_getter - Mass AlbumArt Downloader

DB="$HOME/.mpd/mpd.db"
URL="http://www.last.fm/music"
DIR="$HOME/.covers"
LOG="$DIR/fail.log"

grep -E '^Artist:|^Album:' "$DB" | sed 's/\([^:]*\): \(.*\)/\1="\2"/' | sed 'N;s/\n/ /' | uniq |
while read tag ; do
    eval ${tag} # This gets values for Artist and Album.
    LCL="${DIR}/${Artist}-${Album}.jpg" # sonata format
    [ -f "${LCL}" ] && continue
    URI="${URL}/${Artist}/${Album}" ; URI="${URI// /%20}"
    CVR=$( curl -s "${URI}" | grep 'LFM.set("ParentResource"' |
    tr '"' '\n' | sed '/.*.jpg/!d;s/\\//g' | tail -1 )
    [ -n "${CVR}" ] && wget -q -O "${LCL}" "${CVR}" ||
    echo "${tag}" >> "${LOG}"
done

When it comes to multi-artist albums, there probably will be sorta duplicated work...

Edit: So I updated my conky_mpd_cover_updater. Now it does not do any download.

#!/bin/bash
###:: mpd now-playing CoverArt Updater for conky [does not download]

hold_dir="$HOME/.covers"
none_cvr="nocover.png"
disp_cvr="/tmp/conky.cover"
play_log="/tmp/mpd_playing.log"

touch "${play_log}"
artist=$(mpc --format %artist% current)
album=$(mpc --format %album% current)
current="${artist}-${album}" # sonata format.
last=$(cat "${play_log}")

if [ "${current}" != "${last}" ] ; then
    cd "${hold_dir}" || exit 1
    if [ -f "${current}.jpg" ] ; then
        cover="${current}.jpg"
    elif [ -f "Various Artists-${album}.jpg" ] ; then
        cover="Various Artists-${album}.jpg"
    else
        cover="${none_cvr}"
    fi
    ln -sf "${hold_dir}/${cover}" "${disp_cvr}"
    echo "${current}" > "${play_log}"
fi

Last edited by lolilolicon (2009-10-04 08:19:44)


This silver ladybug at line 28...

Offline

Board footer

Powered by FluxBB