-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundcloud_likes_export.rb
More file actions
executable file
·82 lines (69 loc) · 1.87 KB
/
Copy pathsoundcloud_likes_export.rb
File metadata and controls
executable file
·82 lines (69 loc) · 1.87 KB
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
# add a new app at http://soundcloud.com/you/apps and get your CLIENT_ID
require 'soundcloud'
require 'open-uri'
require 'csv'
require 'whirly'
CLIENT_ID = "a3e059563d7fd3372b49b37f00a00bcf" # Client ID of created app
username = ARGV[0] # Your username
return if username.nil?
page_size = 200
@res = []
@client = SoundCloud.new(:client_id => CLIENT_ID)
tracks = @client.get("/users/#{username}/favorites", limit: page_size, order: 'created_at', linked_partitioning: 1)
loop do
if tracks.next_href
@res += tracks.collection
tracks = @client.get(tracks.next_href)
else
@res += tracks.collection
break
end
end
# save all tracks to csv likes.csv
def export_csv(tracks)
CSV.open("likes.csv", 'wb') do |csv|
tracks.uniq.each do |track|
csv << [track.title, track.permalink_url]
end
end
end
def format_name(name)
replacements = [ ["/", "-"], ["!", ""] ]
replacements.each { |r| name.gsub!(r[0], r[1]) }
name
end
def download_track(track_id, name)
Dir.mkdir("likes") unless Dir.exists?("likes")
track_path = "./likes/#{format_name(name)}.mp3"
unless File.exists?(track_path)
url = @client.get("/tracks/#{track_id}/streams").http_mp3_128_url
if url
File.open(track_path, "wb") do |mp3|
mp3 << open(url, 'rb').read
end
end
end
Whirly.status = "Downloaded #{name}"
end
# download all tracks to /likes folder
def batch_download
tracks = @res.uniq.map { |track| [ track.id, track.title ] }
threads = []
Whirly.start spinner: "dots", status: "Downloading..." do
tracks.each_slice(8) do |batch|
batch.each do |track|
threads << Thread.new { download_track(track[0], track[1]) }
end
end
threads.each { |thread| thread.join }
end
end
case
when ARGV.include?("-csv")
export_csv(@res)
when ARGV.include?("-dl")
batch_download
else
export_csv(@res)
batch_download
end