transfermarkt 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ff333189fee136cb5539922398095f6e8ea4b36
4
+ data.tar.gz: 2de595aada23ff6a060812aba745653d5f325bb5
5
+ SHA512:
6
+ metadata.gz: 33c30928618a32f8f13b8d356e15e818345cd80f389b4efb3fe463f2b2884cf5106bd0657767f5543933f4854e73cb186151fe40154c658972a7b9d3259b6dbb
7
+ data.tar.gz: 93ea39582d5abeda7b56430ed60f5a622eff737b7fcc749ffdfe6f5f62fe7e0ce0066e19f98f0c76f136e9946a65500ca4742fbff24f84984212e0cf7957568f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transfermarkt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Elad Meidar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Transfermarkt
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'transfermarkt'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install transfermarkt
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,39 @@
1
+ module Transfermarkt
2
+ class Club < Transfermarkt::EntityBase
3
+ attr_accessor :name,
4
+ :country,
5
+ :club_uri,
6
+ :players,
7
+ :player_uris
8
+
9
+ def self.fetch_by_club_uri(club_uri, fetch_players = false)
10
+ puts "fetching club #{club_uri}"
11
+
12
+ req = self.get("/#{club_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
13
+ if req.code != 200
14
+ nil
15
+ else
16
+ club_html = Nokogiri::HTML(req.parsed_response)
17
+ options = {}
18
+
19
+ options[:club_uri] = club_uri
20
+ options[:name] = club_html.xpath('//*[@id="vereinsinfo"]').text
21
+ options[:country] = club_html.xpath('//*[@id="centerbig"]//form//table//tr[1]//td[2]//h1//a[2]').text
22
+ options[:player_uris] = club_html.xpath('//table[@id="spieler"]//tr//td//table//tr//td[2]//a[contains(@href,"profil")]').collect{|player_html| player_html["href"]}
23
+
24
+ puts "found #{options[:player_uris].count} players"
25
+ options[:players] = []
26
+
27
+ if fetch_players
28
+ options[:player_uris].each do |player_uri|
29
+ options[:players] << Transfermarkt::Player.fetch_by_profile_uri(player_uri)
30
+ end
31
+ end
32
+
33
+ puts "fetched club players for #{options[:name]}"
34
+
35
+ self.new(options)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ module Transfermarkt
2
+ class EntityBase
3
+ include HTTParty
4
+
5
+ base_uri Transfermarkt.base_uri
6
+
7
+ def initialize(options = {})
8
+ options.each_pair do |key, value|
9
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,74 @@
1
+ module Transfermarkt
2
+ class League < Transfermarkt::EntityBase
3
+ attr_accessor :name,
4
+ :country,
5
+ :league_uri,
6
+ :clubs,
7
+ :clubs_index
8
+ :club_uris
9
+
10
+ def self.fetch_clubs_and_uris_by_league_uri(league_uri)
11
+ req = self.get("/#{league_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
12
+ if req.code != 200
13
+ nil
14
+ else
15
+ league_html = Nokogiri::HTML(req.parsed_response)
16
+ options = {}
17
+
18
+ options[:league_uri] = league_uri
19
+ options[:name] = league_html.xpath('//*[@id="wb_seite"]/table/tr[1]/td[2]/h1/text()').text.strip.gsub(" -","")
20
+ options[:country] = league_html.xpath('//*[@id="wb_seite"]/table/tr[1]/td[2]/h1/a').text
21
+
22
+ club_uris = league_html.xpath('//table[@id="vereine"]//tr//td[2]//a[@class="s10"]').collect{|player_html| player_html["href"]}
23
+ club_names = league_html.xpath('//table[@id="vereine"]//tr//td[2]//a[@class="s10"]').collect{|player_html| player_html.text }
24
+
25
+ clubs = Hash[club_names.zip(club_uris)]
26
+
27
+ options[:clubs_index] = clubs
28
+ self.new(options)
29
+ end
30
+ end
31
+
32
+ def self.fetch_by_league_uri(league_uri, fetch_clubs = false)
33
+ puts "fetching league #{league_uri}"
34
+
35
+ req = self.get("/#{league_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
36
+ if req.code != 200
37
+ nil
38
+ else
39
+ league_html = Nokogiri::HTML(req.parsed_response)
40
+ options = {}
41
+
42
+ options[:league_uri] = league_uri
43
+ options[:name] = league_html.xpath('//*[@id="wb_seite"]/table/tr[1]/td[2]/h1/text()').text.strip.gsub(" -","")
44
+ options[:country] = league_html.xpath('//*[@id="wb_seite"]/table/tr[1]/td[2]/h1/a').text
45
+
46
+ options[:club_uris] = league_html.xpath('//table[@id="vereine"]//tr//td[2]//a[@class="s10"]').collect{|player_html| player_html["href"]}
47
+
48
+ puts "Found #{options[:club_uris].count} clubs"
49
+ options[:clubs] = []
50
+
51
+ if fetch_clubs
52
+ options[:club_uris].each do |club_uri|
53
+ options[:clubs] << Transfermarkt::Club.fetch_by_club_uri(club_uri, fetch_clubs)
54
+ end
55
+ end
56
+
57
+ puts "fetched league clubs for #{options[:name]}"
58
+
59
+ self.new(options)
60
+ end
61
+ end
62
+
63
+ def self.fetch_league_uris
64
+ root_uri = "/en/ligat-haal/startseite/wettbewerb_ISR1.html"
65
+ req = self.get("/#{root_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
66
+ if req.code != 200
67
+ nil
68
+ else
69
+ root_html = Nokogiri::HTML(req.parsed_response)
70
+ league_uris = root_html.xpath('//*[@id="categorymenu"]/li/ul/li/a').collect{|league| league["href"]}
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,118 @@
1
+ module Transfermarkt
2
+ class Player < Transfermarkt::EntityBase
3
+ attr_accessor :profile_uri,
4
+ :age,
5
+ :date_of_birth,
6
+ :full_name,
7
+ :name_in_native_country,
8
+ :foot,
9
+ :height,
10
+ :picture,
11
+ :club,
12
+ :market_value,
13
+ :nationality,
14
+ :position,
15
+ :performance_data,
16
+ :injuries_data
17
+
18
+ def initialize(options = {})
19
+ super
20
+ self.market_value = self.market_value.to_s.gsub(".", "").to_i
21
+ self.height = self.height.to_s.gsub(",", "").to_i
22
+ end
23
+
24
+ def self.fetch_by_profile_uri(profile_uri = "")
25
+ puts "fetching player profile #{profile_uri}"
26
+
27
+ req = self.get("/#{profile_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
28
+ if req.code != 200
29
+ nil
30
+ else
31
+ profile_html = Nokogiri::HTML(req.parsed_response)
32
+ options = {}
33
+
34
+ options[:profile_uri] = profile_uri
35
+ options[:club] = profile_html.xpath('//*[@id="centerbig"]//div[1]//div//table//tr[2]//td//a[1]').text
36
+ options[:full_name] = profile_html.xpath('//*[@id="centerbig"]//div[1]//div//table//tr[1]//td[2]//h1').text.gsub(/[\d]/, "").strip
37
+ options[:picture] = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[1]//img')[1]["src"]
38
+
39
+ headers = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[2]//table//tr//td[1]').collect(&:text)
40
+ headers = headers.collect {|header| header.downcase.gsub(":", "").gsub(" ", "_").gsub("'s", "").to_sym}
41
+
42
+ values = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[2]//table//tr//td[2]').collect(&:text)
43
+ values = values.collect {|value| value.strip.match(/[A-Za-z0-9,. -]*/)[0] }
44
+
45
+ # get player performance
46
+ options[:performance_data] = {}
47
+
48
+ performance_uri = profile_uri.gsub("profil", "leistungsdaten")
49
+
50
+ options = options.merge(Hash[headers.zip(values)])
51
+
52
+ # If there is a performance data blcok
53
+ if profile_html.xpath('//*[@id="centerbig"]/div[4]/p[3]/a').any?
54
+
55
+ perforamnce_types = []
56
+ 10.times do |i|
57
+ perforamnce_types << (Time.now.year - i).to_s
58
+ end
59
+
60
+ perforamnce_types.each do |type|
61
+ performance_with_type_uri = ""
62
+ if type == "All"
63
+ performance_with_type_uri = performance_uri.gsub(".html", "_gesamt.html")
64
+ else
65
+ performance_with_type_uri = performance_uri.gsub(".html", "_#{type}.html")
66
+ end
67
+
68
+ goalkeeper = options[:position] == "Goalkeeper"
69
+ options[:performance_data][type] = self.fetch_performance_data(performance_with_type_uri, goalkeeper)
70
+ end
71
+ end
72
+
73
+ options[:injuries_data] = self.fetch_injuries_data(profile_html)
74
+
75
+ puts "fetched player #{options[:full_name]}"
76
+
77
+ self.new(options)
78
+ end
79
+ end
80
+ private
81
+ def self.fetch_performance_data(performance_uri, is_goalkeeper = false)
82
+ req = self.get("/#{performance_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
83
+ if req.code != 200
84
+ nil
85
+ else
86
+ performance_data = []
87
+ performance_html = Nokogiri::HTML(req.parsed_response)
88
+ performance_headers = if is_goalkeeper
89
+ [:competition, :matches, :goals, :own_goals, :assists, :yellow_cards, :second_yellows, :red_cards, :substituted_in, :substituted_out , :goals_conceded, :saves, :minutes]
90
+ else
91
+ [:competition, :matches, :goals, :own_goals, :assists, :yellow_cards, :second_yellows, :red_cards, :substituted_in, :substituted_out, :minutes_per_goal, :minutes]
92
+ end
93
+
94
+ performance_html.xpath('//table[@class="standard_tabelle"][1]//tr[position()>1]').each do |competition|
95
+ values = Nokogiri::HTML::DocumentFragment.parse(competition.to_html).search("*//td").collect(&:text)
96
+ if values.first == ""
97
+ values.delete_at 0
98
+ end
99
+ performance_data << Hash[performance_headers.zip(values)]
100
+ end
101
+ performance_data
102
+ end
103
+
104
+ return performance_data
105
+ end
106
+
107
+ def self.fetch_injuries_data(player_html)
108
+ injury_data = []
109
+ injuries_headers = [:season, :from, :to, :injury]
110
+
111
+ player_html.xpath('//*[@id="centerbig"]/div[4]/table[3]//tr[position()>1]').each do |injury_row|
112
+ values = Nokogiri::HTML::DocumentFragment.parse(injury_row.to_html).search("*//td").collect(&:text)
113
+ injury_data << Hash[injuries_headers.zip(values)]
114
+ end
115
+ injury_data
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ module Transfermarkt
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,43 @@
1
+ require "transfermarkt/version"
2
+
3
+ module Transfermarkt
4
+ require 'httparty'
5
+ require 'nokogiri'
6
+
7
+ autoload :EntityBase, 'transfermarkt/entity_base'
8
+ autoload :Player, 'transfermarkt/player'
9
+ autoload :Club, 'transfermarkt/club'
10
+ autoload :League, 'transfermarkt/league'
11
+
12
+ USER_AGENT = "Firefox"
13
+
14
+ def Transfermarkt.base_uri
15
+ "http://www.transfermarkt.com/"
16
+ end
17
+
18
+ def self.test_fetch_league
19
+ before = Time.now
20
+ league_uri = "en/primera-division/startseite/wettbewerb_ES1.html"
21
+ league = Transfermarkt::League.fetch_by_league_uri(league_uri)
22
+ after = Time.now
23
+
24
+ return {:before => before, :after => after, :league => league}
25
+ end
26
+
27
+ def self.test_fetch_club
28
+ club_uri = "/en/maccabi-haifa/startseite/verein_1064.html"
29
+ club = Transfermarkt::Club.fetch_by_club_uri(club_uri)
30
+ end
31
+
32
+ def self.test_fetch_player
33
+ uri = "/en/lionel-messi/profil/spieler_28003.html"
34
+ player = Transfermarkt::Player.fetch_by_profile_uri(uri)
35
+ end
36
+
37
+ def self.test_fetch_player_performance_data
38
+ uri = "/en/lionel-messi/leistungsdaten/spieler_28003_2012.html"
39
+ data = Transfermarkt::Player.fetch_performance_data(uri)
40
+ end
41
+ end
42
+
43
+ require 'transfermarkt/player'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'transfermarkt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transfermarkt"
8
+ spec.version = Transfermarkt::VERSION
9
+ spec.authors = ["Elad Meidar"]
10
+ spec.email = ["elad@eizesus.com"]
11
+ spec.description = "Integrate with Transfermarkt"
12
+ spec.summary = "Allows access to transfermarkt data"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "httparty"
24
+ spec.add_dependency "nokogiri"
25
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transfermarkt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Elad Meidar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Integrate with Transfermarkt
70
+ email:
71
+ - elad@eizesus.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/transfermarkt.rb
82
+ - lib/transfermarkt/club.rb
83
+ - lib/transfermarkt/entity_base.rb
84
+ - lib/transfermarkt/league.rb
85
+ - lib/transfermarkt/player.rb
86
+ - lib/transfermarkt/version.rb
87
+ - transfermarkt.gemspec
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.6
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Allows access to transfermarkt data
112
+ test_files: []