Skip to content

Instantly share code, notes, and snippets.

@kathawala
kathawala / weather.sh
Created November 26, 2014 09:15
Script to fetch weather data from API
#!/bin/bash
API=
# Set URL for fetching weather information and an Error Msg for invalid input
URL=http://api.wunderground.com/api/$API/geolookup/conditions/q/CA/Stanford.xml
ERR="Error: Arguments expected in form [City] [State]. Ex: weather Palo_Alto CA"
# handles arguments for location / default is Stanford CA
# well-formed argument example: "weather Memphis TN"
@kathawala
kathawala / ram.sh
Created November 28, 2014 23:06
Little script to print out your memory usage
#!/bin/bash
# free is a utility which provides statistics about memory in use
# -m flag provides memory in Mb
# awk takes all the arguments from grep and can print them
USED=`free -m | grep cache: | awk '{print $3}'`
TOTAL=`free -m | grep Mem: | awk '{print $2}'`
# bc is a linux calculator which can print floating point
# scale determines number of decimals (ex: 0.00)
@kathawala
kathawala / undecorate.ds
Created November 29, 2014 19:40
1-line window undecoration rule for devilspie
(undecorate)
@kathawala
kathawala / play.sh
Created December 8, 2014 08:59
A script to download a youtube video and play it in smplayer, good for flash-only videos or long videos which buffer
#!/bin/bash
#utility to play a youtube video after the url has been cut using ctrl-x
youtube-dl -o "/home/farhan/Downloads/frmyoutube.%(ext)s" "$(xclip -o)" | tee /home/farhan/bin/play.log
#make sure we don't have any errors
if [ $(grep -i "error" ~/bin/play.log) ]; then
echo "Error downloading video. Try again :("
exit 1
fi
@kathawala
kathawala / gmail.py
Created December 12, 2014 00:46
A simple parser for GMail's Atom feed xml. Prints out the Title and Summary and Email address of sender
import xml.etree.ElementTree as etree
tree = etree.parse('/home/farhan/bin/email.xml')
root = tree.getroot()
for entry in root.iter('{http://purl.org/atom/ns#}entry'):
for title in entry.findall('{http://purl.org/atom/ns#}title'):
print ('TITLE: ', title.text)
for author in entry.findall('{http://purl.org/atom/ns#}author'):
for email in author.findall('{http://purl.org/atom/ns#}email'):
@kathawala
kathawala / randomwords.py
Created December 15, 2014 05:47
Prints and erases 5 random lines from a file
# Reads out 5 random lines from a file and erases them
import os.path
import random
dictionary = open("/home/farhan/bin/words", "r")
words = set()
last_pos = dictionary.tell()
rand_words = list(dictionary)
@kathawala
kathawala / getpath.py
Created December 22, 2014 09:49
Python script getting its own directory
#!/bin/env python
import inspect
import os
import sys
def get_script_dir(follow_symlinks=True):
if getattr(sys, 'frozen', False): # py2exe, PyInstaller, cx_Freeze
path = os.path.abspath(sys.executable)
else:
@kathawala
kathawala / bigpkg.sh
Created January 17, 2015 20:41
Prints out a list of all pacman packages sorted by size from largest to smallest
#!/bin/bash
( echo "PACKAGE SIZE(K)";
for A in /var/lib/pacman/local/*/desc; do
egrep -A1 '%(NAME|SIZE)' $A \
| gawk '/^[a-z]/ { printf "%s ", $1 }; /^[0-9]/ { printf "%.0f\n", $1/1024 }'
done | sort -nrk2 ) | column -t
@kathawala
kathawala / cleanpkg.sh
Created May 18, 2015 05:23
Removes cached packages which have 2+ more recent package versions cached
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
x="zzzzz"
copies=()
CLEAN_DIR="/var/cache/pacman/pkg"
@kathawala
kathawala / zip-only.sh
Created May 24, 2015 01:33
one-liner to zip all .c and .h files in a directory
find . | grep -E '\.(c|h)$' | zip six.zip -@