public
Description: Your friendly neighborhood nzb downloader
Clone URL: git://github.com/maddox/pyrot.git
Search Repo:
Click here to lend your support to: pyrot and make a donation at www.pledgie.com !
maddox (author)
Fri May 09 11:45:43 -0700 2008
commit  10e0bd09fa963635be4fea31ead4dc1132c37b72
tree    04aa8c9e8a985b08c83b49de6a5ab568143bd8da
parent  5bcce78b81508d1ecaf108974af3f9d5c4fc830a
pyrot / lib / newzbin.rb
100644 147 lines (113 sloc) 4.224 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
####### Newzbin API
### http://v3.newzbin.com
 
# open a new newzbin connection, and search it with the method provided. Pass it vars to narrow the search
# Download the nzb using the provided get_nzb method
#
# newz = Newzbin::Connection.new('username', 'password')
# nzbs = newz.search(:q => 'casino royale', :ps_rb_video_format => 131072)
#
# puts nzbs.inspect
#
# newz.get_nzb(nzbs.first.id)
 
 
 
require 'rubygems'
require 'net/http'
require 'cgi'
require 'xmlsimple'
 
module Newzbin
  
  class Connection
 
    def initialize(username=nil, password=nil)
      @host = 'http://v3.newzbin.com'
      @search = '/search/'
      @dnzb = '/dnzb'
      @username = username
      @password = password
    end
 
    def http_get(url)
      Net::HTTP.start('v3.newzbin.com') do |http|
        req = Net::HTTP::Get.new(url)
        req.add_field 'Cookie', 'NzbSmoke=Px0oEtmhh%24APwIHmiPyBRJTjBcfOw7HPbCiho%3D; NzbSessionID=24c3e702b9caeb9c490e831042864b85'
        response = http.request(req)
        # puts response.body
        response.body
      end
      
    end
 
    def request_url(params)
      # http://v3.newzbin.com/search/?q=speed&searchaction=Search&fpn=p&category=6&area=-1ps_rb_video_format=131072&sort=ps_edit_date&order=desc&areadone=-1&feed=rss&
      params.delete_if {|key, value| (value == nil || value == '') }
      url = "#{@search}?searchaction=Search&fpn=p&area=-1&order=desc&areadone=-1&feed=rss&u_nfo_posts_only=0&sort=ps_edit_date&order=desc&u_url_posts_only=0&u_comment_posts_only=0&u_v3_retention=9504000"
      params.each_key do |key| url += "&#{key}=" + CGI::escape(params[key].to_s) end if params
      url
    end
 
    def search(params)
      nzbs = []
      response = XmlSimple.xml_in(http_get(request_url(params)), { 'ForceArray' => false })
      
      case response["channel"]["item"].class.name
      when "Array"
        response["channel"]["item"].each { |item| nzbs << Nzb.new(item)}
      when "Hash"
        nzbs << Nzb.new(response["channel"]["item"])
      end
      
      nzbs
 
    end
    
    def get_name(id)
      http = Net::HTTP.new(@host)
      
      http.request_post('/dnzb', "username=#{@username}&password=#{@password}&reportid=#{id}") {|response|
        p response.status
        p response['content-type']
        # response.read_body do |str| # read body now
        # print str
        # end
      }
      
      # response = http.post(@dnzb, "username=#{@username}&password=#{@password}&reportid=#{id}")
      
      # response = Net::HTTP.post_form(URI.parse("#{@host}#{@dnzb}"),{:username => @username, :password => @password, :reportid => id})
 
      case response["x-dnzb-rcode"].to_i
      when 200
        response["x-dnzb-name"]
      when 450
        puts "ERROR 450: 5 nzbs per minute please."
        false
      else
        puts "ERROR #{response["x-dnzb-rcode"]}: #{response["x-dnzb-rtext"]}"
        false
      end
 
    end
 
    def get_nzb(id)
      response = Net::HTTP.post_form(URI.parse("#{@host}#{@dnzb}"),{:username => @username, :password => @password, :reportid => id})
 
      case response["x-dnzb-rcode"].to_i
      when 200
        puts "NZB downloaded OK"
        response.body
      when 450
        puts "ERROR 450: 5 nzbs per minute please."
        false
      else
        puts "ERROR #{response["x-dnzb-rcode"]}: #{response["x-dnzb-rtext"]}"
        false
      end
    
    end
  end
  
  
 
  class Nzb
    attr_accessor :pub_date, :size_in_bytes, :category, :attributes, :title, :info_url, :id
 
    def initialize(details)
      #puts details.inspect
      @pub_date = details["pubDate"]
      @size_in_bytes = details["size"]["content"]
      @category = details["category"]
      @title = details["title"]
      @id = details["id"]
      @info_url = details["moreinfo"]
      @attributes = {}
      
 
      case details["attributes"]["attribute"].class.name
      when "Array"
        details["attributes"]["attribute"].each do |attri|
 
          case @attributes.has_key? attri["type"]
          when false
            @attributes[attri["type"]] = attri["content"]
          when true
            @attributes[attri["type"]] += ", #{attri["content"]}"
          end
 
        end
      end
 
 
    end
  end
    
end