tf2r 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cda160e8773382ca326bd42ad5f6469cbf63fd57
4
- data.tar.gz: 3a43402a23424491b50559b27fbde5f2c72fd2a0
3
+ metadata.gz: 53baabcfbcb2844a14bd53e3e37a7d5d82d80894
4
+ data.tar.gz: 0085e3ec2ef5d5f51f1b5172cff907f5752a199f
5
5
  SHA512:
6
- metadata.gz: 5a76b5c1c173ca4190a1c4e6197feacfe0a00d4304aa11d07d3de6e5e0f6c9eb9e8a5e335f4b93a594d4adbc50b150c6a9111fdd0f8f683ac9c571ae67cd632d
7
- data.tar.gz: 5d1aa559bcb08c0b1479b99d56301987289732c15431056dfab4e458814bb39767a2e149577ec4e862fc3a5e129920e519472909ef44863e17a2f30b0cf472cf
6
+ metadata.gz: 380c2b12de5cf17a5152f32aca08d0a7fc3962339abcb318a8e6669bcdb370f283c155a3dd6ad916fe888a8910415c1f8838fe76cb41b750b55410b85540987f
7
+ data.tar.gz: 0b3e807888dff2cff5501a132418d8bfefb6b19745b0647736ee64d3eaa366317b2ec476c20692908745f90d20bb0f9426b47bb5b60063a75e41d13139e6073b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tf2r (0.1.0)
4
+ tf2r (0.1.1)
5
5
  mechanize (~> 2.7)
6
6
 
7
7
  GEM
@@ -1,6 +1,7 @@
1
1
  require 'mechanize'
2
2
  require 'pry'
3
3
 
4
+ require 'tf2r/api'
4
5
  require 'tf2r/scraper'
5
6
  require 'tf2r/version'
6
7
 
@@ -0,0 +1,26 @@
1
+ module TF2R
2
+ class API
3
+ HOST = 'http://tf2r.com'
4
+ ENDPOINT = '/job.php'
5
+
6
+ def self.get_raffle_info(link_snippet)
7
+ params = {checkraffle: true, rid: link_snippet[1..-1],
8
+ lastentrys: 0, lastchat: 0}
9
+ response = request(params).body
10
+ JSON.parse(response)['message']
11
+ end
12
+
13
+ private
14
+
15
+ # Make this sane with something like httparty
16
+ def self.request(params)
17
+ uri = URI.parse(HOST)
18
+ http = Net::HTTP.new(uri.host, uri.port)
19
+
20
+ request = Net::HTTP::Post.new(ENDPOINT)
21
+ request.body = URI.encode_www_form(params)
22
+
23
+ response = http.request(request)
24
+ end
25
+ end
26
+ end
@@ -58,6 +58,19 @@ module TF2R
58
58
  raffle_links.reverse!
59
59
  end
60
60
 
61
+ # Scrapes a raffle for all available information.
62
+ #
63
+ # @param page [Mechanize::Page] the raffle page.
64
+ # @return [Array]
65
+ # * the raffle Hash from scrape_raffle_for_raffle
66
+ # * the creator Hash from scrape_raffle_for_creator
67
+ # * the participants Array from scrape_raffle_for_participants
68
+ def scrape_raffle(page)
69
+ [scrape_raffle_for_raffle(page),
70
+ scrape_raffle_for_creator(page),
71
+ scrape_raffle_for_participants(page)]
72
+ end
73
+
61
74
  # Scrapes a raffle page for information about the creator.
62
75
  #
63
76
  # @example
@@ -137,21 +150,28 @@ module TF2R
137
150
  title = infos[0].text.split('Title: ')[-1]
138
151
  description = raffle_tds[1].text
139
152
 
153
+ # This doesn't work right now, because Miz just displays "10%" in the
154
+ # page HTML and updates it with JS after a call to the API.
155
+ # win_chance = /(.+)%/.match(infos.css('#winc').text)[1].to_f / 100
156
+
157
+ # This is actually horrible. Scraper should not know about API.
158
+ win_chance = get_win_chance(link_snippet)
159
+
140
160
  start_time = raffle_tds[9].attribute('data-rstart-unix').to_s
