functions.sh (4801B)
1 # shellcheck shell=sh 2 3 status_dotfiles() { 4 set -- \ 5 --modified \ 6 --others \ 7 --exclude-standard \ 8 --directory \ 9 --no-empty-directory 10 if [ -n "$(dotfiles ls-files "$@" || true)" ] || 11 ! dotfiles submodule foreach \ 12 "[ -z \"\$(git ls-files ""$*"")\" ]" \ 13 >/dev/null 2>/dev/null; then 14 echo "In dotfiles:" 15 dotfiles status -s 16 dotfiles submodule foreach 'git status -s' 17 return 1 18 fi 19 } 20 21 print_goodbye() { 22 if [ -z "${TMUX}" ]; then 23 status_dotfiles || read -r _ 24 fi 25 } 26 27 status_news() { 28 output_email="$( 29 for path in "${XDG_DATA_HOME}/mail/"*; do 30 mailbox=$(basename "${path}") 31 32 count_spam_new=$(find "${path}/[Gmail]/Spam/new" -type f | wc -l) 33 count_spam_cur=$(find "${path}/[Gmail]/Spam/cur" -type f | wc -l) 34 count_spam="$((count_spam_new + count_spam_cur))" 35 36 count_new=$(find "${path}/Inbox/new" -type f | wc -l) 37 count_cur=$(find "${path}/Inbox/cur" -type f | wc -l) 38 count_cur_unseen=$(find "${path}/Inbox/cur" -type f | grep -cv "S[^,]*$") 39 count_unseen="$((count_new + count_cur_unseen))" 40 count="$((count_new + count_cur))" 41 42 if [ "$((count + count_spam))" -gt 0 ]; then 43 echo "${count_new}/${count_unseen}/${count}-${count_spam}" "${mailbox}" 44 fi 45 done 46 )" 47 unset mailbox count 48 if [ -n "${output_email}" ]; then 49 echo "# Email" 50 echo "${output_email}" | column 51 echo 52 fi 53 unset output_email 54 55 sql_output=$(mktemp) 56 tags=$(mktemp) 57 echo "SELECT feedurl, SUM(unread) FROM rss_item GROUP BY feedurl" | 58 sqlite3 ~/.local/share/newsboat/cache.db | 59 sort >"${sql_output}" 60 awk '! (/^#/ || /^$/) { print $1 "|" $2 }' ~/.config/newsboat/urls | 61 sed "/\"/d" | 62 sort -u >"${tags}" 63 output_rss="$( 64 join -t "|" "${sql_output}" "${tags}" | 65 awk -F "|" '{ arr[$3] += $2 } END { for (key in arr) if (arr[key] > 0) printf("%d %s\n", arr[key], key) }' 66 )" 67 rm -f "${sql_output}" "${tags}" 68 unset sql_output tags 69 if [ -n "${output_rss}" ]; then 70 echo "# RSS" 71 echo "${output_rss}" | column 72 echo 73 fi 74 unset output_rss 75 76 } 77 78 check_imca() { 79 current_date="$(date +%s)" 80 81 if [ -f /tmp/check_imca ]; then 82 if [ "$(cat /tmp/check_imca || true)" -ne 200 ]; then 83 next_check="$(($(stat -c %Y "/tmp/check_imca") + 1 * 60))" 84 else 85 next_check="$(($(stat -c %Y "/tmp/check_imca") + 15 * 60))" 86 fi 87 else 88 next_check="${current_date}" 89 fi 90 91 if [ "${current_date}" -ge "${next_check}" ]; then 92 curl -s -o /dev/null -w "%{http_code}" imca.edu.pe/es/ >/tmp/check_imca 93 fi 94 95 status_code="$(cat /tmp/check_imca)" 96 if [ "${status_code}" -ne 200 ]; then 97 echo "imca.edu.pe/es/ status code ${status_code}" 98 fi 99 unset status_code 100 } 101 102 check_exchange_rate() { 103 current_date="$(date +%s)" 104 105 if [ -f /tmp/check_exchange_rate ]; then 106 if [ -z "$(cat /tmp/check_exchange_rate || true)" ]; then 107 next_check="$(($(stat -c %Y "/tmp/check_exchange_rate") + 1 * 60))" 108 else 109 next_check="$(($(stat -c %Y "/tmp/check_exchange_rate") + 15 * 60))" 110 fi 111 else 112 next_check="${current_date}" 113 fi 114 115 if [ "${current_date}" -ge "${next_check}" ]; then 116 response=$( 117 curl -sS https://cuantoestaeldolar.pe/cambio-de-dolar-online | 118 sed "s/class>/>/g;s/class //g;s/defer //g;s/alt //g;s/defer>/>/g;s/data-n-p//g;s/data-n-g//g;s/data-n-css//g;s/nomodule //g" | 119 xpath -q -e "/html/body/div/main/div[3]/div/div[7]/div/div[1]" 2>/dev/null 120 ) 121 122 buy=$( 123 for i in $(seq 26); do 124 exchange=$(echo "${response}" | xpath -q -e "/div/div[${i}]/div[1]/div[6]/div/div[1]/p" 2>/dev/null | sed 's/<[^>]*>//g') 125 [ "${exchange}" != "0.000" ] && [ -n "${exchange}" ] && echo "${exchange}" 126 done | sort | tail -n 1 127 ) 128 129 sell=$( 130 for i in $(seq 26); do 131 exchange=$(echo "${response}" | xpath -q -e "/div/div[${i}]/div[1]/div[6]/div/div[2]/p" 2>/dev/null | sed 's/<[^>]*>//g') 132 [ "${exchange}" != "0.000" ] && [ -n "${exchange}" ] && echo "${exchange}" 133 done | sort | head -n 1 134 ) 135 echo "${buy}" "${sell}" >/tmp/check_exchange_rate 136 fi 137 } 138 139 wm_status() { 140 # Update RSS 141 current_date="$(date +%s)" 142 143 # shellcheck disable=SC2154 144 next_update="$(($(stat -c %Y "${XDG_DATA_HOME}/newsboat/cache.db") + 1 * 60 * 60))" 145 if [ "${current_date}" -ge "${next_update}" ]; then 146 newsboat -x reload 147 fi 148 unset current_date next_update 149 150 if ! systemctl -q is-active cronie; then 151 echo "cronie unit is inactive" 152 echo 153 fi 154 155 if grep -q '\[s2idle\]' /sys/power/mem_sleep; then 156 cat /sys/power/mem_sleep 157 echo 158 fi 159 160 check_imca 161 check_exchange_rate 162 status_news 163 exchange_rate 164 } 165 166 print_hello() { 167 if [ -z "${TMUX}" ]; then 168 output_tmux=$(tmux list-sessions 2>/dev/null) 169 if [ -n "${output_tmux}" ]; then 170 echo "# tmux sessions" 171 echo "${output_tmux}" 172 echo 173 fi 174 unset output_tmux 175 fi 176 } 177 178 exchange_rate() { 179 read -r buy sell </tmp/check_exchange_rate 180 181 echo "# cuantoestaeldolar.pe" 182 { 183 echo "buy: ${buy}" 184 echo "sell: ${sell}" 185 } | column 186 187 unset response exchange 188 }