Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions APIs/amazon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand All @@ -12,4 +12,4 @@ echo ${1// /"%20"}

url="https://completion.amazon.com/search/complete?search-alias=aps&mkt=1&q=$(urlencode "$*")"
res=$(jq -r '.[1][]' <<< "$(curl -s "$url" &)" | tr -d '"' )
echo "$res"
echo "$res"
4 changes: 2 additions & 2 deletions APIs/archwiki
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand All @@ -12,4 +12,4 @@ limit=10

url="https://wiki.archlinux.org/api.php?action=opensearch&format=json&formatversion=2&search=$(urlencode "$*")&namespace=0%7C3000&limit=$limit&suggest=true"
res=$(jq -r '.[1][]' <<< "$(curl -s "$url" &)" | tr -d '"' )
echo "$res"
echo "$res"
2 changes: 1 addition & 1 deletion APIs/baidu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

url="http://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=31726,1468,31672,21112,31111,31591,31605,31464,30823&wd=$(urlencode "$*")&csor=4&pwd=isn"
Expand Down
4 changes: 2 additions & 2 deletions APIs/ddg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand All @@ -12,4 +12,4 @@ lang="us-en"

url="https://ac.duckduckgo.com/ac/?q=$(urlencode "$*")&type=list&kl=$lang"
res=$(jq -r '.[1][]' <<< "$(curl -s "$url" &)" | tr -d '"' | sed 's/\(.*\),/\1 /')
echo "$res"
echo "$res"
2 changes: 1 addition & 1 deletion APIs/deezer
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/Belkadev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

# settings
Expand Down
2 changes: 1 addition & 1 deletion APIs/google
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/Belkadev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand Down
2 changes: 1 addition & 1 deletion APIs/googlebooks
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ orderBy="relevance" # {relevance,newest}

# doc url https://developers.google.com/books/docs/v1/using
urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}


Expand Down
70 changes: 35 additions & 35 deletions APIs/lastfm
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,68 @@ LASTFM_USER="" # useless
LASTFM_API_KEY="" # get from here: https://www.last.fm/api/account/create
search="album" # accepted: {album,track,artist}
similar=false # returns recs based on your search
# instead of matching the result (only for artists)
# values: {true,false}
# instead of matching the result (only for artists)
# values: {true,false}

catchError() {
if [[ $? != 0 ]]; then
notify-send "error reaching servers" ;
exit
fi
notify-send "error reaching servers" ;
exit
fi
}

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

updateUrl () {
unset URL
URL="http://ws.audioscrobbler.com/2.0/?method=$1"; shift
URL+="&api_key=$API_KEY&format=json"
URL+="&$@"
unset URL
URL="http://ws.audioscrobbler.com/2.0/?method=$1"; shift
URL+="&api_key=$API_KEY&format=json"
URL+="&$@"
}

[ -z LASTFM_API_KEY ] && echo "please enter your API key first" && exit 1
######## ARTISTS
function searchArtist {
URL= updateUrl "artist.search" "artist=$(urlencode "$1")"
artists=$(jq '.results.artistmatches.artist[].name' <<< "$(curl -s $URL)" | tr -d '"')
catchError
[ -z "$artists" ] && notify-send "nothing found" && exit 1
echo $artists
URL= updateUrl "artist.search" "artist=$(urlencode "$1")"
artists=$(jq '.results.artistmatches.artist[].name' <<< "$(curl -s $URL)" | tr -d '"')
catchError
[ -z "$artists" ] && notify-send "nothing found" && exit 1
echo $artists
}
function getRecs {
method=$1; shift
URL= updateUrl "$method.getsimilar" "$method=$(urlencode "$1")"
recs=$(jq '.similarartists.artist[].name' <<< "$(curl -s $URL)" | tr -d '"')
catchError
[ -z "$recs" ] && notify-send "nothing found" && exit 1
echo $recs
method=$1; shift
URL= updateUrl "$method.getsimilar" "$method=$(urlencode "$1")"
recs=$(jq '.similarartists.artist[].name' <<< "$(curl -s $URL)" | tr -d '"')
catchError
[ -z "$recs" ] && notify-send "nothing found" && exit 1
echo $recs
}
######## ALBUMS
function searchAlbum {
URL= updateUrl "album.search" "album=$(urlencode "$*")"
albums=$(jq '.results.albummatches.album[] | "\(.artist) - \(.name)"' <<< "$(curl -s $URL)" | tr -d '"' )
catchError
[ -z "$albums" ] && notify-send "nothing found" && exit 1
echo "$albums"
URL= updateUrl "album.search" "album=$(urlencode "$*")"
albums=$(jq '.results.albummatches.album[] | "\(.artist) - \(.name)"' <<< "$(curl -s $URL)" | tr -d '"' )
catchError
[ -z "$albums" ] && notify-send "nothing found" && exit 1
echo "$albums"
}
######## TRACKS
function searchTrack {
URL= updateUrl "track.search" "track=$(urlencode "$*")"
tracks=$(jq '.results.trackmatches.track[] | "\(.artist) - \(.name)\n"' <<< "$(curl -s $URL)" | tr -d '"' )
catchError
[ -z "$tracks" ] && notify-send "nothing found" && exit 1
echo $tracks
URL= updateUrl "track.search" "track=$(urlencode "$*")"
tracks=$(jq '.results.trackmatches.track[] | "\(.artist) - \(.name)\n"' <<< "$(curl -s $URL)" | tr -d '"' )
catchError
[ -z "$tracks" ] && notify-send "nothing found" && exit 1
echo $tracks
}

input="$*"
case "$search" in
"album") res=$(searchAlbum "$search") ;;
"track") res=$(searchTrack "$search") ;;
"artist") [[ "$recs" == "true" ]] && res=$(searchArtist "$search") || res=$(getRecs "$search") ;;
"album") res=$(searchAlbum "$search") ;;
"track") res=$(searchTrack "$search") ;;
"artist") [[ "$recs" == "true" ]] && res=$(searchArtist "$search") || res=$(getRecs "$search") ;;

*) exit 1 ;;
*) exit 1 ;;
esac

wait
Expand Down
2 changes: 1 addition & 1 deletion APIs/wikipedia
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand Down
2 changes: 1 addition & 1 deletion APIs/youtube
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Author: https://github.com/BelkaDev

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

#settings
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ of the wrapper script:

``` Bash
case "$API" in
"google") $BROWSER "https://www.google.com/search?q=$result" ;;
"deezer" | "lastfm") play "$selection" ;;
"youtube") youtube "$selection" ;;
"books") getBook "$selection" ;;
"google") $BROWSER "https://www.google.com/search?q=$result" ;;
"deezer" | "lastfm") play "$selection" ;;
"youtube") youtube "$selection" ;;
"books") getBook "$selection" ;;
esac
```

Expand Down
58 changes: 29 additions & 29 deletions handler
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,44 @@ logfile="$(dirname $0)/logfile.tmp"
API=$(cat $logfile)
rm "$logfile"
case "$API" in
"ddg") search="$ddg_api" ; allowExcess=true ;;
"deezer") search="$deezer_api" ;;
"youtube") search="$youtube_api" ; allowExcess=true ;;
"google") search="$google_api" ; allowExcess=true ;;
"books") search="$googlebooks_api" ;;
"lastfm") search="$lastfm_api" ;;
"wikipedia") search="$wikipedia_api" allowExcess=true ;;
"amazon") search="$amazon_api" ; allowExcess=true ;;
"archwiki") search="$archwiki_api" ; allowExcess=true ;;
"baidu") search="$baidu_api" ; allowExcess=true ;;
*) echo "unrecognized api" && exit 1 ;; # unreachable
"ddg") search="$ddg_api" ; allowExcess=true ;;
"deezer") search="$deezer_api" ;;
"youtube") search="$youtube_api" ; allowExcess=true ;;
"google") search="$google_api" ; allowExcess=true ;;
"books") search="$googlebooks_api" ;;
"lastfm") search="$lastfm_api" ;;
"wikipedia") search="$wikipedia_api" allowExcess=true ;;
"amazon") search="$amazon_api" ; allowExcess=true ;;
"archwiki") search="$archwiki_api" ; allowExcess=true ;;
"baidu") search="$baidu_api" ; allowExcess=true ;;
*) echo "unrecognized api" && exit 1 ;; # unreachable
esac

suggestions=""
default_custom_format="{{name_enum}}:{{value}}"
custom_format="${format:-$default_custom_format}"

