~ubuntu-branches/ubuntu/maverick/newsbeuter/maverick

« back to all changes in this revision

Viewing changes to contrib/heise.rb

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2009-12-08 21:20:22 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20091208212022-agupa2xecomowov8
Tags: 2.1-1
* New upstream release
  - fix rss title parsing (Closes: #526612).
  - fix FTBFS on kFreeBSD (Closes: #543591).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby
 
2
#
 
3
# get, parse and enrich heise rss feeds
 
4
#
 
5
# call with the feed specified you like to retrieve. Currently supported:
 
6
#
 
7
#  news      - heise newsticker
 
8
#  teleopils - Telepolis
 
9
#  security  - heise security news
 
10
#
 
11
# Change history
 
12
#
 
13
#  26.06.2009    erb    suppressed error messages due to unrepsonsive servers
 
14
#
 
15
 
 
16
require 'net/http'
 
17
require 'uri'
 
18
 
 
19
require 'rexml/document'
 
20
include REXML
 
21
 
 
22
require "open-uri"
 
23
require 'timeout'
 
24
 
 
25
#try to retrieve web site, following up to 5 redirects
 
26
def geturl(url, depth=5)
 
27
  raise ArgumentError, 'Followed more 4 redirections. Stopping this nightmare now.' if depth == 0
 
28
  response = Net::HTTP.get_response(URI.parse(url))
 
29
  case response
 
30
    when Net::HTTPSuccess     then response.body
 
31
    when Net::HTTPRedirection then geturl(response['location'], depth-1) # follow redirection
 
32
  else
 
33
    response.error!
 
34
  end
 
35
end
 
36
 
 
37
if ENV['http_proxy'].nil? && !ENV['HTTP_PROXY'].nil?
 
38
  ENV['http_proxy'] = ENV['HTTP_PROXY']
 
39
end
 
40
 
 
41
#          key            feed URL
 
42
FEEDS = { "news"      => "http://www.heise.de/newsticker/heise.rdf",
 
43
          "telepolis" => "http://www.heise.de/tp/news.rdf",
 
44
          "security"  => "http://www.heise.de/security/news/news.rdf"
 
45
        }
 
46
 
 
47
GOOGLEON="<!--googleon: index-->"
 
48
GOOGLEOFF="<!--googleoff: index-->"
 
49
 
 
50
def listFeeds
 
51
  FEEDS.each_key { |k| print "  #{k}\n" }
 
52
end
 
53
 
 
54
if ARGV.length < 1
 
55
  print "usage: #{File::basename($0)} <feed>\n"
 
56
  print "<feed> is one of\n"
 
57
  listFeeds
 
58
  exit
 
59
end
 
60
 
 
61
feed=ARGV[0]
 
62
 
 
63
unless FEEDS.has_key?(feed)
 
64
  print "unknown feed '#{feed}'. Use one of these:\n"
 
65
  listFeeds
 
66
  exit
 
67
end
 
68
 
 
69
feedurl = FEEDS[feed]
 
70
 
 
71
#get feed
 
72
feed_text = ""
 
73
retries=4
 
74
begin
 
75
  Timeout::timeout(15) do
 
76
    f = open(feedurl)
 
77
    feed_text = f.read unless f.nil?
 
78
  end
 
79
rescue Timeout::Error
 
80
  retries -= 1
 
81
  exit 1 if retries < 1
 
82
  sleep 1
 
83
  retry
 
84
end
 
85
 
 
86
exit 2 if feed_text.length < 20
 
87
 
 
88
#print "Got this feed: ", feed_text, "\n"; STDOUT.flush
 
89
 
 
90
xml = Document.new(feed_text)
 
91
 
 
92
#loop over items
 
93
xml.elements.each("//item") do |item|
 
94
  # extract link to article
 
95
  article_url = item.elements['link'].text
 
96
  article_url.sub!(%r{from/rss.*$}, "")
 
97
  article_short_url = article_url.sub(%r{/[^/]*--/}, "/")
 
98
 
 
99
  # get full text for article
 
100
  article_text = ""
 
101
  retries = 4
 
102
  begin
 
103
#    print "<!-- Reading article from ", article_url, " -->\n"; STDOUT.flush
 
104
    Timeout::timeout(15) do
 
105
      article = open(article_url)
 
106
      article_text = article.read unless article.nil?
 
107
    end
 
108
  rescue Timeout::Error
 
109
    retries -= 1
 
110
    next if retries < 1
 
111
    sleep 1
 
112
    retry
 
113
  end
 
114
 
 
115
  next if article_text.length < 20
 
116
 
 
117
  article_text.gsub!(/<!\[CDATA\[/, "")
 
118
  article_text.gsub!(/\]\]>/, "")
 
119
 
 
120
  # now, heise speciality: get everything between GOOGLEON and GOOGLEOFF patterns :-)
 
121
  p1 = article_text.index(GOOGLEON)
 
122
  p2 = article_text.index(GOOGLEOFF)
 
123
  if (p1 && p2)
 
124
    result = ""
 
125
    pos = p1
 
126
    while(pos < article_text.length) do
 
127
      p1 = article_text.index(GOOGLEON, pos)
 
128
      break unless p1
 
129
      p2 = article_text.index(GOOGLEOFF, pos)
 
130
      p2 = article_text.length unless p2
 
131
      if p1 < p2
 
132
        result += article_text[p1+GOOGLEON.length..p2-1]
 
133
        pos=p2+GOOGLEOFF.length
 
134
      else
 
135
        pos=p1+GOOGLEON.length
 
136
      end
 
137
    end
 
138
    article_text = result
 
139
  end
 
140
 
 
141
  # get rid of comments and other annoying artifacts
 
142
  article_text.gsub!(/<!--LINK_ICON--><img[^>]*><!--\/LINK_ICON-->/m, " ")
 
143
  article_text.gsub!(/<!--[^>]*-->/, "")
 
144
  article_text.gsub!(/\s+/m, " ")
 
145
 
 
146
  # insert full text article into feed
 
147
  description = Element.new("description")
 
148
  description.text= CData.new(article_text)
 
149
  item.add_element(description)
 
150
 
 
151
  guid = Element.new("guid")
 
152
  guid.text= article_short_url
 
153
  item.add_element(guid)
 
154
end
 
155
  
 
156
#reproduce enriched feed
 
157
xml.write($stdout, -1)