import_old_spb_news.rake
1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'csv'
require 'net/http'
namespace :spb do
desc "Import news from CSV file"
task :import_old_spb_news => :environment do
error ENV["CSV_FILE"].blank?, "csv file with all the news is required to run this task!
Ex.: rake spb:import_old_spb_news CSV_FILE=my_path/my_csv_file.csv."
spb_profile = Profile['spb']
error spb_profile.blank?, "There is no SPB community."
spb_blog = spb_profile.articles.find_by :slug => "noticias"
error spb_blog.nil?, "There is no SPB blog named 'noticias' in the SPB community to import the news to."
CSV.foreach(ENV["CSV_FILE"], encoding: "UTF-8", headers: true, col_sep: "|", quote_char: "\"") do |row|
date = DateTime.parse(row["max"])
body = row["content"]
body.gsub!("softwarepublico.gov","antigo.softwarepublico.gov") unless body.blank?
body.gsub!("www.","") unless body.blank?
attrs={
:published_at => date,
:published => (row["publish_status"] == "ready"),
:body => body,
:highlighted => true,
:parent => spb_blog,
:profile => spb_profile,
:name => row["title"]
}
article = Article.find_by(:slug => row["title"].to_slug)
if article.blank?
article = TinyMceArticle.new(attrs)
article.created_at = date
end
article.save!
puts "#{spb_blog.slug}: Importing article: #{article.name}..."
end
puts "", "Deleting standard blog..."
old_blog = spb_profile.articles.find_by :slug => "blog"
old_blog.destroy if (old_blog.present? && old_blog.children.count.zero?)
end
def error failure_condition, msg="ERROR!!"
if failure_condition
puts msg
exit 1
end
end
end