Class: MusicMetadataEnrichment::Group

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/music_metadata_enrichment/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#artist_connections_textObject

Returns the value of attribute artist_connections_text.



19
20
21
# File 'app/models/music_metadata_enrichment/group.rb', line 19

def artist_connections_text
  @artist_connections_text
end

#user_idObject

Returns the value of attribute user_id.



19
20
21
# File 'app/models/music_metadata_enrichment/group.rb', line 19

def user_id
  @user_id
end

Instance Method Details

#import_artist_connectionsObject



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
# File 'app/models/music_metadata_enrichment/group.rb', line 23

def import_artist_connections
  artist_names_without_mbid = []
  artist_names = artist_connections_text.split("\n").map(&:strip)
  
  artist_names.each do |artist_name|
    lastfm = Lastfm.new(LastfmApiKey, LastfmApiSecret)
    lastfm_artist = lastfm.artist.get_info(artist: artist_name)
    
    if lastfm_artist['mbid'].blank?
      artist_names_without_mbid << lastfm_artist['name']
      
      next
    end
    
    artist_ids = MusicArtist.create_by_name(artist_name)
    
    next if artist_ids.none?
        
    artist_connection_artist_ids = artist_connections.where(artist_id: artist_ids).map(&:artist_id)
    artist_ids = artist_ids.select{|id| !artist_connection_artist_ids.include?(id) }
            
    if artist_ids.any?
      artist_ids.each do |artist_id|
        MusicMetadataEnrichment::GroupArtistConnection.create(group_id: id, artist_id: artist_id)
      end
    end
  end
  
  artist_names_without_mbid
end

#import_artists_of_group_members(options = {}) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/music_metadata_enrichment/group.rb', line 54

def import_artists_of_group_members(options = {})
  last_page = options[:last_page]
  last_user_name_from_last_run = options[:last_user_name_from_last_run]
  library_start_page = options[:library_start_page]
  
  user_names, page, last_user_name, last_user_name_from_last_run_reached, i = [], nil, nil, false, 1
  
  lastfm = Lastfm.new(LastfmApiKey, LastfmApiSecret)
  
  1000.times do |page|
    page +=1
    
    if last_page.present? && page == last_page
      i = (page * 50) + 1
    elsif last_page.present? && page < last_page
      next
    end
  
    puts "GROUP MEMBERS PAGE: #{page}"
    
    lastfm_users = MusicArtist.lastfm_request_class_method(lastfm, :group, :get_members, nil, group: 'Dark Electro', page: page)
    sleep 3
    
    if lastfm_users.select{|u| !user_names.include?(u['name'])}.none?
      # over last page
      break
    end
  
    lastfm_users.each do |lastfm_user|
      if user_names.include?(lastfm_user['name'])
        next
      else
        user_names << lastfm_user['name']
      end
  
      if lastfm_user['name'] == last_user_name_from_last_run
        last_user_name_from_last_run_reached = true
      elsif last_user_name_from_last_run.present? && !last_user_name_from_last_run_reached
        i += 1
        next
      end
      
      last_user_name = lastfm_user['name']
      puts "USER #{i}: #{last_user_name} (GROUP MEMBERS PAGE: #{page})"
      user = User.new
      user.group_page = page
      user.lastfm_user_name = lastfm_user['name']
  
      if lastfm_user['name'] == last_user_name_from_last_run && !library_start_page.nil?
        user.import_music_artists(lastfm, library_start_page)
      else
        user.import_music_artists(lastfm)
      end
      
      i += 1
    end
    
    [page, last_user_name]
  end
end