Permalink
Browse files

importing teams

  • Loading branch information...
1 parent afa99da commit 183bd9380c1265e72bb845447896a3c84b343f3f @aserafin aserafin committed Jan 3, 2014
View
8 app/models/team.rb
@@ -1,3 +1,11 @@
class Team < ActiveRecord::Base
validates :name, presence: true
+
+ attr_accessor :logo_remote_url
+
+ has_attached_file :logo
+
+ def logo_remote_url=(url)
+ self.logo = URI.parse(url)
+ end
end
View
44 app/services/futhead_service.rb
@@ -0,0 +1,44 @@
+class FutheadService
+ def initialize
+ @domain = 'http://www.futhead.com'
+ @agent = Mechanize.new
+ end
+
+ def import_clubs(url, page = 1)
+ while true do
+ @agent.get(build_url(url, page)) do |page|
+ page.search('.club-table td.tdname a').each do |club_link|
+ import_club("#{@domain}#{club_link['href']}")
+ end
+ end
+ page += 1
+ end
+ rescue Mechanize::ResponseCodeError => ex
+ return
+ end
+
+ def import_nations(url, page = 1)
+ import_clubs(url, page)
+ end
+
+ def import_club(url)
+ @agent.get(url) do |page|
+ attributes = get_team_attributes_from_club_node(page.search('h1.club-header'))
+ Team.where(name: attributes[:name]).first_or_initialize(attributes).save
+ puts "[#{attributes[:name]}] -> OK!"
+ end; return
+ rescue Exception => ex
+ puts "ERROR! -> #{ex}"
+ end
+
+ def get_team_attributes_from_club_node(club_node)
+ {
+ name: club_node.children[2].text.strip,
+ logo_remote_url: club_node.search('img')[0]['src']
+ }
+ end
+
+ def build_url(url, page)
+ "#{url}?page=#{page}"
+ end
+end
View
10 config/environments/development.rb
@@ -26,4 +26,14 @@
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
+
+ config.paperclip_defaults = {
+ storage: :fog,
+ fog_credentials: {
+ provider: 'Local',
+ local_root: "#{Rails.root}/public"
+ },
+ fog_directory: '',
+ fog_host: 'localhost'
+ }
end
View
12 config/environments/production.rb
@@ -77,4 +77,16 @@
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
+
+ config.paperclip_defaults = {
+ storage: :fog,
+ fog_credentials: {
+ provider: 'AWS',
+ region: 'eu-west-1',
+ aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
+ aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
+ },
+ fog_directory: 'elranks',
+ fog_host: 'localhost'
+ }
end
View
5 db/migrate/20140102125542_team_add_external_id_column.rb
@@ -0,0 +1,5 @@
+class TeamAddExternalIdColumn < ActiveRecord::Migration
+ def change
+ add_column :teams, :external_id, :string
+ end
+end
View
5 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20140102120834) do
+ActiveRecord::Schema.define(version: 20140102125542) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -65,9 +65,10 @@
t.datetime "logo_updated_at"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "external_id"
end
- create_table "tournament", force: true do |t|
+ create_table "tournaments", force: true do |t|
t.integer "game_id", null: false
t.string "name", null: false
t.string "type", null: false
View
7 lib/tasks/teams.rake
@@ -0,0 +1,7 @@
+namespace :teams do
+ desc 'import teams from futhead site'
+ task :import => :environment do
+ FutheadService.new.import_nations('http://www.futhead.com/14/nations/')
+ FutheadService.new.import_clubs('http://www.futhead.com/14/clubs/')
+ end
+end

0 comments on commit 183bd93

Please sign in to comment.