Guest User

Untitled

a guest
Jul 13th, 2018
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. require 'rubygems'
  2. require 'active_record'
  3. require 'feed_tools'
  4.  
  5.  
  6. feed_url = ARGV[0]
  7. # This call creates a connection to our database.
  8. ActiveRecord::Base.establish_connection(
  9. :adapter => "mysql",
  10. :host => "127.0.0.1",
  11. :username => "ruby", # Note that while this is the default setting for MySQL,
  12. :password => "password", # a properly secured system will have a different MySQL
  13. # username and password, and if so, you'll need to
  14. # change these settings.
  15. :database => "rss2mysql")
  16. class Items < ActiveRecord::Base
  17. end
  18. # If the table doesn't exist, we'll create it.
  19. unless Items.table_exists?
  20. ActiveRecord::Schema.define do
  21. create_table :items do |t|
  22. t.column :title, :string
  23. t.column :content, :string
  24. t.column :source, :string
  25. t.column :url, :string
  26. t.column :timestamp, :timestamp
  27. t.column :keyword_id, :integer
  28. t.column :guid, :string
  29. end
  30. end
  31. end
  32. feed=FeedTools::Feed.open(feed_url)
  33. feed.items.each do |feed_item|
  34. if not (Items.find_by_title(feed_item.title) \
  35. or Items.find_by_url(feed_item.link) \
  36. or Items.find_by_guid(feed_item.guid))
  37. puts "processing item '#{feed_item.title}' - new"
  38.  
  39. Items.new do |newitem|
  40. newitem.title=feed_item.title.gsub(/<[^>]*>/, '')
  41. newitem.guid=feed_item.guid
  42. if feed_item.publisher.name
  43. newitem.source=feed_item.publisher.name
  44. end
  45. newitem.url=feed_item.link
  46. newitem.content=feed_item.description
  47. newitem.timestamp=feed_item.published
  48. newitem.save
  49. end
  50. else
  51. puts "processing item '#{feed_item.title}' - old"
  52. end
  53. end
Add Comment
Please, Sign In to add comment