141
161
  start_time = DateTime.strptime(start_time, '%s').to_time
142
162
  end_time = raffle_tds[11].attribute('data-rsend-unix').to_s
143
163
  end_time= DateTime.strptime(end_time, '%s').to_time
144
164
 
145
- win_chance = /(.+)%/.match(infos.css('#winc').text)[1].to_f / 100
146
-
147
165
  entries = /(\d+)\/(\d+)/.match(infos.css('#entry').text)
148
166
  current_entries = entries[1].to_i
149
167
  max_entries = entries[2].to_i
150
168
 
151
169
  text = page.parser.css('.welcome_font').css('div')[3..-1].text
152
- is_done = end_time < Time.now ||
170
+ is_done = end_time <= Time.now ||
153
171
  current_entries == max_entries ||
154
- page.parser.css('.welcome_font')[5..-1].text.downcase.include?('winner')
172
+ (page.parser.css('.welcome_font').size > 5 &&
173
+ page.parser.css('.welcome_font')[5..-1].text.downcase.include?('winner')
174
+ )
155
175
 
156
176
  {link_snippet: link_snippet, title: title, description: description,
157
177
  start_time: start_time, end_time: end_time, win_chance: win_chance,
@@ -287,5 +307,16 @@ module TF2R
287
307
  def extract_hex_colour(str)
288
308
  /#(\w+)\s*;/.match(str)[1].downcase
289
309
  end
310
+
311
+ # Gets the win_chance for a raffle as reported by job.php.
312
+ #
313
+ # @example
314
+ # get_win_chance('kabc123') => 0.05
315
+ #
316
+ # @param link_snippet [String] the link snippet of the raffle.
317
+ # @return [Float] the win chance.
318
+ def get_win_chance(link_snippet)
319
+ TF2R::API.get_raffle_info(link_snippet)['wc'] / 100.0
320
+ end
290
321
  end
291
322
  end
@@ -1,3 +1,3 @@
1
1
  module TF2R
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -59,6 +59,25 @@ describe TF2R::Scraper do
59
59
  end
60
60
  end
61
61
 
62
+ describe '#scrape_raffle' do
63
+ let(:page) {
64
+ VCR.use_cassette('scrape_raffle_for_raffle') do
65
+ scraper.fetch('http://tf2r.com/kstzcbd.html')
66
+ end
67
+ }
68
+ let(:result) { scraper.scrape_raffle(page) }
69
+
70
+ it 'returns an Array' do
71
+ expect(result).to be_an(Array)
72
+ end
73
+
74
+ it 'returns the same as its child methods' do
75
+ expect(result[0]).to eql(scraper.scrape_raffle_for_raffle(page))
76
+ expect(result[1]).to eql(scraper.scrape_raffle_for_creator(page))
77
+ expect(result[2]).to eql(scraper.scrape_raffle_for_participants(page))
78
+ end
79
+ end
80
+
62
81
  describe '#scrape_main_page' do
63
82
  let(:result) { scraper.scrape_main_page }
64
83
 
@@ -13,6 +13,9 @@ VCR.configure do |c|
13
13
  c.cassette_library_dir = 'spec/vcr/cassettes'
14
14
  c.hook_into :webmock
15
15
  c.ignore_localhost = true
16
+ c.ignore_request do |request|
17
+ URI(request.uri).path == '/job.php'
18
+ end
16
19
  end
17
20
 
18
21
  WebMock.disable_net_connect!(allow_localhost: true)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tf2r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Kim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-12 00:00:00.000000000 Z
11
+ date: 2014-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -154,6 +154,7 @@ files:
154
154
  - README.md
155
155
  - Rakefile
156
156
  - lib/tf2r.rb
157
+ - lib/tf2r/api.rb
157
158
  - lib/tf2r/scraper.rb
158
159
  - lib/tf2r/version.rb
159
160
  - spec/raffles.html