predictwise 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/predictwise.rb +79 -0
  2. metadata +67 -0
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ ###############################################################################
3
+ # PredictWise Site Parser v0.0.1b by Chase Higgins
4
+ ###############################################################################
5
+ # PredictWise Site Parser is a tool to mimic what a PredictWise API might look
6
+ # like. It acts as a wrapper that can request a contract on the site, and parse
7
+ # the HTML page to get the prediction information. PredictWise has said they
8
+ # are looking into creating an API in the near future, but that is too long
9
+ # to wait to start leveraging their data, now that InTrade is gone.
10
+ ###############################################################################
11
+
12
+ require 'open-uri';
13
+
14
+ class PredictWise
15
+ def self.getPrediction(contract)
16
+ # getPredictions is the most obviously important method, and can act as
17
+ # an all-in-one retrieval tool for a contact
18
+ # first we need page
19
+ p = getPage(contract);
20
+ # now parse them
21
+ events = parseContract(p);
22
+ return events;
23
+ end; # end getPredictions method
24
+
25
+ def self.getPage(contract)
26
+ # get the HTML that makes up the contract page
27
+ p = open('http://www.predictwise.com/' + contract);
28
+ html = p.read;
29
+ # predictwise sort of makes our task a little easier here, since the actual
30
+ # contract is in an iframe, which we can pull to reduce the amount of data
31
+ # we need to parse to get the predictions
32
+ p = html.match(/<iframe class='predictw_table_iframe' src='([\w\d:\/.-]+)/);
33
+ p = p[0];
34
+ parts = p.split("src='");
35
+ url = parts[1];
36
+ # now we can use that URL to get our predictions table
37
+ p = open(url);
38
+ # now we have the actual table of the contract, we can send it back and
39
+ # get ready to parse the data from it that we would like to have
40
+ return p.read;
41
+ end; # end getPage method
42
+
43
+ def self.parseContract(contract)
44
+ # parse the contract table that is provided in the argument
45
+ # now we should be able to get each row
46
+ matches = contract.scan(/<tr class='(even|odd)'>(.*)<\/tr>/);
47
+ # matches is now an array of every prediction in the table, in iterable form
48
+ events = [];
49
+ matches.each { |m|
50
+ events << getPredictionFromRow(m);
51
+ };
52
+ return events;
53
+ end; # end parseContract method
54
+
55
+ def self.getPredictionFromRow(row)
56
+ # take the matched row object and return a hash entry for the condition and
57
+ # the associated probability of its occurence
58
+ data = row[1]; # this is where the matched string is
59
+ data = data.scan(/<td[^>]+>([\w\d\s.]+)/);
60
+ # data[0] is event, data[1] is predictwise prediction, the one we want
61
+ h = {
62
+ :event => data[0][0],
63
+ :probability => data[1][0].strip
64
+ };
65
+ return h;
66
+ end; # end getPredictionFromRow method
67
+
68
+ def self.sortEvents(events, type = nil);
69
+ # take a events list and sort it in the way specified
70
+ # if type is ommitted, events are sorted by probability, same if type is 'prob'
71
+ # if type is set to 'event_name', then the sort will be alphabetic by event title
72
+ if type == 'prob' or type == nil
73
+ sorted = events.sort_by { |h| h[:probability].to_i}.reverse;
74
+ elsif type == 'event_name'
75
+ sorted = events.sort_by { |h| h[:event]};
76
+ end;
77
+ return sorted;
78
+ end; # end sortEvents method
79
+ end; # end PredictWise class
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: predictwise
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Chase Higgins
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-03-29 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A simple API wrapper for PredictWise
23
+ email: chasehx@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/predictwise.rb
32
+ has_rdoc: true
33
+ homepage: http://twitter.com/tzDev
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: PredictWise API Wrapper
66
+ test_files: []
67
+