Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Feedzirra parsers
module Feedzirra
module Parser
class FlickrEntry
include SAXMachine
include FeedEntryUtilities
element :guid, :as => :id
element :link, :as => :url
element :title
element :"media:title", :as => :title
element :link, :as => :url
element :pubDate, :as => :published
element :pubdate, :as => :published
element :"media:description", :as => :summary
element :"media:credit", :as => :author, :with => { :role => "photographer" }
element :"media:thumbnail", :as => :thumbnail, :value => :url
element :"media:content", :as => :content, :value => :url
element :"media:category", :as => :categories, :with => { :scheme => "urn:flickr:tags" }
def tags
@categories.blank? ? [] : @categories.split(" ")
end
end
end
end
module Feedzirra
module Parser
class Flickr
include SAXMachine
include FeedUtilities
element :title
element :pubDate, :as => :published
element :pubdate, :as => :published
element :lastBuildDate, :as => :updated
elements :item, :as => :entries, :class => FlickrEntry
attr_accessor :feed_url
def self.able_to_parse?(xml) #:nodoc:
xml =~ /xmlns:flickr=\"urn:flickr:\"/im
end
end
end
end
module Feedzirra
module Parser
class VimeoEntry
include SAXMachine
include FeedEntryUtilities
element :guid, :as => :id
element :title
element :"media:title", :as => :title
element :link, :as => :url
element :"dc:creator", :as => :author
element :"media:credit", :as => :author, :with => { :role => "author" }
element :description, :as => :summary
element :pubDate, :as => :published
element :pubdate, :as => :published
element :"media:player", :as => :content, :value => :url
element :enclosure, :as => :content, :value => :url, :with => { :type => "application/x-shockwave-flash" }
element :"media:category", :as => :keyword_list
element :"media:thumbnail", :as => :thumbnail, :value => :url
def embed_code
"<object width=\"400\" height=\"225\"><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"#{@content}&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"#{@content}&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"400\" height=\"225\"></embed></object>"
end
def media_id
if @url =~ /http:\/\/vimeo.com\/(\d*)/i
$1
elsif @content =~ /http:\/\/vimeo.com\/moogaloop.swf?clip_id=(\d*)/i
$1
else
''
end
end
def tags
@keyword_list.blank? ? [] : @keyword_list.split(",")
end
end
end
end
module Feedzirra
module Parser
class Vimeo
include SAXMachine
include FeedUtilities
element :title
element :link, :as => :url
element :"atom:link", :as => :feed_url, :value => :href, :with => { :rel => "self" }
elements :item, :as => :entries, :class => VimeoEntry
def self.able_to_parse?(xml) #:nodoc:
xml =~ /xmlns:atom=\"http:\/\/www.w3.org\/2005\/Atom\" xmlns:dc=\"http:\/\/purl.org\/dc\/elements\/1.1\/\" xmlns:media=\"http:\/\/search.yahoo.com\/mrss\/\"/im
end
end
end
end
module Feedzirra
module Parser
class YoutubeEntry
include SAXMachine
include FeedEntryUtilities
element :id
element :title
element :content, :as => :summary
element :name, :as => :author
element :link, :as => :url, :value => :href, :with => { :rel => "alternate" }
element :published
element :updated
element :"media:player", :as => :url, :value => :url
element :"media:content", :as => :content, :value => :url, :with => { :type => "application/x-shockwave-flash" }
element :"media:keywords", :as => :keyword_list
element :"yt:duration", :as => :duration, :value => :seconds
elements :category, :as => :keywords, :value => :term, :with => { :scheme => "http://gdata.youtube.com/schemas/2007/keywords.cat" }
elements :category, :as => :categories, :value => :term, :with => { :scheme => "http://gdata.youtube.com/schemas/2007/categories.cat" }
elements :"media:thumbnail", :as => :thumbnails, :value => :url
def embed_code
"<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"#{@content}\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"#{@content}\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>"
end
def thumbnail
@thumbnails.reject { |t| t !~ /0.jpg/ }.to_s
end
def media_id
if @id =~ /http:\/\/gdata.youtube.com\/feeds\/api\/videos\/(\S[^\/?]*)/i
$1
elsif @url =~ /http:\/\/www.youtube.com\/watch\?v\=(\S[^(&amp\;)]*)/i
$1
else
''
end
end
def tags
@keywords || @keyword_list.blank? ? [] : @keyword_list.split(",")
end
end
end
end
module Feedzirra
module Parser
class Youtube
include SAXMachine
include FeedUtilities
element :id
element :updated
element :title
element :name, :as => :author
element :link, :as => :feed_url, :value => :href, :with => { :rel => "http://schemas.google.com/g/2005#feed" }
element :link, :as => :url, :value => :href, :with => { :rel => "alternate" }
elements :entry, :as => :entries, :class => YoutubeEntry
def self.able_to_parse?(xml) #:nodoc:
xml =~ /xmlns:yt=\'http:\/\/gdata.youtube.com\/schemas\/2007\'/im
end
end
end
end
@activerain

This comment has been minimized.

Copy link

activerain commented Jun 14, 2011

How do you install and use these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.