# encoding: utf-8 require 'sindex' require 'sjunkieex' require 'eventmachine' require 'fiber' require 'yaml' require 'junkie' require 'junkie/pyload/api' require 'junkie/pyload/observer' require 'junkie/errors' module Junkie class Reactor include Log, Helper, Config DEFAULT_CONFIG = { :hd_enabled => false, :hoster_id => "ul", :series_index_file => File.join(Dir.home, '.sindex/seriesindex.xml'), :episode_queue_timer_refresh => 5, # in seconds :episode_search_refresh => 15, # in minutes } def initialize @config = Config.get_config(self) @sindex = Sindex::SeriesIndex.new(index_file: @config[:series_index_file]) @sjunkieex = Sjunkieex::Interface.new(@sindex, @config) @pyload_observer = Junkie::Pyload::Observer.new() @episode_queue = EM::Queue.new @found_episodes = Hash.new build_procs # has to be called here end def build_procs # Proc that looks for new episodes on Seriesjunkies.org and adds them to # the episode_queue if they are new @look_for_new_episodes = Proc.new { in_fiber { log.info "Looking for new episodes" @sjunkieex.get_links_for_downloads.each do |episode| identifier = "%s@%s" % [episode.id, episode.series] if not @found_episodes.has_key? identifier log.info("Found new episode '#{episode}'") @episode_queue.push(episode) @found_episodes[identifier] = episode end end } } # Proc that checks is Pyload-Observer is ready for new episodes and the # episode_queue contains new episodes. # # @note Is called from within the reactor @add_episodes_to_pyload = Proc.new do if @pyload_observer.is_ready? @episode_queue.pop do |episode| log.info("Popped episode '#{episode}' from queue") in_fiber { @pyload_observer.add_episode(episode) } end end end end ########################################################################### #################### The Reactor ########################################## ########################################################################### def start log.info("Starting Junkie #{Junkie::VERSION}") EM.run do # do some initialization work @pyload_observer.setup # Look for new episodes and add them to the Queue if they haven't # been found yet EM.next_tick do @look_for_new_episodes.call EM.add_periodic_timer(@config[:episode_search_refresh] * 60) do @look_for_new_episodes.call end end # Add found episodes into Pyload if there are any episodes and pyload # is ready EM.add_periodic_timer( @config[:episode_queue_timer_refresh], @add_episodes_to_pyload) # for determining blocking operations # EM.add_periodic_timer(1) { puts Time.now.to_i } end end end end