tpb_api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/tpb.rb +101 -0
  2. metadata +45 -0
data/lib/tpb.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'sanitize'
4
+
5
+ class ThePirateBay
6
+ DETAILS_STRING = {
7
+ "Type:" => :type,
8
+ "Files:" => :files,
9
+ "Size:" => :size,
10
+ "Info:" => :info,
11
+ "Spoken language(s):" => :spoken,
12
+ "Uploaded:" => :uploaded,
13
+ "By:" => :by,
14
+ "Seeders:" => :seeders,
15
+ "Leechers:" => :leechers,
16
+ "Comments" => :comment
17
+ }
18
+ class << self
19
+ attr_accessor :base_url
20
+ def configure
21
+ yield self
22
+ end
23
+ def search(keyword, categories = [])
24
+ Search.new keyword, categories
25
+ end
26
+
27
+ def top(category)
28
+ Top.new category
29
+ end
30
+ end
31
+
32
+ module TableResultsParser
33
+ private
34
+
35
+ def parse_results(html_node)
36
+ res = html_node.css("div.detName")
37
+ res.map do |torrent|
38
+ torrent_node = torrent.css("a").first
39
+ Torrent.new(torrent_node[:href], name: torrent_node.children)
40
+ end
41
+ end
42
+ end
43
+
44
+ class Search
45
+ include TableResultsParser
46
+ def initialize(query, categories = [])
47
+ @result_html = Nokogiri::HTML(open("#{ThePirateBay.base_url}/search/#{query}/0/99/#{categories.join(',')}"))
48
+ end
49
+
50
+ def result
51
+ parse_results(@result_html)
52
+ end
53
+ end
54
+
55
+ class Top
56
+ include TableResultsParser
57
+ def initialize(category)
58
+ @result_html = Nokogiri::HTML(open("#{ThePirateBay.base_url}/top/#{category}"))
59
+ end
60
+
61
+ def result
62
+ parse_results(@result_html)
63
+ end
64
+ end
65
+
66
+ class Torrent
67
+ attr_reader :href, :name, :details
68
+ def initialize(href, data)
69
+ @href = href
70
+ @name = data[:name]
71
+ @details = {}
72
+ @is_loaded = false
73
+ end
74
+
75
+ def method_missing(method)
76
+ if !@details.has_key?(method) and !@is_loaded
77
+ puts "fetching"
78
+ end
79
+ @details[method]
80
+ end
81
+
82
+ def fetch_details
83
+ @result_html = Nokogiri::HTML(open(URI.parse(URI.encode("https://thepiratebay.sx#{href}", "[]"))))
84
+ @result_html.css("div#details > dl").each do |dl|
85
+ keys = dl.children.css("dt")
86
+ values = dl.children.css("dd")
87
+ keys.each_with_index do |key, index|
88
+ key = Sanitize.clean(key.to_s).strip
89
+ value = Sanitize.clean(values[index].to_s).strip
90
+ @details[DETAILS_STRING[key]] = value if DETAILS_STRING.has_key?(key)
91
+ end
92
+ end
93
+ is_loaded = true
94
+ self
95
+ end
96
+ end
97
+ end
98
+
99
+ ThePirateBay.configure do |config|
100
+ config.base_url = "http://thepiratebay.sx"
101
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpb_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michał Koźmiński
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: The ThePirateBay api for ruby apps
15
+ email: michal.kozminski@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/tpb.rb
21
+ homepage: https://github.com/michalkozminski/TPB
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.25
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: ThePirateBay api
45
+ test_files: []