require 'em-http' require 'fiber' require 'junkie/log' require 'sjunkieex' module Junkie # Monkey-patched version of the Interface from the `sjunkieex` gem class ::Sjunkieex::Interface include Junkie::Log, Junkie::Config DEFAULT_CONFIG = { :hd_enabled => false, # the first hoster has the highest priority :hoster_ids => ['NOT_AN_HOSTER'], } # Method that searches for new episodes and returns it in a nice structure # # @note should be called inside a Ruby fiber # @return [Hash] series names as keys, data as values def get_links_for_downloads @junkie_config ||= Config.get_config(self) episodes = [] look_for_new_episodes.each do |link,series| links = parse_series_page(series, link) links.each do |identifier, link_data| hd = @junkie_config[:hd_enabled] # select links, depending on wanted resolution links = [] if hd if link_data[:hd_1080p] links = link_data[:hd_1080p] elsif link_data[:hd_720p] links = link_data[:hd_720p] end else (links = link_data[:sd]) if link_data[:sd] end if links.empty? log.info("#{series}(#{identifier}) no links in this resolution") next end # select a link which corresponds to the hoster with the highest # possible priority selected_link = nil catch(:break) { @junkie_config[:hoster_ids].each do |hoster| links.each do |link| if link.match(/\/f-\w+\/#{ hoster }_/) selected_link = link throw :break end end end } if selected_link.nil? log.warn("#{series}(#{identifier}) no links for any specified hoster") next end episode = Junkie::Episode.new( series, selected_link, link_data[:episodedata]) episodes << episode end end episodes rescue IOError => e # an ioerror is mostly raised on a temporary network problem log.error("An IOError was raised, I will return empty array of episodes") log.error(e) return [] end private def get_page_data(link) f = Fiber.current http = EventMachine::HttpRequest.new(link).get :timeout => 10 http.callback { f.resume(http) } http.errback { f.resume(http) } Fiber.yield if http.error raise IOError, http.error end http.response end end end