mma 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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mma.rb +187 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a75ffe8455cf2f4e91144e7b165713ca6f7068b
4
+ data.tar.gz: 2b73414d11870e982dab00cda858b046045ec337
5
+ SHA512:
6
+ metadata.gz: 5d30ee4efcd6b59314e7dca9919907671db4e55e73cc61d40864f79d3c921443397066e33e648c0fdf5a0d40b2456c189f0879107454ee6d58224dd453f667a1
7
+ data.tar.gz: 13f2e6933b234d83e9e63ca58b9943c9cc693dc5fab706375457f61d7d929fbdabd87d4f1cb8b7d86297b4fcd2192af294d09d65ae57398f4e5b1ccc43381cbe
@@ -0,0 +1,187 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ # Example usage:
8
+ # MMA.fighter_query('royce gracie')
9
+
10
+ class MMA
11
+ GOOGLE_URL = "http://www.google.com/search?q="
12
+ SHERDOG_URL = "http://www.sherdog.com/fighter/"
13
+
14
+ # Public methods:
15
+ # MMA.fighter_query('name of fighter')
16
+ # MMA.sherdog_link('name of fighter')
17
+
18
+ def self.sherdog_link(fighter)
19
+ sherdog_links = google_search(fighter).css('.g .r a').select do |link|
20
+ link.to_s.include? SHERDOG_URL
21
+ end
22
+ if sherdog_links != []
23
+ first_link = sherdog_links.first.attribute('href').to_s
24
+ first_link_endtrim = first_link.partition('&').first
25
+ fir_link_trimmed = first_link_endtrim[first_link_endtrim.index(SHERDOG_URL)..-1]
26
+ else
27
+ "Fighter not found"
28
+ end
29
+ end
30
+
31
+ def self.fighter_query(fighter)
32
+ sherdog_page = sherdog_page(fighter)
33
+
34
+ if sherdog_page == "Fighter not found"
35
+ return "Fighter not found"
36
+ end
37
+
38
+ fighter_hash = fighter_template
39
+
40
+ fighter_hash[:link] = sherdog_link(fighter)
41
+ fighter_hash[:img_url] = sherdog_page.css('.bio_fighter [itemprop="image"]').attr('src').to_s
42
+ fighter_hash[:name] = sherdog_page.css('h1[itemprop="name"] .fn').text
43
+ fighter_hash[:nickname] = sherdog_page.css('h1[itemprop="name"] .nickname').text.gsub(/"/, '')
44
+ fighter_hash[:age] = sherdog_page.css('.item.birthday strong').text.gsub(/[^\d]/, '')
45
+ fighter_hash[:birthday] = sherdog_page.css('span[itemprop="birthDate"]').text
46
+ fighter_hash[:locality] = sherdog_page.css('span[itemprop="addressLocality"]').text
47
+ fighter_hash[:nationality] = sherdog_page.css('strong[itemprop="nationality"]').text
48
+ fighter_hash[:flag_url] = sherdog_page.css('.birthplace img').attr('src').to_s
49
+ fighter_hash[:association] = sherdog_page.css('.item.association span[itemprop="name"]').text
50
+ fighter_hash[:height] = sherdog_page.css('.item.height strong').text
51
+ fighter_hash[:weight] = sherdog_page.css('.item.weight strong').text
52
+ fighter_hash[:weight_class] = sherdog_page.css('.item.wclass strong').text
53
+
54
+ record = sherdog_page.css('.record .count_history')
55
+
56
+ wins = record.css('.left_side .bio_graph')[0]
57
+ fighter_hash[:wins][:total] = wins.css('.counter').text
58
+ fighter_hash[:wins][:knockouts] = wins.css('.graph_tag:nth-child(3)').text.to_i.to_s
59
+ fighter_hash[:wins][:submissions] = wins.css('.graph_tag:nth-child(5)').text.to_i.to_s
60
+ fighter_hash[:wins][:decisions] = wins.css('.graph_tag:nth-child(7)').text.to_i.to_s
61
+ fighter_hash[:wins][:others] = wins.css('.graph_tag:nth-child(9)').text.to_i.to_s
62
+
63
+ losses = record.css('.left_side .bio_graph')[1]
64
+ fighter_hash[:losses][:total] = losses.css('.counter').text
65
+ fighter_hash[:losses][:knockouts] = losses.css('.graph_tag:nth-child(3)').text.to_i.to_s
66
+ fighter_hash[:losses][:submissions] = losses.css('.graph_tag:nth-child(5)').text.to_i.to_s
67
+ fighter_hash[:losses][:decisions] = losses.css('.graph_tag:nth-child(7)').text.to_i.to_s
68
+ fighter_hash[:losses][:others] = losses.css('.graph_tag:nth-child(9)').text.to_i.to_s
69
+
70
+ if record.at('.right_side .bio_graph .card .result:contains("Draws") + span')
71
+ fighter_hash[:draws] = record.at('.right_side .bio_graph .card .result:contains("Draws") + span').text
72
+ end
73
+
74
+ if record.at('.right_side .bio_graph .card .result:contains("N/C") + span')
75
+ fighter_hash[:no_contests] = record.at('.right_side .bio_graph .card .result:contains("N/C") + span').text
76
+ end
77
+
78
+ fighter_hash[:fights] = build_fights(sherdog_page)
79
+
80
+ return fighter_hash
81
+ end
82
+
83
+ # Private class methods below
84
+
85
+ def self.google_query(string)
86
+ array = string.split(" ")
87
+ array << "sherdog"
88
+ array.join("+").to_s
89
+ end
90
+
91
+ def self.google_search(fighter)
92
+ url_text = Net::HTTP.get(URI.parse(GOOGLE_URL + google_query(fighter)))
93
+ Nokogiri::HTML(url_text)
94
+ end
95
+
96
+ def self.sherdog_page(fighter)
97
+ if sherdog_link(fighter) != "Fighter not found"
98
+ url_text = Net::HTTP.get(URI.parse(sherdog_link(fighter)))
99
+ return Nokogiri::HTML(url_text)
100
+ else
101
+ return sherdog_link(fighter)
102
+ end
103
+ end
104
+
105
+ def self.build_fights(sherdog_page)
106
+ fights = []
107
+ sherdog_page.css('.module.fight_history tr:not(.table_head)').each do |row|
108
+ fight = fight_template
109
+
110
+ fight[:event] = row.css('td:nth-child(3) a').text
111
+ fight[:date] = row.css('td:nth-child(3) .sub_line').text
112
+ fight[:opponent] = row.css('td:nth-child(2) a').text
113
+ fight[:result] = row.css('td:nth-child(1) .final_result').text
114
+
115
+ method = row.css('td:nth-child(4)').text
116
+ if method.split[0].to_s.include? "Draw"
117
+ fight[:method] = "Draw"
118
+ else
119
+ fight[:method] = method.split[0]
120
+ end
121
+ if method[/\(.*?\)/]
122
+ fight[:method_details] = method[/\(.*?\)/][1..-2]
123
+ end
124
+
125
+ fight[:round] = row.css('td:nth-child(5)').text
126
+ fight[:time] = row.css('td:nth-child(6)').text
127
+ fight[:referee] = row.css('td:nth-child(4) .sub_line').text
128
+
129
+ if fight[:event] != ""
130
+ fights << fight
131
+ end
132
+ end
133
+ fights
134
+ end
135
+
136
+ def self.fighter_template
137
+ {
138
+ link: "",
139
+ img_url: "",
140
+ name: "",
141
+ nickname: "",
142
+ age: "",
143
+ birthday: "",
144
+ locality: "",
145
+ nationality: "",
146
+ flag_url: "",
147
+ association: "",
148
+ height: "",
149
+ weight: "",
150
+ weight_class: "",
151
+ wins: {
152
+ total: 0,
153
+ knockouts: 0,
154
+ submissions: 0,
155
+ decisions: 0,
156
+ others: 0
157
+ },
158
+ losses: {
159
+ total: 0,
160
+ knockouts: 0,
161
+ submissions: 0,
162
+ decisions: 0,
163
+ others: 0
164
+ },
165
+ draws: 0,
166
+ no_contests: 0,
167
+ fights: []
168
+ }
169
+ end
170
+
171
+ def self.fight_template
172
+ {
173
+ event: "",
174
+ date: "",
175
+ opponent: "",
176
+ result: "",
177
+ method: "",
178
+ method_details: "",
179
+ round: "",
180
+ time: "",
181
+ referee: ""
182
+ }
183
+ end
184
+
185
+ private_class_method :google_query, :google_search, :fighter_template, :fight_template ,:sherdog_page, :build_fights
186
+
187
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yifan Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: Leverages MMA websites (e.g., Sherdog) for MMA data (e.g., fighter details)
28
+ email: contact@yifanchen.io
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/mma.rb
34
+ homepage: http://rubygems.org/gems/mma
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.8
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: MMA search tool
58
+ test_files: []