Class: Ektoplayer::DatabaseUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/ektoplayer/updater.rb

Constant Summary collapse

ALBUM_STR_TAGS =
Set.new(%w(url title date cover_url
description download_count rating votes).map(&:to_sym)).freeze
TRACK_STR_TAGS =
Set.new(%w(url number title remix artist bpm).map(&:to_sym)).freeze

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ DatabaseUpdater

Returns a new instance of DatabaseUpdater.



17
18
19
# File 'lib/ektoplayer/updater.rb', line 17

def initialize(db)
   @db = db
end

Instance Method Details

#update(start_url: FREE_MUSIC_URL, pages: 0, parallel: 10) ⇒ Object



21
22
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
# File 'lib/ektoplayer/updater.rb', line 21

def update(start_url: FREE_MUSIC_URL, pages: 0, parallel: 10)
   queue = parallel > 0 ? SizedQueue.new(parallel) : Queue.new 
   insert_browserpage(bp = BrowsePage.new(start_url))
   results = Queue.new

   if pages > 0
      bp.page_urls[(bp.current_page_index + 1)..(bp.current_page_index + pages + 1)]
   else
      bp.page_urls[(bp.current_page_index + 1)..-1]
   end.
   each do |url|
      queue << Thread.new do
         results << BrowsePage.new(url)
         queue.pop # unregister our thread
      end.priority

      if results.size > 40
         @db.transaction
         40.times { insert_browserpage(results.pop(true)) }
         @db.commit
      end
   end

   sleep 1 while not queue.empty?

   @db.transaction
   while (result = queue.pop(true) rescue nil)
      insert_browserpage(result)
   end
   @db.commit
rescue
   Application.log(self, $!)
end