# 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' require 'junkie/webinterface/interface' module Junkie class Reactor include Log, Helper, Config DEFAULT_CONFIG = { :series_index_file => File.join(Dir.home, '.sindex/seriesindex.xml'), :episode_search_refresh => 15, # in minutes } def initialize @config = Config.get_config(self) log.info("Starting Junkie #{Junkie::VERSION}") @channels = { episodes: EM::Channel.new, notifications: EM::Channel.new, info: EM::Channel.new, } @pyload_observer = Junkie::Pyload::Observer.new(@channels) @twitter_notification = Junkie::Notification::Twitter.new(@channels) @dumper = Junkie::Notification::Dumper.new(@channels) @found_episodes = Hash.new build_procs # has to be called here end ########################################################################### #################### The Reactor ########################################## ########################################################################### def start EM.run do @channels[:episodes].subscribe do |episode| next unless episode.status == :extracted @channels[:notifications].push(episode) end # 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 # start the web interface Junkie::Webinterface::Interface.setup(@channels) end end private 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 { operation = proc { log.debug "Reload Sindex from file" sindex = Sindex::SeriesIndex.new(index_file: @config[:series_index_file]) sjunkieex = Sjunkieex::Interface.new(sindex) log.info "Looking for new episodes" sjunkieex.get_links_for_downloads } callback = proc { |episodes| episodes.each do |episode| identifier = "%s@%s" % [episode.id, episode.series] if not @found_episodes.has_key? identifier log.info("Found new episode '#{episode}'") @channels[:episodes].push(episode) @found_episodes[identifier] = episode end end # send out the current time out of the websocket @channels[:info].push({ key: "Last Episode Search", desc: "The last time junkie has searched for new episodes", value: Time.new.strftime("%H:%M:%S") }) } EM.defer(operation, callback) } end end end