#!/bin/bash ######################################################################## # Title : for mp3 music files at http://music.socbay.com/music/ # Author : Anh K. Huynh # URL : http://viettug.org/wiki/fs/socbay_lookup # Version : 1.2.6 (2010/08/30) # $Id: socbay_lookup.sh 20 2010-08-30 13:46:23Z pi $ # Requires : http://www.shelldorado.com/scripts/cmds/urlencode.txt # Requires : writable /tmp/ # Requires : sed, awk, grep, wget, tr # TODO : pagination, lyric lookup, options ######################################################################## _URL_ENCODE="" _QUERY_FILE="" _QUERY_STRING="" _RESULT_FILE="" _TEMP_DIR="" _REQUIRED_PROGRAMS="sed awk grep wget tr" _msg() { echo -e ":: $*" } # Purpose : Safely Creating Temporary Files in Shell Scripts # Source : Stefan Nordhausen # Poster : David Isecke # URL : http://www.linuxsecurity.com/content/view/115462/81/ _create_temp_dir() { tmp=${TMPDIR-/tmp} tmp=$tmp/kah.$RANDOM.$RANDOM.$RANDOM.$$ (umask 077 && mkdir $tmp) || { _msg "Could not create temporary directory! Exiting." 1>&2 exit 1 } export _TEMP_DIR="$tmp/" export _QUERY_FILE="$tmp/query" export _RESULT_FILE="$tmp/result" } _check_programs() { while [ "x$1" != "x" ]; do type $1 >/dev/null 2>&1 if [ $? -ge 1 ]; then _msg "Could not locate '$1'. Exiting." 1>&2 exit 4 fi shift done } # check for some tools _check_programs $_REQUIRED_PROGRAMS # on FreeBSD we must use gsed # if gsed isnot found (on Linux), make an alias type gsed >/dev/null 2>&1 if [ $? -ge 1 ]; then alias gsed='sed' fi # test whether 'urlencode.sh' does exist which urlencode.sh >/dev/null 2>&1 if [ $? -ge 1 ]; then _msg "Could not locate 'urlencode.sh' in your PATH" 1>&2 if [ ! -f ./urlencode.sh ]; then _msg "Could not locate 'urlencode.sh' in current directory" 1>&2 _msg "Please download this script at" _msg "\thttp://www.shelldorado.com/scripts/cmds/urlencode.txt. Exiting." 1>&2 exit 2 else _URL_ENCODE="sh ./urlencode.sh" fi else _URL_ENCODE="urlencode.sh" fi # test for arguments if [ "x$*" == "x" ]; then _msg "Missing arguments. Please specify something to search. For e.g,." _msg "\t $0 mĘ” hoa" exit 3 fi ## _create_temp_dir echo $* | $_URL_ENCODE > $_QUERY_FILE _QUERY_STRING="`cat $_QUERY_FILE`" _URL="http://music.socbay.com/music/xmlmp3.aspx?q=`cat $_QUERY_FILE`&type_search=0" _URL="http://www.socbay.com/mp3/page/0/0/`cat $_QUERY_FILE`" _URL="http://123.30.6.9:3740/Search.aspx?q=`cat $_QUERY_FILE`&num=46&f=0&r=0&start=0&callback=?" # f=0: search by title # f=5: search by lyrics _URL="http://www.socbay.com/j_mp3Search_v1/?q=$_QUERY_STRING&num=30&f=0&r=0&start=0" # port 3740 or 3761 _URL="http://123.30.6.9:3761/Search.aspx?q=`cat $_QUERY_FILE`&num=46&f=0&r=0&start=0&callback=?" _msg "Fetching data from $_URL" 1>&2 wget -A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506" \ -q "$_URL" \ --referer "http://www.socbay.com/mp3" \ -O $_RESULT_FILE _msg "Result of querying \"$*\"" 1>&2 src="http://www.socbay.com/mp3/download/" cat $_RESULT_FILE \ |tr '"' '\n' \ |grep http:// rm -rf $_TEMP_DIR # end of file #