Created
June 10, 2010 19:49
-
-
Save andynu/433539 to your computer and use it in GitHub Desktop.
ftp listing to rss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# | |
# little ftps with tls to rss utility | |
# makes a connection, lists the contents of the | |
# directory and produces an rss feed of the results. | |
# | |
# config in our home directory ~/.ftprss | |
# example: | |
# :host: ftp.example.com | |
# :user: username | |
# :pass: password | |
# :port: 81 | |
# :paths: | |
# - /uploads | |
# | |
require 'logger' | |
require 'ftpfxp' | |
require 'digest/md5' | |
require 'yamlrc' | |
module Net #{{{ | |
class FTP | |
def connect(host, port = FTP_PORT) | |
@host = host | |
if @debug_mode | |
print "connect: ", host, ", ", port, "\n" | |
end | |
synchronize do | |
@sock = open_socket(host, port) | |
voidresp | |
end | |
end | |
def makepasv | |
if @sock.peeraddr[0] == "AF_INET" | |
host, port = parse227(sendcmd("PASV")) | |
else | |
host, port = parse229(sendcmd("EPSV")) | |
# host, port = parse228(sendcmd("LPSV")) | |
end | |
# unroutable address | |
if(host =~ /^192\.168/) | |
host = @host | |
end | |
return host, port | |
end | |
private :makepasv | |
end | |
end # }}} | |
class FTPTLSToRSS | |
@@releasers = %w[ ] | |
@@log = Logger.new(STDOUT) | |
def initialize(config={}) | |
@rc = Yamlrc.load(".ftprss") | |
@host = @rc[:host] | |
@user = @rc[:user] | |
@pass = @rc[:pass] | |
@port = @rc[:port] | |
@paths = @rc[:paths] | |
end | |
def get_items | |
ftp = Net::FTPFXPTLS.new | |
ftp.passive = true | |
ftp.debug_mode = false | |
ftp.connect(@host,@port) | |
ftp.login(@user,@pass) | |
items = [] | |
@paths.each do |dir| | |
ftp.ls(dir).each do |line| | |
mask,sub,user,group,size,mo,day,time,*folder = line.split(/\s+/) | |
pubDate = DateTime.parse("%s %s %s" % [mo,day,time]) | |
guid = Digest::MD5.hexdigest([sub,size,mo,day,time,folder].join) | |
folder = folder.join(" ") | |
next if [".",".."].include?(folder) | |
items << [mask,sub,user,group,size,mo,day,time,dir,folder,guid,pubDate] | |
end | |
end | |
return items.sort{|a,b| b[10] <=> a[10] } | |
end | |
def rss(items) | |
puts <<-XML | |
<?xml version="1.0" encoding="ISO-8859-1" ?> | |
<rss version="2.0"> | |
<channel> | |
<title>ftp rss</title> | |
<link>n/a</link> | |
<description>ftp directory listing</description> | |
XML | |
titles = {} | |
items.each do |item| | |
mask,sub,user,group,size,mo,day,time,dir,folder,guid,pubDate = item | |
search_term = folder.gsub(/divx|xvid|avi|\[\w+\]|dvdrip/i,'').gsub(/[-.]/,'+') | |
search_term = search_term.gsub(/\d{4}.*$/,'') | |
@@releasers.each do |releaser| | |
search_term = search_term.gsub(releaser,'') | |
end | |
search_term = search_term.squeeze('+') | |
puts <<-XML | |
<item> | |
<title>#{folder} (#{dir})</title> | |
<link>http://www.imdb.com/find?tt=on;nm=on;mx=20;q=#{search_term}</link> | |
<pubDate>#{pubDate.strftime("%a, %d %b %Y %H:%M:%S %Z")}</pubDate> | |
<guid>#{guid}</guid> | |
</item> | |
XML | |
end | |
puts <<-XML | |
</channel> | |
</rss> | |
XML | |
end | |
def run | |
rss(get_items) | |
end | |
end | |
if $0 == __FILE__ then | |
FTPTLSToRSS.new().run() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment