From acec14612648932f46e581f8583276c35ee70310 Mon Sep 17 00:00:00 2001 From: danogentili Date: Wed, 15 Jun 2016 15:50:18 +0200 Subject: [PATCH] First commit. --- JSON.sh | 196 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 12 +++ audiokeychain.sh | 29 +++++++ 3 files changed, 237 insertions(+) create mode 100755 JSON.sh create mode 100644 README.md create mode 100755 audiokeychain.sh diff --git a/JSON.sh b/JSON.sh new file mode 100755 index 0000000..75f8715 --- /dev/null +++ b/JSON.sh @@ -0,0 +1,196 @@ +#!/usr/bin/env bash + +throw () { + echo "$*" >&2 + exit 1 +} + +BRIEF=0 +LEAFONLY=0 +PRUNE=0 +NORMALIZE_SOLIDUS=0 + +usage() { + echo + echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]" + echo + echo "-p - Prune empty. Exclude fields with empty values." + echo "-l - Leaf only. Only show leaf nodes, which stops data duplication." + echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options." + echo "-s - Remove escaping of the solidus symbol (stright slash)." + echo "-h - This help text." + echo +} + +parse_options() { + set -- "$@" + local ARGN=$# + while [ "$ARGN" -ne 0 ] + do + case $1 in + -h) usage + exit 0 + ;; + -b) BRIEF=1 + LEAFONLY=1 + PRUNE=1 + ;; + -l) LEAFONLY=1 + ;; + -p) PRUNE=1 + ;; + -s) NORMALIZE_SOLIDUS=1 + ;; + ?*) echo "ERROR: Unknown option." + usage + exit 0 + ;; + esac + shift 1 + ARGN=$((ARGN-1)) + done +} + +awk_egrep () { + local pattern_string=$1 + + gawk '{ + while ($0) { + start=match($0, pattern); + token=substr($0, start, RLENGTH); + print token; + $0=substr($0, start+RLENGTH); + } + }' pattern="$pattern_string" +} + +tokenize () { + local GREP + local ESCAPE + local CHAR + + if echo "test string" | egrep -ao --color=never "test" &>/dev/null + then + GREP='egrep -ao --color=never' + else + GREP='egrep -ao' + fi + + if echo "test string" | egrep -o "test" &>/dev/null + then + ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})' + CHAR='[^[:cntrl:]"\\]' + else + GREP=awk_egrep + ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})' + CHAR='[^[:cntrl:]"\\\\]' + fi + + local STRING="\"$CHAR*($ESCAPE$CHAR*)*\"" + local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?' + local KEYWORD='null|false|true' + local SPACE='[[:space:]]+' + + $GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$" +} + +parse_array () { + local index=0 + local ary='' + read -r token + case "$token" in + ']') ;; + *) + while : + do + parse_value "$1" "$index" + index=$((index+1)) + ary="$ary""$value" + read -r token + case "$token" in + ']') break ;; + ',') ary="$ary," ;; + *) throw "EXPECTED , or ] GOT ${token:-EOF}" ;; + esac + read -r token + done + ;; + esac + [ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value= + : +} + +parse_object () { + local key + local obj='' + read -r token + case "$token" in + '}') ;; + *) + while : + do + case "$token" in + '"'*'"') key=$token ;; + *) throw "EXPECTED string GOT ${token:-EOF}" ;; + esac + read -r token + case "$token" in + ':') ;; + *) throw "EXPECTED : GOT ${token:-EOF}" ;; + esac + read -r token + parse_value "$1" "$key" + obj="$obj$key:$value" + read -r token + case "$token" in + '}') break ;; + ',') obj="$obj," ;; + *) throw "EXPECTED , or } GOT ${token:-EOF}" ;; + esac + read -r token + done + ;; + esac + [ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value= + : +} + +parse_value () { + local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0 + case "$token" in + '{') parse_object "$jpath" ;; + '[') parse_array "$jpath" ;; + # At this point, the only valid single-character tokens are digits. + ''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;; + *) value=$token + # if asked, replace solidus ("\/") in json strings with normalized value: "/" + [ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=${value//\\\//\/} + isleaf=1 + [ "$value" = '""' ] && isempty=1 + ;; + esac + [ "$value" = '' ] && return + [ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1 + [ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1 + [ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1 + [ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \ + [ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1 + [ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value" + : +} + +parse () { + read -r token + parse_value + read -r token + case "$token" in + '') ;; + *) throw "EXPECTED EOF GOT $token" ;; + esac +} + +if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]); +then + parse_options "$@" + tokenize | parse +fi diff --git a/README.md b/README.md new file mode 100644 index 0000000..60e9885 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Audiokeychain.com client + +This repo contains a bash client for [audiokeychain.com](https://audiokeychain.com). + +The script in question is capable of recognizing the key and bpm of a song. + +Usage: +``` +./audiokeychain.sh song.mp3 +``` + +Created by [Daniil Gentili](https://daniil.it) diff --git a/audiokeychain.sh b/audiokeychain.sh new file mode 100755 index 0000000..ea2e082 --- /dev/null +++ b/audiokeychain.sh @@ -0,0 +1,29 @@ +#!/bin/bash +if [ "$1" = "" ]; then + echo "Usage: $0 music.mp3" + exit +fi + +sha256=$(sha256sum "$1" | sed 's/\s.*//g') +cookies="/tmp/audiokeychain"$sha256".txt" + +bucket=$(curl -q 'https://www.audiokeychain.com/bucket' -H 'Host: www.audiokeychain.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -c $cookies) + +if [ "$bucket" = "" ]; then + echo "Couldn't obtain bucket code" + exit 1 +fi + +uploadresult=$(curl -q 'https://www.audiokeychain.com/upload' -H 'Host: www.audiokeychain.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'X-Requested-With: XMLHttpRequest' -H 'Content-Type: multipart/form-data;' -H 'Connection: keep-alive' -F "XUploadForm[file]=@$1" -F "zwa=$bucket" -b $cookies | ./JSON.sh -s) + +if ! echo "$uploadresult" | grep -q 'name'; then + echo "Couldn't upload file." + exit 1 +fi + +result=$(curl -q "https://www.audiokeychain.com/queue/$bucket" -H 'Host: www.audiokeychain.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data 'errors=%5B%5D' -b $cookies | ./JSON.sh -s | sed '/^\["tracks",0\]\t/!d;s/^\["tracks",0\]\t//g;s/^"//g;s/"$//g;s/[<]\/tr[>].*/<\/tr>/g') + +printf "$result" + + +rm $cookies