├── README.md ├── open-bookmark ├── lowbattery-notify ├── webscrape ├── codeforces-answer ├── 4chan-down └── np-down ├── emoji-picker ├── fufetch └── tools └── shell_alternatives /README.md: -------------------------------------------------------------------------------- 1 | # My script collection 2 | 3 | Work in progress... 4 | -------------------------------------------------------------------------------- /open-bookmark: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # open bookmarks in browser using dmenu 4 | # dependencies "dmenu" any browser 5 | 6 | # Browser command 7 | Browser="qutebrowser" 8 | 9 | TempFile=$(mktemp) 10 | 11 | dmenu -p "Open: " -l 15 > "$TempFile" << EOF 12 | [duckduckgo]start.duckduckgo.com 13 | [reddit]reddit.com 14 | [suckless]suckless.org 15 | [monkeytype]monkeytype.com 16 | [keybr]keybr.com 17 | [github]github.com 18 | EOF 19 | 20 | read -r Url < "$TempFile" 21 | [ -n "$Url" ] && $Browser "${Url#*]}" 22 | 23 | rm -f "$TempFile" 24 | -------------------------------------------------------------------------------- /lowbattery-notify: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Notifies user when battery drops to <=15% 4 | # dependencies "notify-send" 5 | 6 | # Might need to be modified depending on the device (BAT0, BAT1, BAT2) 7 | BatteryDir="/sys/class/power_supply/BAT1" 8 | # Battery percentage to notify at 9 | MinPercent=15 10 | # Checks every 10 minutes 11 | Refresh=600 12 | 13 | while :; do 14 | read -r BatStatus < "$BatteryDir/status" 15 | read -r BatPercent < "$BatteryDir/capacity" 16 | if [ "$BatStatus" = "Discharging" ] && [ $BatPercent -le $MinPercent ]; then 17 | notify-send -u critical "Low Battery: $BatPercent%" 18 | fi 19 | sleep $Refresh 20 | done 21 | -------------------------------------------------------------------------------- /webscrape/codeforces-answer: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # usage : codeforces-answer 439 A 4 | # shows the best solutions one by one 5 | 6 | prob_num=$1 7 | prob_letter=$2 8 | 9 | 10 | get_top_answer_id () { 11 | url="https://codeforces.com/problemset/status/$prob_num/problem/$prob_letter?order=BY_CONSUMED_TIME_ASC" 12 | curl -s "$url" | 13 | grep -o "submissionId=\"[^\"]*\"" | 14 | { 15 | while IFS=\" read _ id ; do 16 | printf "%d\n" "$id" 17 | done 18 | } 19 | } 20 | 21 | get_code_id () { 22 | id=$1 23 | url="https://codeforces.com/problemset/submission/$prob_num/$id" 24 | 25 | curl -s "$url" | sed -n -E -e ' 26 | / *
"$TempFile" <7 | # Example: 8 | # 4chan-down w 2175954 9 | 10 | Prog="4chan-down" 11 | 12 | TempFile=$(mktemp --tmpdir "${Prog}.tmp.XXXXXXXX") 13 | JsonFile=$(mktemp --tmpdir "${Prog}.json.XXXXXXXX") 14 | 15 | Err () { 16 | printf "$1\n" 17 | exit ${2:-1} 18 | } 19 | 20 | [ "$1" = "--help" ] || [ "$1" = "-h" ] && Err "USAGE: $Prog " 0 21 | [ -z "$2" ] && Err "USAGE: $Prog " 22 | 23 | Board=${1} 24 | Thread=${2} 25 | 26 | # Fetching Data 27 | curl -s "https://a.4cdn.org/$Board/thread/${Thread}.json" > "$JsonFile" 28 | 29 | # Checking data is non empty 30 | read _Word < "$JsonFile" || [ -n "$_Word" ] || 31 | Err "Incorrect thread/board. No media found.\nUSAGE: $Prog " 32 | 33 | mkdir -p "${Board}_${Thread}" 34 | cd "${Board}_${Thread}" 35 | 36 | # start flags 37 | printf "parallel\n" > "$TempFile" 38 | PreUrl="https://i.4cdn.org/$Board/" 39 | 40 | # json to curl config file 41 | jq -r '.posts|.[]|select(.tim !=null)| 42 | "url = \"'"$PreUrl"'\(.tim)\(.ext)\"\nremote-name\n" 43 | ' <"$JsonFile" >> "$TempFile" 44 | 45 | curl -K "$TempFile" 46 | 47 | rm -f "$TempFile" "$JsonFile" 48 | -------------------------------------------------------------------------------- /webscrape/np-down: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # download newspapers from `https://dailyepaper.in/home/` 4 | # https://github.com/pystardust/scripts 5 | # email: notpiestardust@gmail.com 6 | 7 | # USAGE: np-down 8 | # after date selection, download will start 9 | # to change newspaper, select the newspaper in the below site 10 | # https://dailyepaper.in/home/ 11 | # choose newspapers and set its url below 12 | 13 | newspaper_url="https://dailyepaper.in/the-hindu-pdf-epaper-download-2021/" #The Hindu newspaper 14 | # newspaper_url="https://dailyepaper.in/times-of-india-epaper-pdf-download-2020/" #Times Of India 15 | # newspaper_url="https://dailyepaper.in/economic-times-newspaper-today/" #The economic Times 16 | 17 | 18 | 19 | tab_space=$(printf '\t') 20 | gap=" " 21 | gap=$gap$gap$gap 22 | gap=$gap$gap$gap 23 | 24 | html=$(curl -s "$newspaper_url") 25 | selection=$( 26 | { 27 | pattern=' ' 28 | while IFS= read line; do 29 | case $line in 30 | ("$pattern"*vk.com*) 31 | date=${line%${line#*20[0-9][0-9]}} 32 | date=${date##*'>'} 33 | link=${line#*vk.com} 34 | link=${link%%\"*} 35 | printf '%s\t%s\t%s\n' "$date" "$gap" "$link" 36 | ;; 37 | esac 38 | done <<-! 39 | $html 40 | ! 41 | } | fzf -d "$tab_space" -n 1 42 | ) 43 | 44 | [ -z "$selection" ] && exit 0 45 | 46 | paper_url="https://vk.com${selection##*$tab_space}" 47 | 48 | download_url=$(curl -s "$paper_url" | grep -o 'https://[^"]*\.pdf') 49 | curl -O# "$download_url" 50 | -------------------------------------------------------------------------------- /tools/shell_alternatives: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # pure shell alternatives to external commands 4 | 5 | # Literal assignment of new line and tabspace 6 | NewLine=" 7 | " 8 | TabSpace=" " 9 | 10 | 11 | TempFile=$(mktemp) 12 | 13 | Sh_exit () { 14 | rm -f "$TempFile" 15 | } 16 | # remove the tempfile at exit 17 | trap "Sh_exit" EXIT 18 | 19 | # Stores stdin in a variable named $1 20 | # similar to mapfile in bash but stores it all 21 | # in the same variable 22 | Sh_readvar () { 23 | _VarName=$1 24 | _Var= 25 | # checking if last line is not empty is necessary when 26 | # the last line doesn't have \n 27 | # which leads to read giving a non zero exit status 28 | while IFS= read _Line || [ -n "$_Line" ]; do 29 | _Var=$_Var$NewLine$_Line 30 | done 31 | _Var=${_Var#$NewLine} 32 | eval $_VarName=\$_Var 33 | unset _VarName _Var _Line 34 | } 35 | 36 | # Translates all characters in $1 to the $2 37 | # Data is read from stdin 38 | Sh_tr () { 39 | IFS=$1 40 | _Ofs=$2 41 | Sh_readvar _Text 42 | set -- $_Text 43 | printf "%s" "$1" 44 | shift 45 | [ -n "$1" ] && printf "$_Ofs%s" $* 46 | unset IFS _Ofs _Text 47 | } 48 | 49 | # Prints file to stdout 50 | Sh_cat () { 51 | while IFS= read _Line; do 52 | printf "%s\n" "$_Line" 53 | done <"$1" 54 | unset _Line 55 | } 56 | 57 | # Replicates cut, the field needs to be modified 58 | Sh_cut () { 59 | _Delimeter=${1:-$TabSpace} 60 | while IFS=$_Delimeter read _F1 _F2 _F3 _F4 _F5 _; do 61 | printf "%s\n" "$_F2" 62 | done 63 | unset _Delimeter _F1 _F2 _F3 _F4 _F5 64 | } 65 | 66 | Sh_wc_char () { 67 | printf "%d\n" "${#1}" 68 | } 69 | 70 | Sh_wc_line () { 71 | _LineNumber=0 72 | while IFS= read _ || [ -n "$_" ] ; do 73 | _LineNumber=$((_LineNumber+1)) 74 | done 75 | printf "%d\n" "$_LineNumber" 76 | unset _LineNumber 77 | } 78 | 79 | # Prints lines matching pattern 80 | # Pattern needs to be in shell patterns ( * ! [ ? ) 81 | # Line pattern, to identify the line 82 | # Example: *[Ll]inu?* 83 | Sh_grep () { 84 | while IFS= read _Line || [ -n "$_Line" ]; do 85 | case $_Line in 86 | $1) printf "%s\n" "$_Line" ;; 87 | esac 88 | done 89 | unset _Line 90 | } 91 | 92 | # Substitutes pattern in $1 93 | # with string in $2 94 | # Pattern needs to be in shell patterns 95 | # Word pattern ( to identify the word only the word ) do not add * in the ends 96 | # Example: ra??[!j]t 97 | Sh_gsub () { 98 | _Pattern=$1 99 | _Replace=$2 100 | while IFS= read _Line || [ -n "$_Line" ]; do 101 | _Hold= 102 | while :; do 103 | case $_Line in 104 | *${_Pattern}*) 105 | _Hold="$_Hold${_Line%%${_Pattern}*}${_Replace}" 106 | _Line="${_Line#*${_Pattern}}" 107 | ;; 108 | *) 109 | printf "%s\n" "$_Hold$_Line" 110 | break ;; 111 | esac 112 | done 113 | done 114 | unset _Line _Pattern _Replace 115 | } 116 | 117 | # Substitutes only the first occurrence in every line 118 | Sh_sub () { 119 | _Pattern=$1 120 | _Replace=$2 121 | while IFS= read _Line || [ -n "$_Line" ]; do 122 | case $_Line in 123 | *${_Pattern}*) 124 | _Line="${_Line%%${_Pattern}*}${_Replace}${_Line#*${_Pattern}}";; 125 | esac 126 | printf "%s\n" "$_Line" 127 | done 128 | unset _Line _Pattern _Replace 129 | } 130 | 131 | 132 | # Examples 133 | 134 | #Reading contents into variable 135 | Sh_readvar Var1 <
"$TempFile" 152 | Sh_readvar CommandOutput < "$TempFile" 153 | printf "%s\n" "$CommandOutput" 154 | --------------------------------------------------------------------------------