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 09:44:15 -0700 2008
commit  34d9623b7574e0ee22621bbbc667c3ba9dbda313
tree    4793312afac9dced5cc851d54889511f6c37ec21
parent  b495933898851675ef263b1792526cfd1b6db71c
pyrot / lib / newzbin.rb
100644 125 lines (97 sloc) 3.586 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
require 'rubygems'
require 'net/http'
require 'cgi'
require 'xmlsimple'
 
module Newzbin
  
  class Connection
    attr_accessor :host, :search_path, :dnzb_path, :username, :password, :nzbSmoke, :nzbSessionID
 
    def initialize(options={})
      self.host = 'http://v3.newzbin.com'
      self.search_path = '/search/'
      self.dnzb_path = '/dnzb'
      self.username = options[:username]
      self.password = options[:password]
      self.nzbSmoke = options[:nzbSmoke]
      self.nzbSessionID = options[:nzbSessionID]
    end
 
    def http_get(url)
      Net::HTTP.start('v3.newzbin.com') do |http|
        req = Net::HTTP::Get.new(url)
        req.add_field 'Cookie', "NzbSmoke=#{self.nzbSmoke}; NzbSessionID=#{self.nzbSessionID}" if self.nzbSmoke && self.nzbSessionID
        response = http.request(req)
        response.body
      end
      
    end
 
    def request_url(params)
      params.delete_if {|key, value| (value == nil || value == '') }
      url = "#{self.search_path}?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&commit=search"
      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(self.host)
      
      http.request_post(self.dnzb_path, "username=#{self.username}&password=#{self.password}&reportid=#{id}") {|response|
        p response.status
        p response['content-type']
      }
 
      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("#{self.host}#{self.dnzb_path}"),{:username => self.username, :password => self.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)
      self.pub_date = details["pubDate"]
      self.size_in_bytes = details["size"]["content"]
      self.category = details["category"]
      self.title = details["title"]
      self.id = details["id"]
      self.info_url = details["moreinfo"]
      self.attributes = {}
      
 
      case details["attributes"]["attribute"].class.name
      when "Array"
        details["attributes"]["attribute"].each do |attri|
 
          case self.attributes.has_key? attri["type"]
          when false
            self.attributes[attri["type"]] = attri["content"]
          when true
            self.attributes[attri["type"]] += ", #{attri["content"]}"
          end
 
        end
      end
 
 
    end
  end
    
end