Skip to content

Instantly share code, notes, and snippets.

@jagmitg
Created March 25, 2012 21:56
Show Gist options
  • Save jagmitg/2200222 to your computer and use it in GitHub Desktop.
Save jagmitg/2200222 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
class Importer
def self.get_film_links
doc = Nokogiri::XML(open('http://jagmit.co.uk/ruby/all-performances.xml'))
doc.xpath('//cinema').each do |cinema_data|
import_cinema(cinema_data)
end
end
private
def self.import_cinema(cinema_data)
# find cinema
cinema = Cinema.find_or_create_by_name(cinema_data[:name])
cinema.update_attributes(:external_id => cinema_data[:id],
:url => cinema_data[:address],
:postcode => cinema_data[:postcode],
:telephone => cinema_data[:phone])
# add films
cinema_data.children.first.children.each do |film_data|
import_film(cinema, film_data)
end
end
def self.import_location(cinema_data)
location = Location.find_or_create_by_title(cinema_data[:name])
cinema_url = cinema_data[:url] + cinema_data[:postcode]
update_attribute(:adressing, cinema_url)
end
def self.import_film(cinema, film_data)
puts "#{film_data.inspect}"
film = Film.find_or_create_by_title(film_data[:title])
film.update_attributes(:rating => film_data[:rating],
:release => film_data[:release],
:length => film_data[:length],
:poster => film_data[:poster],
:director => film_data[:director],
:synopsis => film_data[:synopsis],
:cast => film_data[:cast])
# add shows
film_data.children.first.children.each do |show_data|
import_screening(cinema, film, show_data)
end
end
def self.import_screening(cinema, film, show_data)
screening = Screening.find_by_cinema_id_and_film_id(cinema.id, film.id)
screening ||= Screening.create(:cinema => cinema, :film => film)
screening.shows.create(:day => show_data[:date],
:showed_at => show_data[:time],
:video_type => show_data[:videoType])
end
end
def self.import_location(cinema_data)
location = Location.find_or_create_by_title(cinema_data[:name])
cinema_url = cinema_data[:url] + cinema_data[:postcode]
update_attribute(:adressing, cinema_url)
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120325212946) do
create_table "cinemas", :force => true do |t|
t.string "name"
t.string "url"
t.string "postcode"
t.string "telephone"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "external_id"
end
create_table "films", :force => true do |t|
t.string "title"
t.string "rating"
t.string "release"
t.string "length"
t.string "poster"
t.string "director"
t.string "synopsis"
t.string "cast"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "locations", :force => true do |t|
t.string "addressing"
t.float "latitude"
t.float "longitude"
t.integer "cinema_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "screenings", :force => true do |t|
t.integer "cinema_id"
t.integer "film_id"
end
create_table "shows", :force => true do |t|
t.string "day"
t.string "showed_at"
t.string "video_type"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "screening_id"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment