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