urlencode() {
echo ${1// /"%20"}
echo ${1// /"%20"}
}

convertJson(){
echo "$1" | sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/.*/"&"/' | paste -sd "," -
}

removeIllegal(){
##### https://github.com/fogine/rofi-blocks/commit/9f45da637baf0f0d342c2e2957535564aa622164
##### if you care about special characters, the patch above
##### fixes the issue in which they break rofi
##### once installed you can omit this function
echo "$1" | tr -dc '[:alnum:][:space:]-\n\r'
##### https://github.com/fogine/rofi-blocks/commit/9f45da637baf0f0d342c2e2957535564aa622164
##### if you care about special characters, the patch above
##### fixes the issue in which they break rofi
##### once installed you can omit this function
echo "$1" | tr -dc '[:alnum:][:space:]-\n\r'
}

fill_menu(){

JSON_LINES="$(convertJson "$suggestions")"

TEXT=$(cat <<EOF | tr -d "\n" | tr -d "\t"
TEXT=$(cat <<EOF | tr -d "\n" | tr -d "\t"
{
"event format":"${custom_format}",
"lines":[
Expand All @@ -72,24 +72,24 @@ fill_menu(){
}
EOF
)
printf '%s\n' "$TEXT"
printf '%s\n' "$TEXT"
}

echo '{"input action":"send"}'

unset length i
while IFS= read -r line; do
((i++))
[[ "$line" = 'SELECT'* ]] && selected=$(echo "$line" | cut -d':' -f2-) && break;
print=$(echo "$line" | cut -d':' -f2-)
#custom algorithm to reduce Api calls
[[ "${#print}" < $length ]] && length="${#print}" && continue;
length="${#print}"
if (( i%refreshrate == 0 )) || [ ! -z $allowExcess ]; then suggestions="$(removeIllegal "$(bash $search $print)")"
((i++))
[[ "$line" = 'SELECT'* ]] && selected=$(echo "$line" | cut -d':' -f2-) && break;
print=$(echo "$line" | cut -d':' -f2-)
#custom algorithm to reduce Api calls
[[ "${#print}" < $length ]] && length="${#print}" && continue;
length="${#print}"
if (( i%refreshrate == 0 )) || [ ! -z $allowExcess ]; then suggestions="$(removeIllegal "$(bash $search $print)")"

[[ "${line:0:1}" != '{' ]] && suggestions="${print}"$'\n'"$suggestions"
fill_menu "$print"
fi
[[ "${line:0:1}" != '{' ]] && suggestions="${print}"$'\n'"$suggestions"
fill_menu "$print"
fi
done
selected="$(tr '[:lower:]' '[:upper:]' <<< ${selected:0:1})${selected:1}"
[ -z "$selected" ] && rm $logfile || printf "$selected" >"$logfile"
Expand Down
24 changes: 12 additions & 12 deletions suggest
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ API="$1"
#Full path to the handler script
handler=$(dirname $0)/handler

[ ! -f "$handler" ] && echo "invalid path to handler script" && exit 1
[ ! -f "$handler" ] && echo "invalid path to handler script" && exit 1
case "$API" in
"google") prompt="Google: " ;;
"deezer") prompt="Music: " ;;
"youtube") prompt="Youtube: " ;;
"lastfm") prompt="Music: " ;;
"books") prompt="Books: " ;;
"wikipedia") prompt="Wikipedia: " ;;
"ddg") prompt="Ddg: " ;;
"amazon") prompt="Amazon: " ;;
"archwiki") prompt="Archwiki: " ;;
"baidu") prompt="Baidu: " ;;
*) echo "unrecognized API" && exit 1 ;;
"google") prompt="Google: " ;;
"deezer") prompt="Music: " ;;
"youtube") prompt="Youtube: " ;;
"lastfm") prompt="Music: " ;;
"books") prompt="Books: " ;;
"wikipedia") prompt="Wikipedia: " ;;
"ddg") prompt="Ddg: " ;;
"amazon") prompt="Amazon: " ;;
"archwiki") prompt="Archwiki: " ;;
"baidu") prompt="Baidu: " ;;
*) echo "unrecognized API" && exit 1 ;;
esac
###############
logfile="$(dirname $handler)/logfile.tmp" # logfile to communicate between two ends
Expand Down