gist: 4640 Download_button fork
public
Public Clone URL: git://gist.github.com/4640.git
brazilian-blogs-filters
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
require 'hpricot'
require 'open-uri'
 
# update the database with the actual titles from each site
Blog.all.each do |b|
  puts b.url
  begin
    doc = Hpricot(open(b.url.strip))
    title = (doc/"html/*/title/").to_s
    if title
      b.url = b.url.strip
      b.blog_name = title.strip
    else
      b.blog_name = b.author
    end
    b.save
  rescue
    b.destroy
  end
end
 
# update the database with feed info
Blog.all.each do |b|
  puts b.url
  begin
    doc = Hpricot(open(b.url.strip))
    links = (doc/"link[@rel='alternate']")
    href, feed_type = nil, nil
    if links
      if links.size > 1
        tmp = links.select{|l| l.attributes['title'] =~ /All/ || l.attributes['title'] !~ /comment/ }
        links = tmp if tmp
      end
      href = links.first.attributes['href']
      feed_type = links.first.attributes['type'] rescue ""
    end
    if href
      puts "feed: #{href} - type: #{feed_type}"
      b.feed = href =~ /^http/ ? href.strip : b.url.strip + href.strip
      b.feed_type = feed_type.strip
      b.save
    else
      puts "NOT FOUND"
    end
  rescue => e
    puts e
  end
end
 
# prints out each html table line
Blog.all(:order => :random_number).each do |b|
  puts <<-EOF
  <tr>
    <td><a href="#{b.url}" target="_blank">#{b.blog_name}</a></td>
    <td>#{b.author}</td>
  </tr>
  EOF
end
 
# prints out each OPML outline row
Blog.all(:order => :random_number, :conditions => "feed <> ''").each do |b|
  puts <<-EOF
        <outline text="#{b.blog_name}"
            title="#{b.blog_name}" type="rss"
            xmlUrl="#{b.feed}" htmlUrl="#{b.url}"/>
  EOF
end
 
 

Owner

akitaonrails

Revisions