Permalink
Browse files

moved the code to be 100% macruby

  • Loading branch information...
1 parent 6ce3329 commit 42b383a0fc65a4975a95450d234e03f88e1f1102 @mattetti committed Jun 14, 2010
Showing with 541 additions and 633 deletions.
  1. +0 −11 bin/garcon
  2. +4 −0 bin/pastis
  3. +0 −10 bin/waterboy
  4. +4 −0 lib/filter.rb
  5. +2 −2 lib/logs.rb
  6. +0 −71 lib/macaddr.rb
  7. +0 −128 lib/parser.rb
  8. +0 −69 lib/ricard.rb
  9. +0 −47 lib/ricard_client.rb
  10. +0 −35 lib/ricard_plugin.rb
  11. +0 −35 lib/ricard_plugin_base.rb
  12. +87 −0 lib/rss_parser.rb
  13. +0 −13 lib/transmission.rb
  14. +0 −91 lib/wakeonlan.rb
  15. +112 −121 pastis.rb
  16. +22 −0 readme.markdown
  17. +310 −0 vendor/macruby_http.rb
View
@@ -1,11 +0,0 @@
-#!/usr/bin/ruby
-
-require File.join(File.dirname(__FILE__), '..', 'pastis')
-module Garcon
- def self.serve
- @server ||= Ricard::Server.new
- @server.start
- end
-end
-
-Garcon.serve
View
@@ -0,0 +1,4 @@
+#!/usr/local/bin/macruby
+
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'pastis'))
+Pastis.new.check
View
@@ -1,10 +0,0 @@
-#!/usr/bin/ruby
-
-require File.join(File.dirname(__FILE__), '..', 'pastis')
-module Waterboy
- def self.prepare
- Pastis.new.pour
- end
-end
-
-Waterboy.prepare
View
@@ -29,6 +29,10 @@ def to_download?(string)
end
end
+ def self.default
+ [{:inclusive_rules => ['Weeds'], :exclusive_rules => ['720p'], :location => nil}]
+ end
+
end
end
View
@@ -1,6 +1,6 @@
class Pastis
module Logs
- LOG_FILE = ROOT + '/.rsslog'
+ LOG_FILE = '~/.pastis-rsslog'.stringByStandardizingPath
def log
@log ||= File.exist?(LOG_FILE) ? File.open(LOG_FILE, 'r+') : File.new(LOG_FILE, 'w+')
@@ -29,7 +29,7 @@ def prune_logs
end
def clear_logs
- `rm .rsslog`
+ `rm #{LOG_FILE}`
end
end
View
@@ -1,71 +0,0 @@
-##
-# Cross platform MAC address determination. Works for:
-# * /sbin/ifconfig
-# * /bin/ifconfig
-# * ifconfig
-# * ipconfig /all
-#
-# To return the first MAC address on the system:
-#
-# Mac.address
-#
-# To return an array of all MAC addresses:
-#
-# Mac.address.list
-#
-# http://codeforpeople.com/lib/ruby/macaddr/macaddr-1.0.0/
-
-module Mac
- VERSION = '1.0.0'
-
- def Mac.version() ::Mac::VERSION end
-
- class << self
-
- ##
- # Accessor for the system's first MAC address, requires a call to #address
- # first
-
- attr_accessor "mac_address"
-
- ##
- # Discovers and returns the system's MAC addresses. Returns the first
- # MAC address, and includes an accessor #list for the remaining addresses:
- #
- # Mac.addr # => first address
- # Mac.addr.list # => all addresses
-
- def address
- return @mac_address if defined? @mac_address and @mac_address
- re = %r/[^:\-](?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F][^:\-]/io
- cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all'
-
- null = test(?e, '/dev/null') ? '/dev/null' : 'NUL'
-
- lines = nil
- cmds.each do |cmd|
- stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd| fd.readlines} rescue next
- next unless stdout and stdout.size > 0
- lines = stdout and break
- end
- raise "all of #{ cmds.join ' ' } failed" unless lines
-
- candidates = lines.select{|line| line =~ re}
- raise 'no mac address candidates' unless candidates.first
- candidates.map!{|c| c[re].strip}
-
- maddr = candidates.first
- raise 'no mac address found' unless maddr
-
- maddr.strip!
- # maddr.instance_eval{ @list = candidates; def list() @list end }
-
- @mac_address = maddr
- end
-
- ##
- # Shorter alias for #address
-
- alias_method "addr", "address"
- end
-end
View
@@ -1,128 +0,0 @@
-require 'digest/md5'
-require 'digest/sha1'
-
-class Pastis
- include Logs
-
- module Parser
-
- class ParserError < StandardError; end
-
- def extract_link(item)
- if XML_PARSER == 'nokogiri'
- item.xpath('link').first.content
- elsif XML_PARSER == 'rexml'
- item.elements['link'].text
- else
- raise 'wow, how did you manage to not have a XML parser set?'
- end
- end
-
- def extract_timestamp(item)
- if XML_PARSER == 'nokogiri'
- date = item.xpath('pubDate').first.content
- elsif XML_PARSER == 'rexml'
- date = item.elements['pubDate'].text
- else
- raise ParserError, 'wow, how did you manage to not have a XML parser set?'
- end
- ::DateTime.parse(date).strftime("%F-%T").gsub(':', '_')
- end
-
- def extract_guid(item)
- if XML_PARSER == 'nokogiri'
- if !item.xpath('guid').empty?
- date = item.xpath('guid').first.content
- else
- date = Digest::MD5.hexdigest(item.xpath('link').first.content)
- end
- elsif XML_PARSER == 'rexml'
- if item.elements['guid']
- date = item.elements['guid'].text
- else
- date = Digest::MD5.hexdigest(item.elements['link'].text)
- end
- else
- raise ParserError,'wow, how did you manage to not have a XML parser set?'
- end
- end
-
- def extract_title(item)
- if XML_PARSER == 'nokogiri'
- date = item.xpath('title').first.content
- elsif XML_PARSER == 'rexml'
- date = item.elements['title'].text
- else
- raise ParserError,'wow, how did you manage to not have a XML parser set?'
- end
- end
-
- def extract_enclosure(item)
- if XML_PARSER == 'nokogiri'
- item.xpath('enclosure').first.attributes['url']
- elsif XML_PARSER == 'rexml'
- item.elements['enclosure'].attributes['url']
- else
- raise ParserError,'wow, how did you manage to not have a XML parser set?'
- end
- end
-
- def add_to_transmission_queue(torrent_path, download_destination)
- if download_destination && defined?(Transmission)
- Transmission.add_to_queue(torrent_path, download_destination)
- end
- end
-
- # TODO please please please clean up that mess [Matt]
- def download_torrent(item, path_to_save=nil)
- Dir.mkdir(TORRENTS_LOCAL_PATH) unless File.exist?(TORRENTS_LOCAL_PATH)
- Dir.mkdir(TORRENTS_LOCAL_PATH + '/to_download/') unless File.exist?(TORRENTS_LOCAL_PATH + '/to_download/')
-
- begin
- link = extract_link(item)
- guid = extract_guid(item)
- timestamp = extract_timestamp(item)
- title = extract_title(item)
- url = URI.parse(link.gsub(/\[(.+?)\]/){|grp| "~#{$1}~"})
- path = url.path
- rescue => e
- # raise ParserError, e.message
- p "#{e.message} [#{__FILE__} Line #{__LINE__}]"
- else
- if not_in_logs?(guid)
- res = Net::HTTP.start(url.host, url.port) {|http| http.get( url.path.gsub(/~(.*)~/){|grp| "[#{$1}]"} )}
- if res['content-disposition']
- filename = res['content-disposition'][/filename="(.*)";/, 1]
- torrent_path, download_destination = find_torrents_path_to_user(title, filename)
- File.open(torrent_path, 'w'){|file| file << res.body}
- add_to_transmission_queue(file, destination)
- add_to_logs(guid, filename, timestamp)
- elsif enclosure_link = extract_enclosure(item)
- link =~ /.*\/(.*\.torrent)/
- filename = $1
- if filename
- torrent_path, download_destination = find_torrents_path_to_user(title, filename)
- File.open(torrent_path, 'w'){|file| file << res.body}
- add_to_transmission_queue(torrent_path, download_destination)
- add_to_logs(guid, filename, timestamp)
- else
- puts "couldn't find out the filename of the enclosure"
- end
- else
- puts "skipping #{title} since it doesn't have a torrent file attached"
- end
- end
- end
- end
-
- # returns an array with the torrent path and the torrent destination if available
- def find_torrents_path_to_user(title, filename)
- if FILTERS && FILTERS.map{|filter| filter.to_download?(title)}.uniq.include?(true)
- [File.join(TORRENTS_LOCAL_PATH, 'to_download', filename), FILTERS.detect{|filter| filter.to_download?(title)}.location ]
- else
- [File.join(TORRENTS_LOCAL_PATH, filename), nil]
- end
- end
-
- end # of Parsee
-end
View
@@ -1,69 +0,0 @@
-require 'dnssd'
-require 'eventmachine'
-require 'base64'
-
-class RicardError < StandardError; end
-
-# Bonjour server handling requests from the LAN
-# using the Ricard client.
-module Ricard
- VERSION = '0.1.0'
- PROTOCOL_VERSION = '20090628'
-
- class Server
- attr_reader :options
-
- def initialize(options={})
- default_options = {
- :timeout => 5,
- :port => 9280
- }
- @options = default_options.merge(options)
- end
-
- def start
- register_on_bonjour
- start_server
- end
-
- protected
-
- def register_on_bonjour
- tr = DNSSD::TextRecord.new
- tr['description'] = "Ricard server for Pastis"
-
- DNSSD.register('ricard', '_ricard._tcp', '', options[:port], tr.encode) do |rr|
- puts "Ricard server announced to run on port #{options[:port]}"
- end
- end
-
- def start_server
- EventMachine::run {
- EventMachine::start_server "0.0.0.0", options[:port], RicardEventServer
- puts "Listening for requests on #{options[:port]}"
- }
- end
-
- end
-
-end
-
-module RicardEventServer
-
- def receive_data(data)
- p "received data #{data}"
- data = Marshal.load(data)
- if data.is_a?(Array)
- command, plugin = data
- # do something with the query
- $stderr.puts "Plugin: #{plugin}, Command: #{command}"
- case plugin
- when 'ricard'
- send_data(Marshal.dump(::Ricard::Plugin.execute(command)))
- else
- send_data(Marshal.dump("Plugin #{plugin} not available"))
- end
- end
- end
-
-end
View
@@ -1,47 +0,0 @@
-require 'dnssd'
-require 'base64'
-
-module Ricard
- class Client
- attr_reader :options, :server_info
-
- class DiscoveryError < StandardError; end
- ServerInfo = Struct.new(:ip, :port, :domain)
-
- def initialize(options={})
- default_options = {
- :timeout => 3
- }
- @options = default_options.merge(options)
- end
-
- # Returns an array with the server ip, port and domain
- def discover_ricard_server
- return server_info if server_info
- server_discovery = DNSSD.resolve('ricard', '_ricard._tcp', 'local') do |r|
- ip = ::Ricard::Plugin.domain_to_ip(r.target)
- raise ::Ricard::Client::DiscoveryError, "The ricard server wasn't found on the LAN" unless ip
- @server_info = ServerInfo.new(ip, r.port, r.target)
- puts "Found the Ricard service at #{@server_info.domain} #{@server_info.port} on ip: #{@server.ip}"
- end
- sleep options[:timeout]
- server_discovery.stop
- raise ::Ricard::Client::DiscoveryError, "No Ricard server found on the LAN, make sure you started a server" unless server_info
- server_info
- end
-
- def send_command(command, plugin='ricard')
- puts "Sending: '#{command}' to the #{plugin} Ricard Server plugin"
- ricard_server = discover_ricard_server
-
- data = Marshal.dump([command, plugin])
-
- ricard = TCPSocket.new(ricard_server.ip, ricard_server.port) #.send(data, 0)
- ricard.send(data, 0)
- returned_data = Marshal.load(ricard.recv(200))
- ricard.close
- returned_data
- end
-
- end
-end
Oops, something went wrong.

0 comments on commit 42b383a

Please sign in to comment.