public
Description: This contains various plugins for Feather
Clone URL: git://github.com/eldiablo/feather-plugins.git
Click here to lend your support to: feather-plugins and make a donation at www.pledgie.com !
100644 84 lines (79 sloc) 3.277 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Admin
  class Importer < Base
    include_plugin_views __FILE__
 
    def show
      render
    end
 
    def create
      # Process the article feed if specified
      @articles = process_articles(params[:articles_url]) if params[:articles_url] && params[:articles_url] != ""
      # Process the comment feed if specified
      @comments = process_comments(params[:comments_url]) if params[:comments_url] && params[:comments_url] != "" && (defined?(Comment) && is_plugin_active("feather-comments"))
      # Render the results
      render
    end
 
    private
      ##
      # This processes the articles feed url
      def process_articles(url)
        # Create an array to store the processed articles
        processed = []
        # Grab the article items from the feed
        articles = (retrieve(url)/"rss"/"channel"/"item")
        # Loop through them
        articles.each do |a|
          # Build the permalink
          permalink = URI.parse((a/"guid").text).request_uri
          # Find the article, or create a new one
          article = Article.first(:permalink => permalink) || Article.new
          # Grab the information from the article feed item
          article.title = (a/"title").text
          article.content = (a/"description").text
          article.published = "1"
          article.published_at = DateTime.parse((a/"pubdate").text)
          article.permalink = permalink
          article.user_id = self.current_user.id
          # Add the tags, if present in the feed, and if the tagging plugin is active
          article.tag_list = (a/"category").collect { |c| c.children.first }.join(",") if is_plugin_active("feather-tagging") && defined?(Tag) && defined?(Tagging) && article.respond_to?("tag_list=") && (a/"category") && (a/"category").length > 0
          # Save the article
          article.save
          # Add it to the list of processed articles
          processed << article
        end
        # Return the list of processed articles
        processed
      end
 
      ##
      # This processes the comments feed url
      def process_comments(url)
        # Ensure the comment plugin exists
        raise "Unable to process comments: comment plugin not detected!" unless defined?(Comment)
        # Create an array to store the processed comments
        processed = []
        # Grab the comment items from the feed
        comments = (retrieve(url)/"rss"/"channel"/"item")
        # Loop through them
        comments.each do |c|
          # Create a new comment
          comment = Comment.new
          # Grab the information from the comment feed item
          comment.comment = (c/"description").text
          comment.name = (c/"dc:creator").text
          comment.created_at = DateTime.parse((c/"pubdate").text)
          a = Article.first(:title => (c/"title").text.gsub("Re: ", ""))
          comment.article_id = a.id unless a.nil?
          # Save the comment
          comment.save
          # Add it to the list of processed comments
          processed << comment
        end
        # Return the list of processed comments
        processed
      end
 
      ##
      # This retrieves the content of the specified url
      def retrieve(url)
        Hpricot(Net::HTTP.get(URI.parse(url)))
      end
  end
end