Guest User

Untitled

a guest
Feb 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. require 'hpricot'
  2. require 'open-uri'
  3.  
  4. # update the database with the actual titles from each site
  5. Blog.all.each do |b|
  6. puts b.url
  7. begin
  8. doc = Hpricot(open(b.url.strip))
  9. title = (doc/"html/*/title/").to_s
  10. if title
  11. b.url = b.url.strip
  12. b.blog_name = title.strip
  13. else
  14. b.blog_name = b.author
  15. end
  16. b.save
  17. rescue
  18. b.destroy
  19. end
  20. end
  21.  
  22. # update the database with feed info
  23. Blog.all.each do |b|
  24. puts b.url
  25. begin
  26. doc = Hpricot(open(b.url.strip))
  27. links = (doc/"link[@rel='alternate']")
  28. href, feed_type = nil, nil
  29. if links
  30. if links.size > 1
  31. tmp = links.select{|l| l.attributes['title'] =~ /All/ || l.attributes['title'] !~ /comment/ }
  32. links = tmp if tmp
  33. end
  34. href = links.first.attributes['href']
  35. feed_type = links.first.attributes['type'] rescue ""
  36. end
  37. if href
  38. puts "feed: #{href} - type: #{feed_type}"
  39. b.feed = href =~ /^http/ ? href.strip : b.url.strip + href.strip
  40. b.feed_type = feed_type.strip
  41. b.save
  42. else
  43. puts "NOT FOUND"
  44. end
  45. rescue => e
  46. puts e
  47. end
  48. end
  49.  
  50. # prints out each html table line
  51. Blog.all(:order => :random_number).each do |b|
  52. puts <<-EOF
  53. <tr>
  54. <td><a href="#{b.url}" target="_blank">#{b.blog_name}</a></td>
  55. <td>#{b.author}</td>
  56. </tr>
  57. EOF
  58. end
  59.  
  60. # prints out each OPML outline row
  61. Blog.all(:order => :random_number, :conditions => "feed <> ''").each do |b|
  62. puts <<-EOF
  63. <outline text="#{b.blog_name}"
  64. title="#{b.blog_name}" type="rss"
  65. xmlUrl="#{b.feed}" htmlUrl="#{b.url}"/>
  66. EOF
  67. end
Add Comment
Please, Sign In to add comment