Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. require 'net/ftp'
  2. require 'nokogiri'
  3. require 'date'
  4.  
  5. namespace :xml_parser do
  6. task goods_parse: :environment do
  7. # server = "unity-dana.ru"
  8. # user = "site"
  9. # password = "site2016"
  10. # ftp = Net::FTP.new(server, user, password)
  11. # ftp.passive = true
  12. # ftp.chdir("/\u0421\u0410\u0419\u0422")
  13. # a = ftp.getbinaryfile("\xD0\xAE\xD0\x9D\xD0\x98\xD0\xA2\xD0\x98.xml", nil)
  14. # doc = Nokogiri::XML(a)
  15. a = File.open("ЮНИТИ.xml")
  16. doc = Nokogiri::XML(a)
  17. p "------------------------------------success!------------------------------------"
  18. doc.xpath("//Theme").each do |theme|
  19. category = Category.where(name: theme.at_css("SubjectHeadingText").text).first_or_create(name: theme.at_css("SubjectHeadingText").text)
  20. theme.css("Product").each do |product|
  21. puts product
  22. author_surname = product.css("KeyNames").text
  23. author_name = product.css("NamesBeforeKey").text
  24. full_name = author_name + " " + author_surname
  25. author = Author.where(name: full_name).first_or_create(name: full_name)
  26. #category
  27. #good
  28.  
  29. #name
  30. name = product.css("TitleText")[0].text if product.css("TitleText")[0]
  31. #publish_date
  32. year = product.css("Date").text.to_i
  33. cover = product.css("ProductFormDescription").text
  34. date = Date.new(year)
  35. #weight
  36. weight = product.css("Measure")[0].css("Measurement").text if product.css("Measure")[0]
  37. #price
  38. price = product.css("PriceAmount").text
  39. #theme
  40. theme = product.css("TitleText")[1].text if product.css("TitleText")[1]
  41. #content
  42. content = product.css("Text").text
  43. vendor_code = product.css("RecordReference").text
  44. status = product.css("NotificationType").text
  45. isbn = product.css("IDValue").text
  46. #add dat fields
  47. publisher_name = product.css("PublisherName").text
  48. publish_city = product.css("CityOfPublication").text
  49. tax_percent = product.css("TaxRatePercent").text
  50. product_amount = product.css("ProductAvailability").text
  51. printed_amount = product.css("InitialPrintRun").text
  52. height = product.css("Measure")[1].css("Measurement").text if product.css("Measure")[1]
  53. width = product.css("Measure")[2].css("Measurement").text if product.css("Measure")[2]
  54. length = product.css("Measure")[3].css("Measurement").text if product.css("Measure")[3]
  55. puts product
  56. good = Good.where(name: name).first_or_create(name: name, publish_date: date, weight: weight, price: price,
  57. theme: theme, publisher_name: publisher_name, publish_city: publish_city,
  58. tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
  59. height: height, width: width, length: length, content: content, status: status,
  60. isbn: isbn, vendor_code: vendor_code, cover: cover)
  61. good.update_attributes(name: name, publish_date: date, weight: weight, price: price,
  62. theme: theme, publisher_name: publisher_name, publish_city: publish_city,
  63. tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
  64. height: height, width: width, length: length, content: content, status: status,
  65. isbn: isbn, vendor_code: vendor_code, cover: cover)
  66. category.goods << good
  67. good.authors << author if good.authors.empty?
  68. p "------------------------------------success!------------------------------------"
  69. end
  70. end
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. # doc.xpath("//Product").each do |product|
  87. # #author
  88. # author_surname = product.css("KeyNames").text
  89. # author_name = product.css("NamesBeforeKey").text
  90. # full_name = author_name + " " + author_surname
  91. # author = Author.where(name: full_name).first_or_create(name: full_name)
  92. # #category
  93.  
  94.  
  95.  
  96. # type = product.css("SubjectHeadingText").text
  97. # category = Category.where(name: type).first_or_create(name: type)
  98. # #good
  99.  
  100. # #name
  101. # name = product.css("TitleText")[0].text if product.css("TitleText")[0]
  102. # #publish_date
  103. # year = product.css("Date").text.to_i
  104. # date = Date.new(year)
  105. # #weight
  106. # weight = product.css("Measure")[0].css("Measurement").text if product.css("Measure")[0]
  107. # #price
  108. # price = product.css("PriceAmount").text
  109. # #theme
  110. # theme = product.css("TitleText")[1].text if product.css("TitleText")[1]
  111. # #content
  112. # content = product.css("Text").text
  113. # vendor_code = product.css("RecordReference").text
  114. # status = product.css("NotificationType").text
  115. # isbn = product.css("IDValue").text
  116. # #add dat fields
  117. # publisher_name = product.css("PublisherName").text
  118. # publish_city = product.css("CityOfPublication").text
  119. # tax_percent = product.css("TaxRatePercent").text
  120. # product_amount = product.css("ProductAvailability").text
  121. # printed_amount = product.css("InitialPrintRun").text
  122. # height = product.css("Measure")[1].css("Measurement").text if product.css("Measure")[1]
  123. # width = product.css("Measure")[2].css("Measurement").text if product.css("Measure")[2]
  124. # length = product.css("Measure")[3].css("Measurement").text if product.css("Measure")[3]
  125. # good = Good.where(name: name).first_or_create(name: name, publish_date: date, weight: weight, price: price,
  126. # theme: theme, publisher_name: publisher_name, publish_city: publish_city,
  127. # tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
  128. # height: height, width: width, length: length, content: content, status: status,
  129. # isbn: isbn, vendor_code: vendor_code)
  130. # good.update_attributes(name: name, publish_date: date, weight: weight, price: price,
  131. # theme: theme, publisher_name: publisher_name, publish_city: publish_city,
  132. # tax_percent: tax_percent, product_amount: product_amount, printed_amount: printed_amount,
  133. # height: height, width: width, length: length, content: content, status: status,
  134. # isbn: isbn, vendor_code: vendor_code)
  135. # product.css("ProductSubject").each do |s|
  136. # t = Theme.where(name: s.text).first_or_create(name: s.text)
  137. # if good.themes.where(name: s.text).empty?
  138. # good.themes << t
  139. # end
  140. # end
  141. # good.authors << author if good.authors.empty?
  142.  
  143. # p "------------------------------------success!------------------------------------"
  144.  
  145.  
  146. # end
  147. end
  148.  
  149. task journal_parse: :environment do
  150. server = "unity-dana.ru"
  151. user = "site"
  152. password = "site2016"
  153. ftp = Net::FTP.new(server, user, password)
  154. ftp.passive = true
  155. ftp.chdir("/\u0421\u0410\u0419\u0422")
  156. a = ftp.getbinaryfile("журналы.xml", nil)
  157. doc = Nokogiri::XML(a)
  158. superthemecode = doc.xpath('//Theme').first.xpath('SubjectCode').text
  159. superthemetitle = doc.xpath('//Theme').first.xpath('SubjectHeadingText').text
  160. supertheme = Theme.where(name: superthemetitle).first_or_create(name: superthemetitle)
  161. doc.xpath('//Theme').first.xpath('Theme').each do |theme|
  162.  
  163. theme.xpath('Product').each do |product|
  164. recordreference = product.css('RecordReference').text
  165. notificationtype = product.css('NotificationType').text
  166. productsubject = product.css('ProductSubject').text
  167. collectionidtype = product.css('CollectionIDType').text
  168. idvalue = product.css('IDValue').text
  169. titletype = product.css('TitleType').text
  170. titleelementlevel = product.css('TitleElementLevel').text
  171. extenttype = product.css('ExtentType').text
  172. extentvalue = product.css('ExtentValue').text
  173. extentunit = product.css('ExtentUnit').text
  174. productform = product.css('ProductForm').text
  175. productformdescription = product.css('ProductFormDescription').text
  176. texttype = product.css('TextType').text
  177. contentaudience = product.css('ContentAudience').text
  178. text = product.css('Text').text
  179. pricetype = product.css('PriceType').text
  180. priceamount = product.css('PriceAmount').text
  181. taxtype = product.css('TaxType').text
  182. taxratepercent = product.css('TaxRatePercent').text
  183.  
  184.  
  185. title = product.css('TitleText').text
  186.  
  187. j = Journal.where(recordreference: recordreference, notificationtype: notificationtype, productsubject: productsubject,
  188. collectionidtype: collectionidtype, idvalue: idvalue, titletype: titletype, titleelementlevel: titleelementlevel,
  189. extenttype: extenttype, extentvalue: extentvalue, extentunit: extentunit, productform: productform,
  190. productformdescription: productformdescription, texttype: texttype, contentaudience: contentaudience, text: text,
  191. pricetype: pricetype, priceamount: priceamount, taxtype: taxtype, taxratepercent: taxratepercent, title:title).first_or_create(recordreference: recordreference, notificationtype: notificationtype, productsubject: productsubject,
  192. collectionidtype: collectionidtype, idvalue: idvalue, titletype: titletype, titleelementlevel: titleelementlevel,
  193. extenttype: extenttype, extentvalue: extentvalue, extentunit: extentunit, productform: productform,
  194. productformdescription: productformdescription, texttype: texttype, contentaudience: contentaudience, text: text,
  195. pricetype: pricetype, priceamount: priceamount, taxtype: taxtype, taxratepercent: taxratepercent, title:title)
  196. subjectcode = theme.css("SubjectCode").text
  197. subjectheadingtext = theme.css("SubjectHeadingText").text
  198. t = Theme.where(name: subjectheadingtext).first_or_create(name: subjectheadingtext)
  199. supertheme.themes << t
  200. j.themes << t
  201. end
  202. end
  203. end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement