Class: Ektoplayer::DatabaseUpdater
- Inherits:
-
Object
- Object
- Ektoplayer::DatabaseUpdater
- 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
-
#initialize(db) ⇒ DatabaseUpdater
constructor
A new instance of DatabaseUpdater.
- #update(start_url: FREE_MUSIC_URL, pages: 0, parallel: 10) ⇒ Object
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 54 55 56 57 58 59 60 61 62 63 |
# 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 results = Queue.new bp = BrowsePage.new(start_url) results << bp insert_thread = Thread.new do loop do result = results.pop @db.transaction insert_browserpage(result) @db.commit end end 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 3.times do |try| begin bp = BrowsePage.new(url) Application.log(self, url, bp.albums.size, "albums found") results << bp break rescue Application.log(self, url, $!, "(retry ##{try})") end end queue.pop # unregister our thread end.priority end sleep 1 until queue.empty? insert_thread.kill rescue Application.log(self, $!) end |