Class: Nehm::User
- Inherits:
-
Object
- Object
- Nehm::User
- Defined in:
- lib/nehm/user.rb
Constant Summary collapse
- SOUNDCLOUD_MAX_LIMIT =
Max limit of tracks for correct SoundCloud requests
180
Instance Method Summary collapse
-
#initialize(id) ⇒ User
constructor
A new instance of User.
- #likes(count) ⇒ Object
-
#posts(count) ⇒ Object
Post is last track/repost in profile.
Constructor Details
#initialize(id) ⇒ User
Returns a new instance of User.
9 10 11 |
# File 'lib/nehm/user.rb', line 9 def initialize(id) @id = id end |
Instance Method Details
#likes(count) ⇒ Object
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 |
# File 'lib/nehm/user.rb', line 13 def likes(count) # Method to_i return 0, if there aren't any numbers in string if count == 0 puts Paint['Invalid number of likes!', :red] exit end d = count / SOUNDCLOUD_MAX_LIMIT m = count % SOUNDCLOUD_MAX_LIMIT d = m == 0 ? d : d + 1 likes = [] d.times do |i| limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count count -= SOUNDCLOUD_MAX_LIMIT likes += Client.get("/users/#{@id}/favorites?limit=#{limit}&offset=#{(i)*SOUNDCLOUD_MAX_LIMIT}") end if likes.empty? puts Paint['There are no likes yet :(', :red] exit end likes.map! { |hash| Track.new(hash) } end |
#posts(count) ⇒ Object
Post is last track/repost in profile
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 |
# File 'lib/nehm/user.rb', line 41 def posts(count) # Method to_i return 0, if there aren't any numbers in string if count == 0 puts Paint['Invalid number of posts!', :red] exit end d = count / SOUNDCLOUD_MAX_LIMIT m = count % SOUNDCLOUD_MAX_LIMIT d = m == 0 ? d : d + 1 # Official SC API wrapper doesn't support posts # So I should get posts by HTTP requests conn = Faraday.new(url: 'https://api-v2.soundcloud.com/') posts = [] d.times do |i| limit = count > SOUNDCLOUD_MAX_LIMIT ? SOUNDCLOUD_MAX_LIMIT : count count -= SOUNDCLOUD_MAX_LIMIT response = conn.get("/profile/soundcloud:users:#{@id}?limit=#{limit}&offset=#{i*SOUNDCLOUD_MAX_LIMIT}") parsed = JSON.parse(response.body) collection = parsed['collection'] break if collection.nil? posts += collection end if posts.empty? puts Paint['There are no posts yet :(', :red] exit end rejected = posts.reject! { |hash| hash['type'] == 'playlist' } puts Paint["Was skipped #{rejected.length} playlist(s) (nehm doesn't download playlists)", :yellow] if rejected posts.map! { |hash| Track.new(hash['track']) } end |