# 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 = { :hd_enabled => false, :hoster_id => "ul", :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) @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.debug "Reload Sindex from file" # TODO refactor this so that we can detect if we have to reload the # index (by MD5sum) sindex = Sindex::SeriesIndex.new(index_file: @config[:series_index_file]) sjunkieex = Sjunkieex::Interface.new(sindex, @config) 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}'") @channels[:episodes].push(episode) @found_episodes[identifier] = episode end end } } 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 end end