Commit eeeefc0a by Xuan Trung Le

fix bugs

parent a3c73032
......@@ -8,10 +8,9 @@ class Crawler
LIST_URL = "#{BASE_CAREERBUILDER_URL}/viec-lam"
def self.crawl_job_infomation(job_links)
links = job_links
job_details = []
links.each do |link|
job_links.each do |link|
puts "Fetching #{link}..."
params = {}
link = URI.escape(link)
......@@ -26,7 +25,8 @@ class Crawler
job_details << params
end
end
return job_details
job_details
end
def self.use_template_default(doc, link)
......@@ -72,7 +72,7 @@ class Crawler
# original_link
params[:original_link] = link
return params
params
end
def self.crawl_company_infomation(doc)
......@@ -85,12 +85,12 @@ class Crawler
end
params[:name] ||= 'Bảo mật'
return params
params
end
def self.get_job_link
def self.get_job_links
url = "#{LIST_URL}/tat-ca-viec-lam-trang-#{1}-vi.html"
doc = Nokogiri::HTML(open(url))
return doc.css('.gird_standard .brief .jobtitle .job a').map { |a| a['href'] }.compact.uniq
doc.css('.gird_standard .brief .jobtitle .job a').map { |a| a['href'] }.compact.uniq
end
end
......@@ -2,13 +2,4 @@ class City < ApplicationRecord
belongs_to :country, optional: true
has_and_belongs_to_many :companies
has_and_belongs_to_many :jobs
def self.list
['AN GIANG', 'BÀ RỊA - VŨNG TÀU', 'BẮC GIANG', 'BẮC KẠN', 'BẠC LIÊU', 'BẮC NINH', 'BẾN TRE',
'BÌNH ĐỊNH', 'BÌNH DƯƠNG', 'BÌNH PHƯỚC','BÌNH THUẬN','CÀ MAU','CAO BẰNG','DAK LAK', 'DAK NÔNG','ĐIỆN BIÊN', 'ĐỒNG NAI',
'ĐỒNG THÁP','GIA LAI','HÀ GIANG','HÀ NAM','HÀ TĨNH','HẢI DƯƠNG','HẬU GIANG','HÒA BÌNH','HƯNG YÊN','KHÁNH HÒA','KIÊN GIANG',
'KON TUM','LAI CHÂU','LÂM ĐỒNG','LẠNG SƠN','LÀO CAI','LONG AN','NAM ĐỊNH','NGHỆ AN','NINH BÌNH','NINH THUẬN','PHÚ THỌ','QUẢNG BÌNH',
'QUẢNG NAM','QUẢNG NGÃI','QUẢNG NINH','QUẢNG TRỊ','SÓC TRĂNG','SƠN LA','TÂY NINH','THÁI BÌNH','THÁI NGUYÊN','THANH HÓA','THỪA THIÊN- HUẾ',
'TIỀN GIANG','TRÀ VINH','TUYÊN QUANG','VĨNH LONG','VĨNH PHÚC','YÊN BÁI','PHÚ YÊN','CẦN THƠ','ĐÀ NẴNG','HẢI PHÒNG','HÀ NỘI','HỒ CHÍ MINH',
'KV BẮC TRUNG BỘ', 'KV ĐÔNG NAM BỘ', 'KV NAM TRUNG BỘ', 'KV TÂY NGUYÊN' ]
end
end
......@@ -8,13 +8,11 @@ class Job < ApplicationRecord
has_and_belongs_to_many :cities
def self.create_new_jobs(arr_jobs)
viet_nam = Country.find_or_create_by(name: 'Viet Nam')
another = Country.find_or_create_by(name: 'another')
arr_jobs.each do |item|
job_cities = []
city_name = []
city_names = []
job_industries = []
industry_name = []
industry_names = []
job = Job.new(name: item[:name],
salary: item[:salary],
description: item[:description],
......@@ -25,17 +23,12 @@ class Job < ApplicationRecord
updated_date: item[:updated_date])
# City
unless item[:city].blank?
city_name = item[:city].split(',').map(&:strip)
city_names = item[:city].split(',').map(&:strip)
job_cities = City.where(name: city_name)
job_cities = City.where(name: city_names)
job_cities.each do |city|
job.cities << city
end
city_name = city_name - job_cities.pluck(:name)
city_name.each do |name|
job.cities << City.create(name: name, country: City.list.include?(name.upcase) ? viet_nam : another)
end
end
# Company
......@@ -46,15 +39,15 @@ class Job < ApplicationRecord
# Industry
unless item[:industry].blank?
industry_name = item[:industry].split(',').map(&:strip)
job_industries = Industry.where(name: industry_name)
industry_names = item[:industry].split(',').map(&:strip)
job_industries = Industry.where(name: industry_names)
job_industries.each do |industry|
job.industries << industry
end
industry_name = industry_name - job_industries.pluck(:name)
industry_name.each do |name|
industry_names = industry_names - job_industries.pluck(:name)
industry_names.each do |name|
job.industries << Industry.create(name: name)
end
end
......
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
viet_nam = Country.create(name: 'Viet Nam')
another = Country.create(name: 'another')
cities_of_vn = ["Hà Nội", "Hồ Chí Minh", "An Giang", "Bà Rịa - Vũng Tàu", "Bạc Liêu", "Bắc Giang",
"Bắc Ninh", "Bến Tre", "Bình Dương", "Bình Định", "Bình Phước", "Bình Thuận", "Cà Mau",
"Cao Bằng", "Cần Thơ", "Dak Lak", "Dak Nông", "Đà Nẵng", "Điện Biên",
"Đồng Bằng Sông Cửu Long", "Đồng Nai", "Đồng Tháp", "Gia Lai", "Hà Giang", "Hà Nam",
"Hà Tây", "Hà Tĩnh", "Hải Dương", "Hải Phòng", "Hậu Giang", "Hòa Bình", "Hưng Yên",
"Khác", "Khánh Hòa", "Kiên Giang", "Kon Tum", "KV Bắc Trung Bộ", "KV Đông Nam Bộ",
"KV Nam Trung Bộ", "KV Tây Nguyên", "Lai Châu", "Lạng Sơn", "Lào Cai", "Long An",
"Nam Định", "Nghệ An", "Ninh Thuận", "Phú Thọ", "Phú Yên", "Quảng Bình", "Quảng Nam",
"Quảng Ngãi", "Quảng Ninh", "Quảng Trị", "Sóc Trăng", "Sơn La", "Tây Ninh", "Thái Bình",
"Thái Nguyên", "Thanh Hóa", "Thừa Thiên- Huế", "Tiền Giang", "Toàn quốc", "Trà Vinh",
"Tuyên Quang", "Vĩnh Long", "Vĩnh Phúc", "Yên Bái"]
cities_of_another_country = [
"Banteay Meanchey", "Battambang",
"Kampong Chhnang", "Kampong Speu", "Kampot", "Kandal", "Kâmpóng Thum, Cambodia",
"Kep", "Koh Kong", "Kratie", "Mondulkiri", "Otdar Meanchey", "Pailin", "Phnompenh",
"Preah Sihanouk", "Preah Vihear", "Prey Veng", "Pursat", "Rotanak Kiri", "Siem Reap",
"Sihanoukville", "Stung Treng", "Svay Rieng", "Tbong Khmum", "Kinshasa", "Hồng Kông",
"Attapeu", "Bokeo", "Bolikhamsai", "Champasak", "Houaphanh", "Khammouane",
"Louang Namtha", "Luang Prabang", "Oudomxay", "Phongsaly", "Sainyabuli", "Salavan",
"Savannakhet", "Sekong", "Vientiane", "Xaisomboun", "Xiangkhouang", "Qatar"]
cities_of_vn.each do |city_name|
City.create(name: city_name, country: viet_nam)
end
cities_of_another_country.each do |city_name|
City.create(name: city_name, country: another)
end
......@@ -2,7 +2,7 @@ require "./app/data/crawler.rb"
namespace :data do
task insert_job: :environment do |t|
links = Crawler.get_job_link
links = Crawler.get_job_links
links = Job.filter_link_exist(links)
@data = Crawler.crawl_job_infomation(links)
Job.create_new_jobs(@data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment