Commit 872bce8d by nnnghia98

pass 1 param into save_job

parent 37105061
...@@ -25,6 +25,7 @@ class CrawlData ...@@ -25,6 +25,7 @@ class CrawlData
# Job code # Job code
job_code = job_url.split("/").last.split(".")[-2] || "" job_code = job_url.split("/").last.split(".")[-2] || ""
job[:code] = job_code
# Company code # Company code
company_code = job_page.css(".viewmorejob a @href").present? ? company_code = job_page.css(".viewmorejob a @href").present? ?
...@@ -33,18 +34,17 @@ class CrawlData ...@@ -33,18 +34,17 @@ class CrawlData
crawl_job_title_logger.info "#{job[:title]}" crawl_job_title_logger.info "#{job[:title]}"
job[:workplace].each do |city_name| job[:workplace].each do |city_name|
city_id = get_city(city_name).id city_id = City.find_or_create_by(name: city_name.strip, region: "Việt Nam").id
company_id = get_company(company_code, job[:company_name], job[:company_address], job[:company_description]).id job[:company_id] = get_company(company_code, job[:company_name], job[:company_address],
job_id = get_job(job_code, job[:title], job[:salary], job[:company_description]).id
job[:description], job[:requirement], saved_job = save_job(job)
job[:level], job[:post_date],
job[:expiration_date], company_id).id CityJob.find_or_create_by!(job_id: saved_job.id, city_id: city_id)
CityJob.find_or_create_by!(job_id: job_id, city_id: city_id)
job[:industries].each do |job_industry| job[:industries].each do |job_industry|
job_industry = job_industry.strip job_industry = job_industry.strip
industry_id = get_industry(job_industry).id industry_id = Industry.find_or_create_by!(name:job_industry).id
IndustryJob.find_or_create_by!(industry_id: industry_id, job_id: job_id) IndustryJob.find_or_create_by!(industry_id: industry_id, job_id:saved_job.id)
end end
end end
end end
...@@ -57,29 +57,12 @@ class CrawlData ...@@ -57,29 +57,12 @@ class CrawlData
company company
end end
def get_industry(name) def save_job(job_attrs)
industry = Industry.find_or_create_by!(name: name) attrs = job_attrs[:expiration_date].nil? ? {title: job_attrs[:title], company_id: job_attrs[:company_id]} :
industry {code: job_attrs[:code]}
end
def get_city(name)
name = name.strip
City.find_or_create_by(name: name, region: "Việt Nam")
end
def get_job(code = nil, title, salary, description, requirement, level, post_date, expiration_date, company_id)
attrs = expiration_date.nil? ? {title: job_title, company_id: company_id} : {code: code}
job = Job.find_or_initialize_by attrs job = Job.find_or_initialize_by attrs
job.update_attributes(job_attrs.except(:workplace, :industries, :company_name, :company_address, :company_description))
job.update(code: code,
title: title,
salary: salary,
post_date: post_date,
description: description,
requirement: requirement,
expiration_date: expiration_date,
level: level,
company_id: company_id)
job job
end end
end end
...@@ -4,17 +4,18 @@ class JobHtml ...@@ -4,17 +4,18 @@ class JobHtml
end end
def parse_job def parse_job
get_job_info job_info = get_job_info
get_job_detail job_detail = get_job_detail
{ title: get_title, { title: get_title,
salary: get_job_info[:salary], salary: job_info[:salary],
level: get_job_info[:level], level: job_info[:level],
post_date: get_post_date, post_date: get_post_date,
description: get_job_detail[:description], description: job_detail[:description],
requirement: get_job_detail[:requirement], requirement: job_detail[:requirement],
expiration_date: get_job_info[:expiration_date], expiration_date: job_info[:expiration_date],
workplace: get_job_info[:workplace], workplace: job_info[:workplace],
industries: get_job_info[:industries], industries: job_info[:industries],
company_name: get_company_name, company_name: get_company_name,
company_address: get_company_address, company_address: get_company_address,
company_description: get_company_description } company_description: get_company_description }
...@@ -50,7 +51,7 @@ class JobHtml ...@@ -50,7 +51,7 @@ class JobHtml
end end
end end
return job_info job_info
end end
def get_job_detail def get_job_detail
...@@ -66,7 +67,7 @@ class JobHtml ...@@ -66,7 +67,7 @@ class JobHtml
end end
end end
return job_detail job_detail
end end
def get_company_name def get_company_name
......
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