summaryrefslogtreecommitdiffstats
path: root/soundcloud
blob: ccdd4158598001ab1cf13b8c1bed38f8a57c73dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /bin/sh

# Url Page -> Eff Play
play_stream() {
  echo "play: $1"
  mpv "$(get_streams "$1" | head -1)"
  play_stream "$(next_stream "$1")"
}

# Url Page -> Url Page
next_stream() {
  get_related "$1" | shuf | head -1
}

# Url Page -> Streams
get_streams() {(
  streams_url=$(streams_url "$(get "$1")")
  get "$streams_url" | jq -r 'to_entries[] | "\(.key) \(.value)"' | grep -v ^preview | awk '{print$2}'
)}

# Url Page -> [Url Page]
get_related() {(
  page_url=$1
  track_path=${page_url#https://soundcloud.com}
  related_url=$1/recommended
  related=$(get "$related_url")
  echo "$related" | sed -n 's/<a itemprop="url" href="\([^"]*\)">/\ntrack_path \1\n/gp' \
    | awk \
          -v track_path="$track_path" \
          '
          ($1 == "track_path" && $2 != track_path) {print"https://soundcloud.com"$2}
          '

)}

# Page -> Url Streams
streams_url() {(
  page=$1
  app_url=$(app_url "$page")
  app=$(get "$app_url")
  public_api_host=$(public_api_host "$app")
  stream_id=$(stream_id "$page")
  client_id=$(client_id "$app")
  streams_path=$(paths "$app" | sed -n "/streams$/{s/:id/$stream_id/p;T;q}")
  app_version=$(app_version "$page")
  streams_url="$public_api_host$streams_path?client_id=$client_id&app_version=$app_version"
  echo "$streams_url"
)}

get() {
  curl -fsS "$1"
}

# Page -> Url App
app_url() {
  echo "$1" | sed -n 's|.*"\(https:[^"]*/app-[^"]*\.js\)".*|\1|p'
}

# Page -> StreamId
stream_id() {
  echo "$1" | sed -n 's|.*"soundcloud://sounds:\([0-9]\+\)".*|\1|p;T;q'
}

# App -> ClientId
client_id() {
  echo "$1" | sed -n 's|.*\<client_id:"\([^"]\+\)".*|\1|p;T;q'
}

# Page -> AppVersion
app_version() {
  echo "$1" | sed -n 's|.*__sc_version\s*=\s*"\([^"]*\)".*|\1|p;T;q'
}

# App -> [PathPattern]
paths() {
  echo "$1" | sed -n 's|\<path:"[^"]*"|\n&\n|gp' | sed -n 's/^path:"\([^"]*\)"$/\1/p'
}

# App -> Url
public_api_host() {
  echo "$1" | sed -n 's|.*\<public_api_host:\s*"\([^"]*\)".*|\1|p;T;q'
}