Class: MusicMetadataEnrichment::Group
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MusicMetadataEnrichment::Group
- Defined in:
- app/models/music_metadata_enrichment/group.rb
Instance Attribute Summary collapse
-
#artist_connections_text ⇒ Object
Returns the value of attribute artist_connections_text.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
Instance Attribute Details
#artist_connections_text ⇒ Object
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_id ⇒ Object
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_connections ⇒ Object
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 |
# 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 = MusicArtist.where(mbid: lastfm_artist['mbid']).first unless artist musicbrainz_artist = MusicBrainz::Artist.find(lastfm_artist['mbid']) if musicbrainz_artist artist = MusicArtist.where(mbid: musicbrainz_artist.id).first artist = MusicArtist.create(name: lastfm_artist['name'], mbid: musicbrainz_artist.id) unless artist end end next unless artist if artist_connections.where(artist_id: artist.id).none? MusicMetadataEnrichment::GroupArtistConnection.create(group_id: id, artist_id: artist.id) end end artist_names_without_mbid end |
#import_artists_of_group_members(options = {}) ⇒ Object
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 114 115 116 117 |
# File 'app/models/music_metadata_enrichment/group.rb', line 58 def import_artists_of_group_members( = {}) last_page = [:last_page] last_user_name_from_last_run = [:last_user_name_from_last_run] library_start_page = [: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 |