Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added group import script

  • Loading branch information...
commit 1e64533335935e45746624d308ad05f02582cd05 1 parent d633059
@leemartin leemartin authored
Showing with 58 additions and 1 deletion.
  1. +5 −1 app/models/track.rb
  2. +53 −0 lib/tasks/import.rake
View
6 app/models/track.rb
@@ -37,7 +37,11 @@ def next
private
def add_to_group
- user.soundcloud.put("/groups/#{competition.group}/contributions/#{tid}") if competition.group
+ begin
+ user.soundcloud.put("/groups/#{competition.group}/contributions/#{tid}") if competition.group
+ rescue SoundCloud::ResponseError
+ puts "Could not put track in group."
+ end
end
def remove_from_group
View
53 lib/tasks/import.rake
@@ -0,0 +1,53 @@
+namespace :competition do
+ desc "Import tracks from SoundCloud Group"
+ task :import => :environment do
+
+ # rake competition:import GROUP=http://soundcloud.com/groups/test
+
+ @group = HTTParty.get("http://api.soundcloud.com/resolve?url=#{ENV["GROUP"]}&consumer_key=#{Settings.key}&format=json")
+
+ if @group.response.code == "404"
+
+ puts "Group Not Found"
+
+ else
+
+ puts "Group Found"
+
+ # Search for Tracks
+
+ end_of_group = false
+ offset = 0
+
+ until end_of_group do
+
+ tracks = HTTParty.get("http://api.soundcloud.com/groups/#{@group["id"]}/tracks.json?consumer_key=#{Settings.key}&offset=#{offset}")
+
+ puts "#{tracks.length} Tracks Found"
+
+ end_of_group = true if tracks.length == 0
+
+ for track in tracks
+ if track["sharing"] == "public"
+
+ # Find or Create User
+ user = User.find_or_initialize_by_uid(track["user"]["id"])
+
+ user.update_attributes({
+ :name => "",
+ :username => track["user"]["username"],
+ :permalink => track["user"]["permalink"],
+ :avatar_url => track["user"]["avatar_url"]
+ })
+
+ # Identify or Create Track
+ track = user.tracks.identify_or_create_from_soundcloud(track)
+ end
+ end
+
+ offset += 50
+
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.