From 6156db64bd38f5d3ba299c77df6268a1afed35f1 Mon Sep 17 00:00:00 2001 From: Xiao Pan Date: Thu, 9 Feb 2023 16:50:19 -0800 Subject: init --- README.md | 5 +++++ sh/convwall | 35 +++++++++++++++++++++++++++++ sh/curlkg | 52 ++++++++++++++++++++++++++++++++++++++++++ sh/curlncm | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sh/matlab | 31 +++++++++++++++++++++++++ sh/shufwall | 9 ++++++++ 6 files changed, 207 insertions(+) create mode 100644 README.md create mode 100755 sh/convwall create mode 100755 sh/curlkg create mode 100755 sh/curlncm create mode 100755 sh/matlab create mode 100755 sh/shufwall diff --git a/README.md b/README.md new file mode 100644 index 0000000..99dc029 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +My not maintained, not used, archived codes/configs. + +# Not working codes/configs: + +curlkg diff --git a/sh/convwall b/sh/convwall new file mode 100755 index 0000000..8efd136 --- /dev/null +++ b/sh/convwall @@ -0,0 +1,35 @@ +#!/bin/sh +# CONVert WALLpaper + +# target image aspect ratio +wratio=$SCR_WIDTH +hratio=$SCR_HEIGHT +# chop how much from center +chopnum=99% + +# my benchmark shows -ping is much faster than default +# https://github.com/ImageMagick/ImageMagick/issues/3183#issuecomment-800955241 +width=$(identify -ping -format '%w' "$1") +height=$(identify -ping -format '%h' "$1") + +ratio=$((wratio*100/hratio)) +dim=$((width*100/height)) + +if [ "$dim" -ne "$ratio" ]; then + if [ "$dim" -lt "$ratio" ]; then + chopgeom="${chopnum}x0" + width=$((height*wratio/hratio)) + else + chopgeom="0x$chopnum" + height=$((width*hratio/wratio)) + fi + # may throw error "sort: write failed: 'standard output': Broken pipe \n sort: write error" + # caused by head exit (or close stdin) before sort output complete, can ignore + # https://stackoverflow.com/questions/46202653/bash-error-in-sort-sort-write-failed-standard-output-broken-pipe + bgcolor=$(convert "$1" -gravity center -chop "$chopgeom" -define histogram:unique-colors=true -format %c histogram:info:- | sort -rn | head -n1 | awk \{print\ \$3\} | cut -c1-7) + # another approach is to use "$width" or "x$height" for geometry, current approach is more readable + convert "$1" -gravity center -background "$bgcolor" -extent "${width}x$height" "$2" +else + echo 'same aspect ratio, no need to convert' >&2 + exit 1 +fi diff --git a/sh/curlkg b/sh/curlkg new file mode 100755 index 0000000..4276e82 --- /dev/null +++ b/sh/curlkg @@ -0,0 +1,52 @@ +#!/bin/sh + +# references: +# https://blog.csdn.net/qq_41730930/article/details/79327757 +# https://blog.csdn.net/u011112876/article/details/89634505 +# https://github.com/pystardust/ytfzf + +homepage_url='https://node.kg.qq.com/cgi/fcgi-bin/kg_ugc_get_homepage' +music_url='https://node.kg.qq.com/cgi/fcgi-bin/fcg_get_play_url' +# num seems only 8 and 15 works +num=15 +tmp_file="$(mktemp)" + +get_json () { + curl -s -G --data-urlencode type=get_uinfo -d outCharset=utf-8 -d start="$1" -d num=$num -d share_uid="$uid" "$homepage_url" | sed 's/.*({\(.*\)}).*/{\1}/' +} + +# concurrent file append with less than 4096 bytes will be atomic on arch linux ext4 +# so need to process json data before append to the file, to append less than 4096 bytes for each process +# another appraoch is to use a loop to write to several files each corresponding to one process +# https://stackoverflow.com/questions/1154446/is-file-append-atomic-in-unix +# https://unix.stackexchange.com/questions/458653/parallel-processes-appending-outputs-to-an-array-in-a-bash-script +process_json() { + jq -r '.data.ugclist[]|"'"$music_url"'?shareid=\(.shareid)\n out=\(.title)_\(.time).m4a"' >> "$tmp_file" +} + +while getopts u: opt; do + case $opt in + u) uid="$OPTARG";; + \?) exit 1;; + esac +done +# for lan lan, uid=649b9e82272a348b +# for cheng ruan, uid=64949d822c25328c +[ -z "$uid" ] && echo 'error: must provide share_uid +ex: curlkg -u 649b9e82272a348b ./' >&2 && exit 1 +shift $((OPTIND-1)) +download_dir="${1:-"$PWD"}" +[ -d "$download_dir" ] || mkdir -p "$download_dir" + +first_json="$(get_json 1)" +ugc_total_count=$(echo "$first_json" | jq '.data.ugc_total_count') +echo "$first_json" | process_json +# can also use while loop with i=$((i+1)) +# the calculation considers both ugc_total_count%num==0 and ugc_total_count%num>0 +for i in $(seq 2 $(((ugc_total_count+num-1)/num))); do + get_json "$i" | process_json & +done + +wait +aria2c -d "$download_dir" --auto-file-renaming=false --console-log-level=warn -i "$tmp_file" +rm "$tmp_file" diff --git a/sh/curlncm b/sh/curlncm new file mode 100755 index 0000000..25f39aa --- /dev/null +++ b/sh/curlncm @@ -0,0 +1,75 @@ +#!/bin/sh +# rewrite TianyiShi2001's python script which rewrite NeteaseCloudMusicApi node.js api + +# references: +# https://github.com/ytdl-org/youtube-dl/issues/18051#issuecomment-859964832 +# https://github.com/Binaryify/NeteaseCloudMusicApi +# info about encrytion and api: +# https://github.com/metowolf/NeteaseCloudMusicApi/wiki +# https://github.com/darknessomi/musicbox/wiki + +# lan lan, aid=48860966, rid=793052426, example id=1397315179,1817498734 +# cheng ruan, aid=46703185, rid=792968433 + +# trial and error to get dj_max, don't know why, maybe because it is 2^31 and it is the size limit of whatever type of the variable +dj_max=2147483647 +dl_urls_tmp="$(mktemp)" +# printf 'e82ckenh8dichen8' | hexdump -ve '/1 "%02x"' +key='653832636b656e683864696368656e38' +url='/api/song/enhance/player/url' +request_id=$(date +'%s%3N')_$(seq -w 1 1000 | shuf -n1) +user_agent='Mozilla/5.0 (Linux; U; Android 9; zh-cn; Redmi Note 8 Build/PKQ1.190616.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/12.5.22' +cookie="$(printf '"appver":"8.0.0","versioncode":"140","buildver":"1623455100","resolution":"1920x1080","__csrf":"","os":"pc","requestId":"%s"' "$request_id")" + +die () { + echo "error: $*" >&2 + exit 1 +} + +# artist, song, djradio id +while getopts a:i:r: opt; do + case $opt in + a) aid="$OPTARG";; + i) ids="$OPTARG";; + r) rid="$OPTARG";; + \?) exit 1;; + esac +done +shift $((OPTIND-1)) +download_dir="${1:-"$PWD"}" +[ -d "$download_dir" ] || mkdir -p "$download_dir" + +if [ "$ids" ]; then + # temporary solution, can improve by auto curl names + data="$(echo "$ids" | tr ',' '\n' | awk '{printf("%s\t%s\n",$0,"songname")}')" +else + if [ "$rid" ]; then + data="$(curl -s -G --data-urlencode id="$rid" -d limit="$dj_max" 'https://music.163.com/djradio' | grep 'songlist\|tt f-thide' | sed -e 's/.*songlist-\(.*\)" class.*/\1/g' -e 's/.*title="\(.*\)".*/\1/g' | paste -sd '\t\n' | sort | tr '/' '_')" + # can only curl featured 50 songs for the artist, can improve + elif [ "$aid" ]; then + data="$(curl -s -G --data-urlencode id="$aid" 'https://music.163.com/artist' | grep '\[{.*}\]' | sed -e 's/.*>\[{/\[{/' -e 's/}\]<.*/}\]/' | jq -r '.[]|[.id,.name]|@tsv' | sort | tr '/' '_')" + fi + # awk code not print separator at beginning and end steal from: + # https://unix.stackexchange.com/a/581785/459013 + # not sure awk one-liner or paste way which is better, need benchmark + ids="$(echo "$data" | awk -F'\t' '{printf "%s%s",sep,$1;sep=","}')" + #ids="$(echo "$data" | awk -F'\t' '{print $1}' | paste -sd ',')" +fi + +# I don't fully understand following several lines of code +# I rewrite TianyiShi2001's python script, he rewrites NeteaseCloudMusicApi +text="$(printf '{"ids":"[%s]","br":999000,"header":{%s}}' "$ids" "$cookie")" +message="nobody${url}use${text}md5forencrypt" +digest="$(printf '%s' "$message" | openssl dgst -md5 -hex | awk '{print $2}')" +params="$(printf '%s-36cd479b6b5-%s-36cd479b6b5-%s' "$url" "$text" "$digest")" +encrypted_params="$(printf '%s' "$params" | openssl enc -aes-128-ecb -K "$key" | hexdump -ve '/1 "%02X"')" +# curl default user agent header seems not working +curl -s -A "$user_agent" -d params="$encrypted_params" 'https://interface3.music.163.com/eapi/song/enhance/player/url' | jq -r '.data|sort_by(.id)|.[].url' > "$dl_urls_tmp" +[ "$(echo "$data" | wc -l)" -ne "$(wc -l < "$dl_urls_tmp")" ] && die "number of download urls doesn't match request" + +# can't download some music if live abroad, may need proxy or vpn +# using user_agent, content-type header, cookie header, referer are not necessary? but feels faster +# not sure about cookie header format for aria2c, this link shows that it's kinda messy, so I didn't use it +# https://github.com/aria2/aria2/issues/545#issuecomment-650070869 +echo "$data" | awk -F'\t' '{printf(" out=%s_%s.mp3\n",$2,$1)}' | paste "$dl_urls_tmp" - -d '\n' | aria2c -U "$user_agent" --header='Content-Type: application/x-www-form-urlencoded' --referer='https://music.163.com' -d "$download_dir" --auto-file-renaming=false --console-log-level=warn -i- +rm "$dl_urls_tmp" diff --git a/sh/matlab b/sh/matlab new file mode 100755 index 0000000..9df9e99 --- /dev/null +++ b/sh/matlab @@ -0,0 +1,31 @@ +#!/bin/sh + +# for matlab version R2022a +# https://wiki.archlinux.org/title/MATLAB#Blank/grey_UI_when_using_WM_(non-reparenting_window_manager) +# https://wiki.archlinux.org/title/Java#Gray_window,_applications_not_resizing_with_WM,_menus_immediately_closing +# https://wiki.archlinux.org/title/MATLAB#Unable_to_launch_the_MATLABWindow_application +# https://faq.i3wm.org/question/2564/cant-open-matlab-from-dmenu.1.html +# https://wiki.archlinux.org/title/MATLAB#Desktop_entry +# use /tmp as working (startup) directory and save simulink files there to avoid cross filesystem hardlink error: +# https://bbs.archlinux.org/viewtopic.php?id=277970 + +cmd () { + _JAVA_AWT_WM_NONREPARENTING=1 LD_PRELOAD=/lib64/libfreetype.so exec "$bin_dir/matlab" -sd "$startup_dir" -desktop -nosplash "$@" +} + +startup_dir=/tmp/matlab +bin_dir="$HOME/programs/MATLAB/R2022a/bin" +addon_dir="$HOME/MATLAB Add-Ons" +[ -d "$startup_dir" ] || mkdir "$startup_dir" + +# my workaround for simulink gyroscope compile errors: +# "avr-gcc: error: I2Cdev.o: No such file or directory" (also for other .o files) +# "Code generation information file does not exist." (caused by no .o file error before I guess) +# I guess it is still cross filesystem hardlink error +# need to set path in matlab manually once for "$startup_dir/$addon_dir" sub dirs inside +[ -d "$startup_dir/$addon_dir" ] || cp -r "$addon_dir" "$startup_dir" + +if ! cmd "$@"; then + "$bin_dir/activate_matlab.sh" + cmd "$@" +fi diff --git a/sh/shufwall b/sh/shufwall new file mode 100755 index 0000000..5b249e1 --- /dev/null +++ b/sh/shufwall @@ -0,0 +1,9 @@ +#!/bin/sh + +if [ -z "$1" ]; then + file=$(find -L "$XDG_PICTURES_DIR/wallpapers" -type f | shuf -n 1) +else + file=$(find -L "$@" -maxdepth 1 -type f | shuf -n 1) +fi + +display -resize "${SCR_WIDTH}x$SCR_HEIGHT" -backdrop -window root "$file" -- cgit 1.4.1