rfeedreader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 0.1.0 2007-09-01
2
+
3
+ * Initial release
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Alexandre Girard
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/rfeedreader.rb
7
+ lib/rfeedreader/version.rb
8
+ scripts/txt2html
9
+ setup.rb
10
+ test/test_helper.rb
11
+ test/test_rfeedreader.rb
12
+ website/index.html
13
+ website/index.txt
14
+ website/javascripts/rounded_corners_lite.inc.js
15
+ website/stylesheets/screen.css
16
+ website/template.rhtml
data/README.txt ADDED
@@ -0,0 +1,5 @@
1
+ README for rfeedreader
2
+ ======================
3
+
4
+ Feed parser to read feed and return first posts of this feed.
5
+ Special parsing from sources like Flickr, Jumcut, Google video, ...
data/Rakefile ADDED
@@ -0,0 +1,132 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+
12
+ include FileUtils
13
+ require File.join(File.dirname(__FILE__), 'lib', 'rfeedreader', 'version')
14
+
15
+ AUTHOR = 'Alexandre Girard' # can also be an array of Authors
16
+ EMAIL = "alx.girard@gmail.com"
17
+ DESCRIPTION = "Feed parser to read feed and return first posts of this feed. Special parsing from sources like Flickr, Jumcut, Google video, ..."
18
+ GEM_NAME = 'rfeedreader' # what ppl will type to install your gem
19
+
20
+ @config_file = "~/.rubyforge/user-config.yml"
21
+ @config = nil
22
+ def rubyforge_username
23
+ unless @config
24
+ begin
25
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
26
+ rescue
27
+ puts <<-EOS
28
+ ERROR: No rubyforge config file found: #{@config_file}"
29
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
30
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
31
+ EOS
32
+ exit
33
+ end
34
+ end
35
+ @rubyforge_username ||= @config["username"]
36
+ end
37
+
38
+ RUBYFORGE_PROJECT = 'rfeedreader' # The unix name for your project
39
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
40
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
41
+
42
+ NAME = "rfeedreader"
43
+ REV = nil
44
+ # UNCOMMENT IF REQUIRED:
45
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
46
+ VERS = Rfeedreader::VERSION::STRING + (REV ? ".#{REV}" : "")
47
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
48
+ RDOC_OPTS = ['--quiet', '--title', 'rfeedreader documentation',
49
+ "--opname", "index.html",
50
+ "--line-numbers",
51
+ "--main", "README",
52
+ "--inline-source"]
53
+
54
+ class Hoe
55
+ def extra_deps
56
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
57
+ end
58
+ end
59
+
60
+ # Generate all the Rake tasks
61
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
62
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
63
+ p.author = AUTHOR
64
+ p.description = DESCRIPTION
65
+ p.email = EMAIL
66
+ p.summary = DESCRIPTION
67
+ p.url = HOMEPATH
68
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
69
+ p.test_globs = ["test/**/test_*.rb"]
70
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
71
+
72
+ # == Optional
73
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
74
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
75
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
76
+ end
77
+
78
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
79
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
80
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
81
+
82
+ desc 'Generate website files'
83
+ task :website_generate do
84
+ Dir['website/**/*.txt'].each do |txt|
85
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
86
+ end
87
+ end
88
+
89
+ desc 'Upload website files to rubyforge'
90
+ task :website_upload do
91
+ host = "#{rubyforge_username}@rubyforge.org"
92
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
93
+ local_dir = 'website'
94
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
95
+ end
96
+
97
+ desc 'Generate and upload website files'
98
+ task :website => [:website_generate, :website_upload, :publish_docs]
99
+
100
+ desc 'Release the website and new gem version'
101
+ task :deploy => [:check_version, :website, :release] do
102
+ puts "Remember to create SVN tag:"
103
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
104
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
105
+ puts "Suggested comment:"
106
+ puts "Tagging release #{CHANGES}"
107
+ end
108
+
109
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
110
+ task :local_deploy => [:website_generate, :install_gem]
111
+
112
+ task :check_version do
113
+ unless ENV['VERSION']
114
+ puts 'Must pass a VERSION=x.y.z release version'
115
+ exit
116
+ end
117
+ unless ENV['VERSION'] == VERS
118
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
119
+ exit
120
+ end
121
+ end
122
+
123
+ rule "" do |t|
124
+ # test:file:method
125
+ if /test:(.*)(:([^.]+))?$/.match(t.name)
126
+ arguments = t.name.split(":")[1..-1]
127
+ test_name = arguments.first
128
+ run_file_name = "test_rfeedreader.rb"
129
+
130
+ sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/"
131
+ end
132
+ end
@@ -0,0 +1,194 @@
1
+ require 'net/http'
2
+ require 'rubygems'
3
+ require 'open-uri'
4
+ require 'hpricot'
5
+ require 'timeout'
6
+ require 'rfeedfinder'
7
+ require 'helper/texty_helper'
8
+ require 'helper/html_entities'
9
+
10
+ module Rfeedreader
11
+ module_function
12
+
13
+ class Feed
14
+ attr_accessor :title, :link, :charset, :entries
15
+
16
+ def initialize(link, hpricot_doc)
17
+ @link = link
18
+ read_title hpricot_doc
19
+ read_charset hpricot_doc
20
+ @entries = []
21
+ end
22
+
23
+ def parse_entries(hpricot_doc, nb_posts=10)
24
+ @entries = []
25
+ type = self.source_type
26
+
27
+ # Parse each item
28
+ (hpricot_doc/"item|entry")[0..nb_posts - 1].each do |item|
29
+ case type
30
+ when "source_flickr"
31
+ @entries<<Entry_Flickr.new(item, self.charset)
32
+ when "source_fotolog"
33
+ @entries<<Entry_Fotolog.new(item, self.charset)
34
+ when "source_google_video"
35
+ @entries<<Entry_Google_Video.new(item, self.charset)
36
+ when "source_jumpcut"
37
+ @entries<<Entry_Jumpcut.new(item, self.charset)
38
+ when "source_picasa"
39
+ @entries<<Entry_Picasa.new(item, self.charset)
40
+ else
41
+ @entries<<Entry.new(item, self.charset)
42
+ end
43
+ end
44
+ end
45
+
46
+ def to_s
47
+ "Feed: title: #{title} - charset: #{charset}\n\rlink: #{link} - Entries: #{entries.size}"
48
+ end
49
+
50
+ def display_entries
51
+ i = 0
52
+ @entries.each do |entry|
53
+ i += 1
54
+ puts "#{i} - #{entry}"
55
+ end
56
+ end
57
+
58
+ protected
59
+
60
+ def read_charset(hpricot_doc)
61
+ @charset = hpricot_doc.to_s.scan(/encoding=['"]?([^'"]*)['" ]/)
62
+ @charset = @charset[0] if @charset.is_a? Array
63
+ @charset = @charset.to_s.downcase
64
+ end
65
+
66
+ def read_title(hpricot_doc)
67
+ @title = (hpricot_doc/"//title:first").text
68
+ end
69
+
70
+ def source_type
71
+ return "source_flickr" if link =~ /http:\/\/api\.flickr\.com/
72
+ return "source_fotolog" if link =~ /\.fotolog\.com/
73
+ return "source_google_video" if link =~ /http:\/\/video\.google\.com/
74
+ return "source_jumpcut" if link =~ /http:\/\/rss\.jumpcut\.com/
75
+ return "source_picasa" if link =~ /http:\/\/picasaweb\.google\.com/
76
+ return ""
77
+ end
78
+ end
79
+
80
+ class Entry
81
+ attr_accessor :title, :link, :description, :charset
82
+
83
+ def initialize(item, charset)
84
+ @charset = charset
85
+ @link = read_link item
86
+ @title = read_title item
87
+ @description = read_description item
88
+ end
89
+
90
+ # Return the rss item link
91
+ def read_link(item)
92
+ post_url = nil
93
+ if link = item.search("link:first")
94
+ post_url = link.text
95
+ post_url = link.to_s.scan(/href=['"]?([^'"]*)['" ]/).to_s if (post_url.nil? or post_url.empty?)
96
+ end
97
+ return post_url
98
+ end
99
+
100
+ def read_title(item)
101
+ return TextyHelper::convertEncoding((item/:title).text, @charset).downcase
102
+ end
103
+
104
+ def read_description(item)
105
+ description = (item/"description|summary|content|[@type='text']").text
106
+ if description.include? "&lt;"
107
+ description = HTMLEntities.decode_entities(description)
108
+ else
109
+ description = HTMLEntities.encode_entities(description, :named, :decimal) if @charset == 'utf-8'
110
+ end
111
+ description = TextyHelper::clean(TextyHelper::convertEncoding(description, @charset), 200) unless description.empty?
112
+ description.gsub!(/((https?):\/\/([^\/]+)\/(.*))/, '[<a href=\'\1\'>link</a>]') unless description.empty?
113
+ return description.strip
114
+ end
115
+
116
+ def to_s
117
+ "Entry: title: #{title} - link: #{link}\n\rdescription: #{description}"
118
+ end
119
+ end
120
+
121
+ class Entry_Flickr<Entry
122
+ def read_description(item)
123
+ image = item.search("media:thumbnail").to_s.scan(/url=['"]?([^'"]*)['" ]/).to_s
124
+ image = item.search("content|description").text.scan(/(http:\/\/farm.*_.\.jpg)/).to_s if image.nil? or image.empty?
125
+ image.gsub!(/_.\.jpg/,"_t.jpg")
126
+ return "<a href='#{@link}' class='image_link'><img src='#{image}' class='flickr_image'/></a><br/>"
127
+ end
128
+ end
129
+
130
+ class Entry_Fotolog<Entry
131
+ def read_description(item)
132
+ image = item.search("media:thumbnail").to_s.scan(/url=['"]?([^'"]*)['" ]/).to_s
133
+ return "<a href='#{@link}' class='image_link'><img src='#{image}' class='post_image'/></a>"
134
+ end
135
+ end
136
+
137
+ class Entry_Google_Video<Entry
138
+ def read_description(item)
139
+ image = item.search("media:thumbnail").to_s.scan(/url=['"]?([^'"]*)['" ]/).to_s.gsub(/&amp;/, '&')
140
+ return "<a href='#{@link}' class='image_link'><img src='#{image}' class='google_video_image' width='160px' height='160px'/></a><br/>"
141
+ end
142
+ end
143
+
144
+ class Entry_Jumpcut<Entry
145
+ def read_description(item)
146
+ image = item.search("description").to_s.scan(/src=['"]?([^'"]*)['" ]/).to_s
147
+ return "<a href='#{@link}' class='image_link'><img src='#{image}' class='jumpcut_image' width='160px' height='120px'/></a><br/>"
148
+ end
149
+ end
150
+
151
+ class Entry_Picasa<Entry
152
+ def read_description(item)
153
+ image = item.search("media:thumbnail").to_s.scan(/url=['"]?([^'"]*)['" ]/).to_s
154
+ return "<a href='#{@link}' class='image_link'><img src='#{image}' class='picasa_image'/></a>"
155
+ end
156
+ end
157
+
158
+ def read(uri, nb_posts=10)
159
+
160
+ link = Rfeedfinder::feed(uri)
161
+ doc = open_doc(link)
162
+
163
+ unless doc.nil?
164
+ feed = Feed.new(link, doc)
165
+ entries = feed.parse_entries(doc, nb_posts)
166
+ end
167
+
168
+ return feed
169
+ end
170
+
171
+ def read_first(uri)
172
+ return read(uri, nb_post=1)
173
+ end
174
+
175
+ def open_doc(link)
176
+ begin
177
+ data = Hpricot(open(link,
178
+ "User-Agent" => "Ruby/#{RUBY_VERSION} - Rfeedreader",
179
+ "From" => "rfeedreader@googlegroups.com",
180
+ "Referer" => "http://rfeedreader.rubyforge.org/"), :xml => true)
181
+ rescue Timeout::Error
182
+ return nil
183
+ rescue OpenURI::HTTPError
184
+ html = Net::HTTP.get(URI.parse(link))
185
+ data = Hpricot(html, :xml => true)
186
+ rescue => err
187
+ puts "Error while opening #{link} with Hpricot: #{err.class} " << $!
188
+ return nil
189
+ end
190
+ return data
191
+ end
192
+ end
193
+
194
+ require 'rfeedreader/version'
@@ -0,0 +1,9 @@
1
+ module Rfeedreader #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/scripts/txt2html ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require File.dirname(__FILE__) + '/../lib/rfeedreader/version.rb'
8
+
9
+ version = Rfeedreader::VERSION::STRING
10
+ download = 'http://rubyforge.org/projects/rfeedreader'
11
+
12
+ class Fixnum
13
+ def ordinal
14
+ # teens
15
+ return 'th' if (10..19).include?(self % 100)
16
+ # others
17
+ case self % 10
18
+ when 1: return 'st'
19
+ when 2: return 'nd'
20
+ when 3: return 'rd'
21
+ else return 'th'
22
+ end
23
+ end
24
+ end
25
+
26
+ class Time
27
+ def pretty
28
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29
+ end
30
+ end
31
+
32
+ def convert_syntax(syntax, source)
33
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34
+ end
35
+
36
+ if ARGV.length >= 1
37
+ src, template = ARGV
38
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39
+
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ syntax_items = []
53
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
54
+ ident = syntax_items.length
55
+ element, syntax, source = $1, $2, $3
56
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
57
+ "syntax-temp-#{ident}"
58
+ }
59
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60
+ body = RedCloth.new(body_text).to_html
61
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62
+ end
63
+ stat = File.stat(src)
64
+ created = stat.ctime
65
+ modified = stat.mtime
66
+
67
+ $stdout << template.result(binding)