Created
September 28, 2012 18:40
-
-
Save xbora/3801461 to your computer and use it in GitHub Desktop.
Spotifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Spotifier | |
require 'hallon' | |
attr_accessor :user | |
def initialize(member) | |
appkey = IO.read('./bin/spotify_appkey.key') | |
if Hallon::Session.instance? | |
hallon = Hallon::Session.instance | |
hallon.logout! | |
hallon.forget_me! | |
hallon.login!(member.spotify_username, member.spotify_password) | |
else | |
Hallon::Session.initialize(appkey).tap do |hallon| | |
hallon.login!(member.spotify_username, member.spotify_password) | |
end | |
end | |
objects = [Hallon::User.new(member.spotify_username).load] | |
@user = Hallon::User.new(objects[0].to_str).load | |
end | |
def get_starred_artists | |
starred = @user.starred.load | |
starred_tracks = starred.tracks.map(&:load) | |
#artists = starred_tracks.collect{|t| t.artist.load.name unless t.artist.load.name.nil?} | |
artists = Array.new | |
starred_tracks.each do |t| | |
begin | |
artists << t.artist.load.name unless t.artist.load.name.nil? | |
rescue Exception => e | |
puts e | |
end | |
end | |
top_counts = Hash.new(0) | |
artists.each { |name| top_counts[name] += 1 } | |
unique_starred = Array.new | |
top_counts.each do |tc| | |
unique_starred << tc[0] if tc[1] > 1 | |
end | |
unique_starred | |
end | |
def get_playlist_artists(type) | |
published = @user.published.load | |
top_artists = Array.new | |
all_playlists(published).each do |playlist| | |
begin | |
tracks = playlist.tracks.to_a.map(&:load) | |
next if tracks.nil? || tracks[0].nil? || tracks[1].nil? | |
if type == MemberArtist::TOP_ARTIST | |
top_artists << tracks[0].artist.load.name if tracks[0].artist.load.name == tracks[1].artist.load.name | |
elsif type == MemberArtist::RECENT_ARTIST | |
top_artists << tracks[0].artist.load.name if recent_playlist(tracks) | |
end | |
rescue Exception => e | |
puts e | |
end | |
end | |
top_artists | |
end | |
protected | |
def all_playlists(published) | |
all_playlists = published.contents.find_all do |playlist| | |
playlist.is_a?(Hallon::Playlist) # ignore folders | |
end | |
all_playlists.each(&:load) | |
end | |
def recent_playlist(tracks) | |
(tracks[0].artist.load.name == tracks[1].artist.load.name) && (tracks[0].added_at > Date.today - 30.days) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment