Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/ftp'
- require 'nokogiri'
- require 'date'
- namespace :xml_parser do
- task goods_parse: :environment do
- # server = "unity-dana.ru"
- # user = "site"
- # password = "site2016"
- # ftp = Net::FTP.new(server, user, password)
- # ftp.passive = true
- # ftp.chdir("/\u0421\u0410\u0419\u0422")
- # a = ftp.getbinaryfile("\xD0\xAE\xD0\x9D\xD0\x98\xD0\xA2\xD0\x98.xml", nil)
- # doc = Nokogiri::XML(a)
- a = File.open("ЮНИТИ.xml")
- doc = Nokogiri::XML(a)
- p "------------------------------------success!------------------------------------"
- doc.xpath("//Theme").each do |theme|
- category = Category.where(name: theme.at_css("SubjectHeadingText").text).first_or_create(name: theme.at_css("SubjectHeadingText").text)
- theme.css("Product").each do |product|
- puts product
- author_surname = product.css("KeyNames").text
- author_name = product.css("NamesBeforeKey").text
- full_name = author_name + " " + author_surname
- author = Author.where(name: full_name).first_or_create(name: full_name)
- #category
- #good
- #name
- name = product.css("TitleText")[0].text if product.css("TitleText")[0]
- #publish_date
- year = product.css("Date").text.to_i
- cover = product.css("ProductFormDescription").text
- date = Date.new(year)
- #weight
- weight = product.css("Measure")[0].css("Measurement").text if product.css("Measure")[0]
- #price
- price = product.css("PriceAmount").text
- #theme
- theme = product.css("TitleText")[1].text if product.css("TitleText")[1]
- #content
- content = product.css("Text").text
- vendor_code = product.css("RecordReference").text
- status = product.css("NotificationType").text
- isbn = product.css("IDValue").text
- #add dat fields
- publisher_name = product.css("PublisherName").text
- publish_city = product.css("CityOfPublication").text
- tax_percent = product.css("TaxRatePercent").text
- product_amount = product.css("ProductAvailability").text
- printed_amount = product.css("InitialPrintRun").text
- height = product.css("Measure")[1].css("Measurement").text if product.css("Measure")[1]
- width = product.css("Measure")[2].css("Measurement").text if product.css("Measure")[2]
- length = product.css("Measure")[3].css("Measurement").text if product.css("Measure")[3]
- puts product
- good = Good.where(name: name).first_or_create(name: name, publish_date: date, weight: weight, price: price,
- theme: theme, publisher_name: publisher_name, publish_city: publish_city,
- tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
- height: height, width: width, length: length, content: content, status: status,
- isbn: isbn, vendor_code: vendor_code, cover: cover)
- good.update_attributes(name: name, publish_date: date, weight: weight, price: price,
- theme: theme, publisher_name: publisher_name, publish_city: publish_city,
- tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
- height: height, width: width, length: length, content: content, status: status,
- isbn: isbn, vendor_code: vendor_code, cover: cover)
- category.goods << good
- good.authors << author if good.authors.empty?
- p "------------------------------------success!------------------------------------"
- end
- end
- # doc.xpath("//Product").each do |product|
- # #author
- # author_surname = product.css("KeyNames").text
- # author_name = product.css("NamesBeforeKey").text
- # full_name = author_name + " " + author_surname
- # author = Author.where(name: full_name).first_or_create(name: full_name)
- # #category
- # type = product.css("SubjectHeadingText").text
- # category = Category.where(name: type).first_or_create(name: type)
- # #good
- # #name
- # name = product.css("TitleText")[0].text if product.css("TitleText")[0]
- # #publish_date
- # year = product.css("Date").text.to_i
- # date = Date.new(year)
- # #weight
- # weight = product.css("Measure")[0].css("Measurement").text if product.css("Measure")[0]
- # #price
- # price = product.css("PriceAmount").text
- # #theme
- # theme = product.css("TitleText")[1].text if product.css("TitleText")[1]
- # #content
- # content = product.css("Text").text
- # vendor_code = product.css("RecordReference").text
- # status = product.css("NotificationType").text
- # isbn = product.css("IDValue").text
- # #add dat fields
- # publisher_name = product.css("PublisherName").text
- # publish_city = product.css("CityOfPublication").text
- # tax_percent = product.css("TaxRatePercent").text
- # product_amount = product.css("ProductAvailability").text
- # printed_amount = product.css("InitialPrintRun").text
- # height = product.css("Measure")[1].css("Measurement").text if product.css("Measure")[1]
- # width = product.css("Measure")[2].css("Measurement").text if product.css("Measure")[2]
- # length = product.css("Measure")[3].css("Measurement").text if product.css("Measure")[3]
- # good = Good.where(name: name).first_or_create(name: name, publish_date: date, weight: weight, price: price,
- # theme: theme, publisher_name: publisher_name, publish_city: publish_city,
- # tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
- # height: height, width: width, length: length, content: content, status: status,
- # isbn: isbn, vendor_code: vendor_code)
- # good.update_attributes(name: name, publish_date: date, weight: weight, price: price,
- # theme: theme, publisher_name: publisher_name, publish_city: publish_city,
- # tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
- # height: height, width: width, length: length, content: content, status: status,
- # isbn: isbn, vendor_code: vendor_code)
- # product.css("ProductSubject").each do |s|
- # t = Theme.where(name: s.text).first_or_create(name: s.text)
- # if good.themes.where(name: s.text).empty?
- # good.themes << t
- # end
- # end
- # good.authors << author if good.authors.empty?
- # p "------------------------------------success!------------------------------------"
- # end
- end
- task journal_parse: :environment do
- server = "unity-dana.ru"
- user = "site"
- password = "site2016"
- ftp = Net::FTP.new(server, user, password)
- ftp.passive = true
- ftp.chdir("/\u0421\u0410\u0419\u0422")
- a = ftp.getbinaryfile("журналы.xml", nil)
- doc = Nokogiri::XML(a)
- superthemecode = doc.xpath('//Theme').first.xpath('SubjectCode').text
- superthemetitle = doc.xpath('//Theme').first.xpath('SubjectHeadingText').text
- supertheme = Theme.where(name: superthemetitle).first_or_create(name: superthemetitle)
- doc.xpath('//Theme').first.xpath('Theme').each do |theme|
- theme.xpath('Product').each do |product|
- recordreference = product.css('RecordReference').text
- notificationtype = product.css('NotificationType').text
- productsubject = product.css('ProductSubject').text
- collectionidtype = product.css('CollectionIDType').text
- idvalue = product.css('IDValue').text
- titletype = product.css('TitleType').text
- titleelementlevel = product.css('TitleElementLevel').text
- extenttype = product.css('ExtentType').text
- extentvalue = product.css('ExtentValue').text
- extentunit = product.css('ExtentUnit').text
- productform = product.css('ProductForm').text
- productformdescription = product.css('ProductFormDescription').text
- texttype = product.css('TextType').text
- contentaudience = product.css('ContentAudience').text
- text = product.css('Text').text
- pricetype = product.css('PriceType').text
- priceamount = product.css('PriceAmount').text
- taxtype = product.css('TaxType').text
- taxratepercent = product.css('TaxRatePercent').text
- title = product.css('TitleText').text
- j = Journal.where(recordreference: recordreference, notificationtype: notificationtype, productsubject: productsubject,
- collectionidtype: collectionidtype, idvalue: idvalue, titletype: titletype, titleelementlevel: titleelementlevel,
- extenttype: extenttype, extentvalue: extentvalue, extentunit: extentunit, productform: productform,
- productformdescription: productformdescription, texttype: texttype, contentaudience: contentaudience, text: text,
- pricetype: pricetype, priceamount: priceamount, taxtype: taxtype, taxratepercent: taxratepercent, title:title).first_or_create(recordreference: recordreference, notificationtype: notificationtype, productsubject: productsubject,
- collectionidtype: collectionidtype, idvalue: idvalue, titletype: titletype, titleelementlevel: titleelementlevel,
- extenttype: extenttype, extentvalue: extentvalue, extentunit: extentunit, productform: productform,
- productformdescription: productformdescription, texttype: texttype, contentaudience: contentaudience, text: text,
- pricetype: pricetype, priceamount: priceamount, taxtype: taxtype, taxratepercent: taxratepercent, title:title)
- subjectcode = theme.css("SubjectCode").text
- subjectheadingtext = theme.css("SubjectHeadingText").text
- t = Theme.where(name: subjectheadingtext).first_or_create(name: subjectheadingtext)
- supertheme.themes << t
- j.themes << t
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement