Sunday, January 31, 2016

Simple Webscraper with PhantomJs!

////////////////////////////////////////////////////////// 
/////  PhantomJS URL Scraper v.1.3 ///// 
// 
// Copyrighted by +A.M.Danischewski  2016+ (c)
// This program may be reutilized without limits, provided this 
// notice remain intact. 
// 
// Usage: phantomjs phantom_urls.js <URL> [["class"|"id"] [<query id/class name>]]
//
//   Argument 1: URL -- "https://www.youtube.com/watch?v=8TniRMwL2Vg" 
//   Argument 2: "class" or "id" 
//   Argument 3: If Argument 2 was provided, "class name" or "id name" 
// 
// By default this program will display ALL urls from a user supplied URL.  
// If a class name or id name is provided then only URL's from the class 
// or id are displayed.  
//  
/////////////////////////////////// 

var page = require('webpage').create(), 
    system = require('system'),
    address;

if (system.args.length === 1) {
  console.log(' Usage: phantomjs phantom_urls.js <URL> [["class"|"id"] [<query id/class name>]]');
  phantom.exit();
}

address = system.args[1];
querytype= system.args[2];
queryclass = system.args[3];
page.open(address, function(status) {
  if (status !== 'success') {
    console.log('Error loading address: '+address);
  } else {
   //console.log('Success! In loading address: '+address);   
  }
});

page.onConsoleMessage = function(msg) {
  console.log(msg);
}

page.onLoadFinished = function(status) {
   var dynclass="function() { window.class_urls = new Array(); window.class_urls_next=0; var listings = document.getElementsByClassName('"+queryclass+"'); for (var i=0; i < listings.length; i++) { var el = listings[i]; var ellnks=[].map.call(el.querySelectorAll('a'),function(link) {return link.getAttribute('href');}); var elhtml=el.innerHTML; window.class_urls.push(ellnks.join('\\n')); }; return window.class_urls;}"; 
   var    dynid="function() { window.id_urls = new Array(); window.id_urls_next=0; var listings = document.getElementById('"+queryclass+"'); var ellnks=[].map.call(listings.querySelectorAll('a'),function(link) {return link.getAttribute('href');}); var elhtml=listings.innerHTML; window.id_urls.push(ellnks.join('\\n'));  return window.id_urls;}";  
   var  allurls="function() { var links = page.evaluate(function() { return [].map.call(document.querySelectorAll('a'), function(link) { return link.getAttribute('href'); };); };); console.log(links.join('\\n')); }"; 
   var page_eval_function="";  
   if (querytype === "class") {
   console.log(page.evaluate(dynclass).toString().replace(/,/g, "\n")); 
   } else if (querytype === "id") {
   console.log(page.evaluate(dynid).toString().replace(/,/g, "\n")); 
   } else { 
   var links = page.evaluate(function() {
        return [].map.call(document.querySelectorAll('a'), function(link) {
            return link.getAttribute('href');
        });
    });    
       console.log(links.join('\n'));
   }             
   phantom.exit();
};

Wednesday, January 20, 2016

cp to Multiple Destinations - cpm

alias cpm='_() { src="${1%/}"; shift; printf "\"%s\" " "$@" | xargs -n 1 cp -vR "${src}"; }; _' 

Wednesday, January 6, 2016

To the Clipboard! In Bash

#!/usr/bin/env bash

###     Copyrighted +A.M.Danischewski (c)2016+  
### You may reuse this software without limit provided 
###         this notice remain in tact. 
###
### File: clip.bsh
### Requires: xsel 
###  Description: This script copies something to the clipboard, I 
###  recommend a simple alias, such as: $> alias clip='clip.bsh' 
###  Then you can:    
###     $> echo "hi there" | clip 
###     $> clip myblogpost.html
###     $> clip <<< "little message" 
###  _-=  Enjoy!  _-=
  
declare cliptxt="${1}"
declare rdlnk=$(readlink /proc/$$/fd/0)
declare nl=$(echo -en "\x01") 
if grep -Eq "^pipe:|deleted" <<< "${rdlnk}"; then 
 [[ ! -z "${cliptxt}" ]] && cliptxt=""
 while IFS= read -d $'\n' -r piped_input || break; do cliptxt="${cliptxt}${piped_input}${nl}"; done  
elif [[ -f "${rdlnk}" ]]; then   ## Its a redirected file
 cliptxt=$(< "${rdlnk}") 
elif [[ -e "${cliptxt}" ]]; then ## Its a filename
 cliptxt=$(< "${cliptxt}")       ## File name proper no redirect
else 
 echo " *** Something is wrong, input not read by ${0}." 
fi 
echo -En "${cliptxt}" | tr "${nl}" "\n" | xsel -ib
exit 0 

Bash Sound Fader

#!/usr/bin/env bash 

###     Copyrighted +A.M.Danischewski (c)2016+  
### You may reuse this software without limit provided 
###         this notice remain in tact. 
###
### File: fader.bsh   
###  Description: Simple starter framework for sound manipulation. 
###     $> alias fade='fader.bsh' 
###     $> fade 90 
###  Note: If its broken for some reason, perhaps you have a different 
###  sound card configuration, don't use Alsa/Pulse etc. 
###  To fix it find a way to make the following commands work, and 
###   search and replace them in the code below: 
### 
###   getvol -- The next command should return an integer of the current 
###             volume level:
###    amixer -D pulse sget Master | sed -n "/Left: Playback/p" | \
###            grep -o "\[[0-9]*\%" | sed "s/^.//;s/.\$//" 
### 
###   setvol -- where ${a} is any integer, should set the volume: 
###    amixer -D pulse sset Master ${a}% &>"${DISCARD_DEV}" 
###
###  _-=  Enjoy!  _-=  

declare fade_step=${2:-.05}
declare target_vol=${1}
declare DISCARD_DEV="/dev/random" 

(($#==0)) || [[ "${1}" =~ ^- ]] && usage

function usage() {   
 echo " Usage: fade <target volume> <fade_speed [.05-10]>" 
 exit 0 
} 

function getvol() { 
amixer -D pulse sget Master | sed -n "/Left: Playback/p" | \
          grep -o "\[[0-9]*\%" | sed "s/^.//;s/.\$//"
}

function sanity_checks() { 
((${target_vol}>100)) && target_vol=100 
((${target_vol}<0))   && target_vol=0 
}

function fade() { 
local curr_vol=$(getvol)
for a in $(eval echo {${curr_vol}..${target_vol}}); do 
 amixer -D pulse sset Master ${a}% &>"${DISCARD_DEV}" 
 sleep ${fade_step}
done 
}

function main() { 
sanity_checks
fade &
}

main 
exit 0 

Monday, January 4, 2016

Numbers to Words! In Bash

#!/usr/bin/env bash

###     Copyrighted +A.M.Danischewski (c)2016+  
### You may reuse this software without limit provided 
###         this notice remain in tact. 
### Version: 1.03
### File: num2str.bsh   
###  Description: This script turns numbers into words! 
###     $> num2str.bsh 112975
###        One Hundred Twelve Thousand Nine Hundred Seventy Five
###     $> num2str.bsh 112975 anyflag ## More verbose! 
###        One Hundred Twelve Thousand Nine Hundred Seventy Fifth
###     $> num2str.bsh 1
###        One 
###     $> num2str.bsh 
###        First
###  _-=  Enjoy!  _-=  

declare -a units=("" "one" "two" "three" "four" "five" "six" "seven" \
                  "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" \
                  "fifteen" "sixteen" "seventeen" "eighteen" "nineteen")            
declare -a tens=("" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")
declare -a unitsverbose=("" "first" "second" "third" "fourth" "fifth" "sixth" \
                         "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth" "thirteenth" \
                         "fourteenth" "fifteenth" "sixteenth" "seventeenth" "eigteenth" \
                         "nineteenth")
declare numstr="${1}" 
declare rdlnk=$(readlink /proc/$$/fd/0)

if grep -Eq "^pipe:|deleted" <<< "${rdlnk}"; then 
 [[ ! -z "${numstr}" ]] && numstr="" && set -- ourdearflag again
 while IFS= read -r piped_input || break; do numstr="${numstr}${piped_input}"; done  
elif [[ -f "${rdlnk}" ]]; then 
 [[ ! -z "${numstr}" ]] && numstr="" && set -- ourdearflag again
 numstr=$(head -1 "${rdlnk}") ## Only line 14u, you want more than h4ck.
elif [[ -e "${numstr}" ]]; then 
 numstr=$(head -1 "${numstr}") ## File name proper no redirect.
fi 

function main() { 
 local tmpnum=""
 local tmpnumv="" 
 if (($# > 1)); then 
   ## If more than 2 digits, convert only the ((last digit) (if > 0)) or last 2 digits.
  if ((${#numstr} > 2)); then 
   tmpnumv=$(sed 's/^\(.*\)\(..$\)/\2/' <<< "$numstr")
   tmpnum="$(sed 's/..$//' <<< "$numstr")00"
   tmpnum=$(convert ${tmpnum})
   numstr=$((10#${tmpnumv}))
   tmpnumv=$(convert ${tmpnumv} "unitsverbose")
   ((${#numstr} == 2)) && ((${numstr}>19)) && ((${numstr:1:1}==0)) && tmpnumv="$(sed 's/y$//' <<< "${tmpnumv}")ieth"
   numstr="${tmpnum} ${tmpnumv}"
  elif ((${#numstr} == 2)) && ((${numstr}>19)); then 
    numstr="$(sed 's/y$//' <<< $(convert "${numstr}" "unitsverbose"))ieth"
  else ## Convert the whole number to verbose
   numstr=$(convert "$numstr" "unitsverbose")
  fi 
   grep -qEi 'hundred$|thousand$|million$|billion$' <<< "${numstr}" && numstr="${numstr}th"
 else ## Normal conversion 
   numstr=$(convert "$numstr") 
 fi 
  ## Temp, fix for 329478932 and other large numbers that may cause a double space. 
 sed 's/[ ]*$//;s/  / /g' <<< "${numstr}"
} 
   
function convert() {
local -i INTEGER=$((10#${1}))
local unitstype=${2:-"units"} ## units or unitsverbose
 if (($INTEGER < 0)); then 
   echo -en "minus "
   convert $(($INTEGER*-1)) 
elif (($INTEGER < 20)); then 
   eval echo -en \${${unitstype}[$INTEGER]}
elif (($INTEGER < 100)); then 
   echo -en ${tens[$(($INTEGER/10))]}  
   (($INTEGER%10)) && echo -en " "
   eval echo -en \${${unitstype}[$(($INTEGER%10))]}
elif (($INTEGER < 1000)); then 
   eval echo -en "\${${unitstype}[$(($INTEGER/100))]} hundred" 
   (($INTEGER%100)) && echo -en " "
   convert $(($INTEGER%100)) 
elif (($INTEGER < 1000000)); then 
   convert $(($INTEGER/1000))
   echo -en " thousand" 
   (($INTEGER%1000)) && echo -en " "
   convert $(($INTEGER%1000)) 
elif (($INTEGER < 1000000000)); then 
   convert $(($INTEGER/1000000))
   echo -en " million " 
   (($INTEGER%1000)) && echo -en " "
   convert $(($INTEGER%1000000)) 
else 
   convert $(($INTEGER/1000000000))
   echo -en " billion" 
   (($INTEGER%1000000000)) && echo -en " "
   convert $(($INTEGER%1000000000)) 
fi 
} 

main $@ | sed 's/^./\U&/;s/ [^ ]/\U&/g'
exit 0

Sunday, January 3, 2016

Word dılɟ! in Bash

#!/bin/bash

### Copyrighted +A.M.Danischewski (c)2015+  
### You may reuse this software without limit provided this notice 
### remain in tact. 
###
### File: wordflip.bsh  
###  Description: This script turns text upside down! =) 
###  It can accept piped data and redirects,
###     $> wordflip.bsh <<< "some txt" 
###     $> echo -en "flip\nme\n" | wordflip.bsh
###  _-=  Enjoy!  _-=  

declare word=""
declare -A wordlist 
wordlist["a"]="\u0250" 
wordlist["b"]="q" 
wordlist["c"]="\u0254" 
wordlist["d"]="p" 
wordlist["e"]="\u01dd" 
wordlist["f"]="\u025f" 
wordlist["g"]="\u0183"
wordlist["h"]="\u0265"
wordlist["i"]="\u0131"
wordlist["j"]="\u027e"
wordlist["k"]="\u029e"
wordlist["l"]="l"
wordlist["m"]="\u026f"
wordlist["n"]="u"
wordlist["o"]="o"
wordlist["p"]="d"
wordlist["q"]="b"
wordlist["r"]="\u0279"
wordlist["s"]="s"
wordlist["t"]="\u0287"
wordlist["u"]="n"
wordlist["t"]="\u0287"
wordlist["v"]="\u028c"
wordlist["w"]="\u028d"
wordlist["x"]="x"
wordlist["y"]="\u028e"
wordlist["z"]="z"
wordlist["]"]="["
wordlist["["]="]"
wordlist[","]="'"
wordlist["'"]=","
wordlist["("]=")"
wordlist[")"]="("
wordlist["{"]="}"
wordlist["}"]="{"
wordlist["?"]="\u00bf"
wordlist["!"]="\u00a1"
wordlist["\""]=","
wordlist[">"]="<"
wordlist["<"]=">"
wordlist["+"]="+"
wordlist["_"]="\u203e"
wordlist[";"]="\u061b"

 ## Handle piped input and here-string redirects. 
if readlink /proc/$$/fd/0 | grep -Eq "^pipe:|deleted"; then
 word=""
 while IFS= read -r PIPED_INPUT || { DONE=1; }; do 
  PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}")
  word="${word}${PIPED_INPUT}+"
  ((${DONE})) && break; 
 done
else 
 word="${1}" 
fi 

revword=$(echo "$word"|rev) 
for i in $(eval echo "{1..${#revword}}"); do 
 printf "${wordlist[${revword:$((${i}-1)):1}]}"
done | sed 's/^+*//' | tr '+' '\n' 
echo 
exit 0

Boustrophedonify your text!

alias boustrophedon='sed -n "1{p;b a};{/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//;p};n;p;:a"' 

Friday, January 1, 2016

Ls File Path to Clipboard / via readlink and xsel

alias lsp='_(){ readlink -f "${1}" | tee /dev/stderr | head -1 | xsel -ib; }; _' 

Tuesday, December 29, 2015

Infinite Scroll Any Website via Xdotool! =)

#!/bin/bash 

### Copyrighted +A.M.Danischewski (c)2015+  
### You may reuse this software without limit provided this notice 
### remain in tact. 
###
### File: scroll_me.bsh  
###  Description: This software can be used to automatically infinite 
###  scroll, a webpage on tumblr and any other infinite scroll website.
###
###  _-=  Enjoy!  _-=  

declare    PROGTITLE="${1:-Untitled}" ## Name of window (in title bar)
declare    ITERATIONS=${2:-10}        ## 10 is a bunch, change as needed
declare    SLEEP_PAGERELOAD=${3:-3}   ## Sleep time for new data to load

[[ "${1}" =~ ^- ]] && echo " Usage: ${0##*/} \"<prog title frag>\" [<itertations> [<sleep between reloads>]]" && exit 0

for a in $(eval echo {1..${ITERATIONS}}); do 
 xdotool search "${PROGTITLE}" key End
 sleep ${SLEEP_PAGERELOAD}
done 

exit 0 

Friday, December 25, 2015

Html INFO pages - using info2html

<?php
//---------------------------------------------------------
//                      routing.php
//---------------------------------------------------------
//
// PURPOSE
//  This php script routes calls to the info2html scripts, infocat and 
//  info2html for the PHP built-in webserver (like a PHP mod_rewrite). 
//
// DESCRIPTION 
// How to use: 
//  * Start the php webserver, wherever this file is found, or provide full path: 
//     $> php -S localhost:8000 -t / routing.php 
//  * Start browsing! Visit: 
//     http://localhost:8000/info2html?(coreutil)Top
// Note: Before you use this, edit the following variable to point to the 
//       info2html Perl script. 
//       Also make sure that your info share directory is listed in the 
//       info2html.conf file (mine wasn't I had to add /usr/share/info). 
//
// AUTHORS 
//    2015.12.25        +A.M.Danischewski <adam_ lastname@not gamil.com>
// 
// ORIGINAL AUTHOR
//    2015.12.25        +A.M.Danischewski <adam_ lastname@not gamil.com>
// 
// HISTORY
//    2015.12.25  V2.1  Initial version     
//                      +A.M.Danischewski <adam_ lastname@not gamil.com>
//
// Copyrighted by A.M.Danischewski 2015+ (c)
// You may reuse this code without limit, provided this notice remain in tact. 
// 
$info2html_basepath=realpath(dirname(__FILE__)); 
$page=""; 
if (preg_match('/info2html/', $_SERVER["REQUEST_URI"])) {
 if (isset($_SERVER['QUERY_STRING'])) $page=$_SERVER["QUERY_STRING"]; else print_r($_SERVER);     
  // Calls info2html, same as if info2html were called from the commmand line.
  System($info2html_basepath."/info2html \"".$page."\""); 
} else if (preg_match('/infocat/', $_SERVER["REQUEST_URI"])) {
  // Calls infocat, same as if info2html were called from the commmand line.
  System($info2html_basepath."/infocat"); 
} 
else {  
 return FALSE; 
}
?>
On Linux, with Php/Perl/firefox installed it should require no further configuration than:

Pulling the latest code:
$> git clone "https://github.com/AdamDanischewski/info2html.git"

Move the info2html directory to where you want it to live.
Review, edit, run configure.bsh.
After running configure.bsh you should find an alias file in the scripts directory,
add the alias to your .bashrc file.

Done!!

You can now run the alias i2h as follows:
$> i2h coreutils wc invocation
~ or ~ to view the whole info catalog, i2h alone
$> i2h

If you don't have git a working edited version can be found here: info2html-2.1

Tuesday, December 22, 2015

Simple Message Scheduler -- w/Zenity

#!/usr/bin/env bash 

### Copyrighted by +A.M.Danischewski  2015+ (c)
### This program may be reutilized without limits, provided this 
### notice remain intact. 

 ## Any valid date format will do  
 ##  E.g. "+ 10 minutes", "+ 2 hours", "23:00:00", "@1450829119"
declare     MSG_FMTSTRING=${1:-"20:00:00"} 
declare     MSG_SCHEDULE
declare     PROG_NAME="${0##*/}"
declare -r  SEND_MSG=${2:-"Check up on Marina"}
declare -ir SLEEP_CYCLE=2 
declare -ir ZENITY_WIDTH=400 
declare -ir ZENITY_HEIGHT=200 
declare -r  ZENITY_MSG_TITLE="Important Reminder" 

function usage() { 
 echo -en "\n Usage: ${0##*/} <date format str> <\"message\">\n" 
 echo -en "  E.g. date formats =>> \"+ 10 minutes\", \"+ 2 hours\", \"23:00:00\", \"@1450829119\"\n\n"
 exit 0  
}  

function list_sched_msgs() { 
local cmd_pid
local cmd_line
local cmd_sched
local start_time 
local end_time
while IFS= read a; do 
 cmd_pid=$(awk "{print \$1}" <<< "${a}") 
 cmd_line=$(sed "s/^.*${cmd_pid} *//" <<< "${a}" | cut -d' ' -f3-)
 cmd_sched=$(awk "{print \$4}" <<< "${a}") 
   ## If its not a normal time (20:00:00) then presume its an offset (+ 5 Seconds). 
 ! grep -Eq "@|:" <<< "${cmd_sched}" && cmd_sched=$(grep -iEo '^.*(hour[s]*|minute[s]*|second[s]*)' <<< "${cmd_line}" | tr '[[:upper:]]' '[[:lower:]]') 
 start_time=$(date -d"$(ps -o lstart= -p${cmd_pid})")
 end_time=$(date -d"${start_time} ${cmd_sched}" 2>/dev/random)
  ### If end_time had an error (for the simple "+ 1 hour" type cases, then calculate off the 
  ### start time of the process. 
 (($?)) && end_time=$(date -d"@$(($(date -d"${start_time}" +%s)+$(($(date -d"${cmd_sched}" +%s)-$(date -d"${start_time}" +%s)))))")
 printf "Sched msg: %-35.35s.. Pid:%6.6s  Ends: %-.35s\n" "${cmd_line##*/}" "${cmd_pid}" "${end_time}"  
done < <(ps -eo pid,cmd | grep "[${PROG_NAME:0:1}]${PROG_NAME:1}" | grep -v "[${PROG_NAME:0:1}]${PROG_NAME:1} -l") 
exit 0 
}

function kill_msgs() { 
 kill $(ps -eo pid,cmd | grep "[${PROG_NAME:0:1}]${PROG_NAME:1}" | grep -Ev "[${PROG_NAME:0:1}]${PROG_NAME:1} -(k|l)" | awk '{printf $1" "}')
 exit 0 
} 

function sanity_checks() {
[[ "${MSG_FMTSTRING}" =~ ^-l ]] && list_sched_msgs
[[ "${MSG_FMTSTRING}" =~ ^-k ]] && kill_msgs
[[ "${MSG_FMTSTRING}" =~ ^-	]] && usage 
MSG_SCHEDULE=$(date --date="${MSG_FMTSTRING}" +%s 2>/dev/random)
if (($?)); then 
 echo -en "\nImproperly formatted date string (${MSG_FMTSTRING}) valid strings include: \n"
 echo -en "\t\"+ 10 minutes\", \"+ 2 hours\", \"23:00:00\", \"@1450829119\"\n\n"
 exit 0 
fi 
}
   
function main() { 
sanity_checks	
while :; do 
 if (($(date +%s) > ${MSG_SCHEDULE})); then 
   paplay "/usr/share/sounds/freedesktop/stereo/complete.oga"       
   zenity --width=${ZENITY_WIDTH} --height=${ZENITY_HEIGHT} --text-info \
          --title "${ZENITY_MSG_TITLE}" <<< "${SEND_MSG}"
   break; 
 else 
   sleep ${SLEEP_CYCLE}; 
 fi 
done & ## Background our dear process.  
} 

main 
exit 0 
 
alias msg='msg_sched.bsh' 
alias lsmsgs='msg_sched.bsh -l' 
alias killmsgs='msg_sched.bsh -k' 
alias wakeups='_(){ INTERVALMIN=$((${1}*60)); MSGCNT=${2}; CURRTIME=$(($(date +%s)+${INTERVALMIN})); for a in $(eval echo {${CURRTIME}..$((${CURRTIME}+$((${INTERVALMIN}*${MSGCNT}))))..${INTERVALMIN}}); do msg_sched.bsh "@${a}" "Wake up its $(date -d"@${a}")"; done; lsmsgs; }; _'