Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Minor tweaks after more testing \n * Ratings are now floats for bette…

…r averaging'\n * get_quality method returns a sorted array of best rated videos.\n * Added a concept of a 'type' - one being full comment grab, the other just the last page
  • Loading branch information...
commit 9f4e4f279e5ba01fe13770b04d9dea6020057dd7 1 parent c3468fc
jhart-rpx jhart-rpx authored
3  Gemfile
View
@@ -5,4 +5,5 @@ group :test do
end
gem 'nokogiri'
-gem 'hpricot'
+gem 'hpricot'
+gem 'awesome_print'
2  Gemfile.lock
View
@@ -1,6 +1,7 @@
GEM
remote: http://rubygems.org/
specs:
+ awesome_print (0.4.0)
diff-lcs (1.1.3)
hpricot (0.8.4)
nokogiri (1.5.0)
@@ -17,6 +18,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ awesome_print
hpricot
nokogiri
rspec (~> 2.7)
23 lib/pirate_bay/base.rb
View
@@ -39,6 +39,29 @@ def execute
def cached_filename
File.join("tmp", "searches", "#{search_string}_#{category_id}_#{page}.html")
end
+
+ def get_quality
+ execute
+ results = @results.map do |result|
+ url = "http://www.thepiratebay.org/torrent/#{result.id}/"
+ html = open(url).read
+ p = PirateBay::Details.new html, :init
+ puts "Fetching results"
+ result = {
+ :seeds => result.seeds,
+ :size => result.size,
+ :name => result.name,
+ :video => p.video_quality_average,
+ :audio => p.audio_quality_average,
+ :url => url
+ }
+ puts "Results: #{result}"
+ result
+ end
+
+ results.reject { |a| a[:video].nan? }.sort_by { |a| a[:video] }
+ end
+
def fetch_search_results
url = "http://thepiratebay.org/search/#{search_string}/#{page}/7/#{category_id}" # highest seeded first
44 lib/pirate_bay/details.rb
View
@@ -3,14 +3,16 @@
module PirateBay
class Details
def to_s
- "#<:yeah>"
+ "#<PirateBay::Details: @id=#{@id}>"
end
-
- def initialize(html)
+ def initialize(html, type = :full)
@details_page_html = html
+ id_matches = html.match Regexp.new '<input type="hidden" name="id" value="(.*)"/>' rescue nil
+ @id = id_matches[1].to_i unless id_matches.nil?
@comment_pages_html = []
@scores = []
+ @type = type
end
def fetch_comments(params)
@@ -26,12 +28,13 @@ def fetch_comments(params)
def fetch_all_comments
comment_xhr_params.each do |params|
+ puts " fetching comments for #{params[:page]} of #{params[:pages]}"
fetch_comments params
end
end
- def scores(type = :init)
- if type == :full
+ def scores
+ if @type == :full
fetch_all_comments if @comment_pages_html.empty?
full_html = @comment_pages_html.inject("") { |html, memo| memo += html }
document = Nokogiri::HTML(full_html)
@@ -46,13 +49,18 @@ def scores(type = :init)
end
def comment_xhr_params
- document = Nokogiri::HTML(@details_page_html)
+ document = Nokogiri::HTML(@details_page_html)
comment_link = document.css('div.browse-coms a').first
- params = PirateBay::Details.extract_xhr_params comment_link.attr('onclick')
- results = Array.new(params[:pages]) { |i| i+1 }
- results.map do |i|
- { page: i, pages: params[:pages], crc: params[:crc], id: params[:id] }
+ if comment_link.nil?
+ [{ page: 1, pages: 1, crc: "9b235c98e242f2617ae61dc416ec0de7", id: @id }]
+ else
+ params = PirateBay::Details.extract_xhr_params comment_link.attr('onclick')
+ results = Array.new(params[:pages]) { |i| i+1 }
+ results.map do |i|
+ { page: i, pages: params[:pages], crc: params[:crc], id: params[:id] }
+ end
end
+
end
def self.extract_xhr_params(string)
@@ -64,23 +72,25 @@ def self.extract_xhr_params(string)
def self.search_for_ratings string
- video_score_matches = string.match(/(v|video) ?[:\=-]? ?([0-9]{1,2})/i)
- audio_score_matches = string.match(/(a|audio) ?[:\=-]? ?([0-9]{1,2})/i)
+ video_score_matches = string.match(/(v|video) ?[:\=-]? ?([0-9]\.[0-9]|[0-9]{1,2})/i)
+ audio_score_matches = string.match(/(a|audio) ?[:\=-]? ?([0-9]\.[0-9]|[0-9]{1,2})/i)
ratings = {}
- ratings[:v] = video_score_matches[2].to_i unless video_score_matches.nil?
- ratings[:a] = audio_score_matches[2].to_i unless audio_score_matches.nil?
+ ratings[:v] = video_score_matches[2].to_f unless video_score_matches.nil?
+ ratings[:a] = audio_score_matches[2].to_f unless audio_score_matches.nil?
+ ratings.delete(:v) if ratings[:v] && ratings[:v] > 10
+ ratings.delete(:a) if ratings[:a] && ratings[:a] > 10
# puts "Incoming string of #{string} detected ratings of #{ratings}"
ratings
end
-
+
def video_quality_average
- video_scores = scores(:full).map { |score| score[:v] }.compact
+ video_scores = scores.map { |score| score[:v] }.compact
sum_of_video_scores = video_scores.inject(0) { |score, memo| memo += score }
sum_of_video_scores / video_scores.size.to_f
end
def audio_quality_average
- audio_scores = scores(:full).map { |score| score[:a] }.compact
+ audio_scores = scores.map { |score| score[:a] }.compact
sum_of_audio_scores = audio_scores.inject(0) { |score, memo| memo += score }
sum_of_audio_scores / audio_scores.size.to_f
end
28 spec/pirate_bay/details_spec.rb
View
@@ -5,7 +5,16 @@
it "should parse the page for scores and return an array of them" do
html = File.open(File.join('spec', 'fixtures' , 'torrent_details_with_ratings.html'), 'r').read
tp = PirateBay::Details.new html
- tp.scores.should =~ [{:a => 10, :v => 10}, {:a => 8, :v => 9}, {:a => 10, :v => 10}, {:a => 10, :v => 10}, { :v => 8 }, {:a => 10, :v => 10}, {:a => 10, :v => 10}, {:a => 10, :v => 10} ]
+ tp.scores.should =~ [
+ {:a => 10.0, :v => 10.0},
+ {:a => 8.0, :v => 9.0},
+ {:a => 10.0, :v => 10.0},
+ {:a => 10.0, :v => 10.0},
+ { :v => 8.0 },
+ {:a => 10.0, :v => 10.0},
+ {:a => 10.0, :v => 10.0},
+ {:a => 10.0, :v => 10.0}
+ ]
end
it "should parse the page for scores and return an array of them" do
@@ -18,14 +27,27 @@
describe ".search_for_ratings" do
it "should detect a rating if it exists and return a hash" do
ratings = PirateBay::Details.search_for_ratings("Thanks man. <br>\nA: 10<br>\nV: 10<br>\nMovie: 100")
- ratings.should == { a: 10, v: 10 }
+ ratings.should == { a: 10.0, v: 10.0 }
end
it "should detect a string that states 'audio' and/or 'video' instead of A/V" do
comment = "not in good quality\nAudio: 5\nVideo: 6"
ratings = PirateBay::Details.search_for_ratings comment
- ratings.should == { a: 5, v: 6 }
+ ratings.should == { a: 5.0, v: 6.0 }
end
+
+ it "should not accept an entry that isn't between 0 and 10" do
+ comment = "such good quality\nAudio: 56\nVideo: 6"
+ ratings = PirateBay::Details.search_for_ratings comment
+ ratings.should == { v: 6.0 }
+ end
+
+ it "should be able to detect tenth's of a point" do
+ comment = "such good quality\nAudio: 5.6\nVideo: 6"
+ ratings = PirateBay::Details.search_for_ratings comment
+ ratings.should == { v: 6.0, a: 5.6 }
+ end
+
end
describe "video_quality_average" do
Please sign in to comment.
Something went wrong with that request. Please try again.