📜 ⬆️ ⬇️

Looking for music on z-music.org with bash

It is clear that the “victim” of today's experiment will be z-music.org z-music.org . On this site you can find a song, listen and download it.
The goal is to write a bash script to search for music using the “gentleman's set” of sed , awk and grep . Well, plus a little curl or wget to taste.

Having studied z-music.org a z-music.org , it becomes clear that:
  1. The search is located at z-music.org/search/ . As a GET parameter, you can pass the query text q , the type of the query (AJAX) action and the page number of the search result page
  2. You can learn the bitrate of a song by sending a POST request to z-music.org/bitrate/ with the data "id=<SONG_ID>"
  3. Direct link to download the song looks like m1.z-music.org/t/<SONG_ID>_<HASH> . <HASH> found in the script at z-music.org/theme/new/js/lang.js

You can start writing a script. For server requests, I will use curl , but nothing prevents you from using wget . Instead of curl -s will be wget -q -O - , and instead of curl -F will be wget --post-data

To begin with, write <HASH> in the hsh variable:
 hsh=$(curl -s http://z-music.org/theme/new/js/lang.js | sed -n 's/^var hsh="\([^"]*\)";$/\1/p') 

The variables $query and $page will contain the query text and the page number of the result. Send a request to the server:
 curl -s "http://z-music.org/search/?page=$page&action=ajax&q=$query" 

We get a piece of HTML. Information about the song is located in lines like <a class="info" data-aid="SONG_ID"...>SONG_NAME</a> . Get <SONG_ID> and <SONG_NAME> separated by tabs:
 sed -n 's/^\s*<a class="info" data-aid="\([^"]*\)"[^>]*>\([^<]*\)<\/a>\s*$/\1\t\2/p' 

Knowing <SONG_ID> you can send a request to bitrate. JSON will be returned. In order to pull out the very value of bitrate, use the cut utility. For a change.
 curl -sF "id=$songid" http://z-music.org/bitrate/ | cut -d'"' -f8 

The link to download the song is as follows: m1.z-music.org/t/${songid}_${hsh}/
It remains to combine all this in one script. Let it be “page by page” to search for songs and display information only about those whose bitrate is the same as the specified one.
 #!/bin/bash case $# in 1) query=$1 bitrate=320 ;; 2) query=$1 bitrate=$2 ;; *) echo -e 'Usage:\n\tzmusic "song name"\nor:\n\tzmusic "song name" 256\nwhere 256 is bitrate' exit ;; esac hsh=$(curl -s http://z-music.org/theme/new/js/lang.js | sed -n 's/^var hsh="\([^"]*\)";$/\1/p') # infinite loop incrementing search page for (( page=1; page>0; page++ )) do result=$( \ curl -s "http://z-music.org/search/?page=$page&action=ajax&q=$query" | \ sed -n 's/^\s*<a class="info" data-aid="\([^"]*\)"[^>]*>\([^<]*\)<\/a>\s*$/\1\t\2/p' | \ awk -v hsh=$hsh -F"\t" ' \ { \ system("curl -sF \"id=" $1 "\" http://z-music.org/bitrate/ | cut -d\"\\\"\" -f8"); \ print "http://m1.z-music.org/t/" $1 "_" hsh "/"; \ print $2 "\n"; \ }') # stop the script if nothing found on this page if [ -z "$result" ]; then exit; fi # output songs with $bitrate only while read -r res; do echo -e $res; done <<< "$result" | grep -A2 "$bitrate" echo -e "-- end of page $page --\n" done 

We try to find something about habrakhabr:
 $ zmusic "Habr" "256 \ | 320"
 320
 http://m1.z-music.org/t/-3ev4fnuc23h_2b49895762/
 Habr - Addict song
 -
 320
 http://m1.z-music.org/t/-3ev4fnubytf_2b49895762/
 HaBr - Nedomassaraksh
 -
 320
 http://m1.z-music.org/t/-3ev4fnuc1d3_2b49895762/
 HaBr - Travel [Hedgehogs and Petruccio cover]
 -
 320
 http://m1.z-music.org/t/-3ev4fntmcfl_2b49895762/
 Habr - Do not take the elevator (new single)
 - end of page 1 -


')

Source: https://habr.com/ru/post/141924/


All Articles