Skip to content

Commit

Permalink
added twitter and flickr clients
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhnt committed May 15, 2009
1 parent cd3872a commit 64aab10
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.svn
6 changes: 0 additions & 6 deletions .svn/dir-props

This file was deleted.

23 changes: 0 additions & 23 deletions .svn/entries

This file was deleted.

1 change: 0 additions & 1 deletion .svn/format

This file was deleted.

4 changes: 3 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'open-uri'
require 'hpricot'
require File.join(File.dirname(__FILE__), 'lib', 'youtube_client')
require File.join(File.dirname(__FILE__), 'lib', 'youtube_client')
require File.join(File.dirname(__FILE__), 'lib', 'flickr_client')
require File.join(File.dirname(__FILE__), 'lib', 'twitter_client')
19 changes: 19 additions & 0 deletions lib/flickr/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Flickr

class Client

BASE_URL = "http://api.flickr.com/services/rest"
DEFAULT_PARAMS = { :api_key => "", :format => "rest" }
FEED_URLS = {
:list_photosets => "#{BASE_URL}?method=flickr.photosets.getList&%s"
}

def self.photosets(options = {})
options = DEFAULT_PARAMS.merge(options)
feed = open(format("#{FEED_URLS[:list_photosets]}", options.to_param)).read
Flickr::Parser.parse_photosets(feed, options[:user_id])
end

end

end
30 changes: 30 additions & 0 deletions lib/flickr/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Flickr

class Parser

def self.parse_photosets(feed, user_id)
res = []
doc = Hpricot(feed)
(doc/"photoset").each do |set|
res << parse_photoset(set, user_id)
end
return res
end

def self.parse_photoset(elem, user_id)
set = PhotoSet.new(user_id)
set.id = elem.attributes['id']
set.photos = elem.attributes['photos']
set.videos = elem.attributes['videos']
set.primary = elem.attributes['primary']
set.farm = elem.attributes['farm']
set.secret = elem.attributes['secret']
set.server = elem.attributes['server']
set.description = elem.at('description').inner_html
set.title = elem.at('title').inner_html
return set
end

end

end
29 changes: 29 additions & 0 deletions lib/flickr/photoset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Flickr

class PhotoSet
attr_accessor :id, :photos, :videos, :title, :description
attr_accessor :server, :farm, :secret, :primary, :user_id

def initialize(user_id)
self.user_id = user_id
end

def image_url(type = :small)
t = case type
when :thumbnail then "t"
when :small then "s"
when :medium then "m"
when :large then "b"
when :original then "o"
else "s"
end
return "http://farm#{farm}.static.flickr.com/#{server}/#{primary}_#{secret}_#{t}.jpg"
end

def url
"http://www.flickr.com/photos/#{user_id}/sets/#{id}"
end

end

end
3 changes: 3 additions & 0 deletions lib/flickr_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir.glob(File.join(File.dirname(__FILE__), 'flickr', '*.rb')).each do |file|
require file
end
19 changes: 19 additions & 0 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Twitter

class Client

BASE_URL = "https://twitter.com/"
DEFAULT_PARAMS = { :count => 9, :format => "xml" }
FEED_URLS = {
:user_timeline => "#{BASE_URL}statuses/user_timeline/%s.%s?%s"
}

def self.user_timeline(user_id, options = {})
options = DEFAULT_PARAMS.merge(options)
feed = open(format(FEED_URLS[:user_timeline], user_id, options.delete(:format), options.to_param)).read
Twitter::Parser.parse_statuses(feed)
end

end

end
27 changes: 27 additions & 0 deletions lib/twitter/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Twitter

class Parser

def self.parse_statuses(feed)
res = []
doc = Hpricot(feed)
(doc/"status").each do |entry|
res << parse_status(entry)
end
return res
end

def self.parse_status(entry)
tweet = Tweet.new
tweet.text = entry.at('text').inner_html
tweet.created_at = Time.zone.parse(entry.at('created_at').inner_html)
tweet.source = entry.at('source').inner_html
tweet.id = entry.at('id').inner_html
tweet.user_id = entry.at('user id').inner_html
tweet.user_image_url = entry.at('user profile_image_url').inner_html
return tweet
end

end

end
9 changes: 9 additions & 0 deletions lib/twitter/tweet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Twitter

class Tweet

attr_accessor :id, :text, :source, :created_at, :user_id, :user_image_url

end

end
3 changes: 3 additions & 0 deletions lib/twitter_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir.glob(File.join(File.dirname(__FILE__), 'twitter', '*.rb')).each do |file|
require file
end
19 changes: 19 additions & 0 deletions lib/youtube/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module YouTube

class Client

BASE_URL = "http://gdata.youtube.com/feeds/api/"
DEFAULT_PARAMS = { "v" => 2, "max-results" => 5, "format" => 5 }
FEED_URLS = {
:user_uploads => "#{BASE_URL}users/%s/uploads?%s"
}

def self.user_uploads(user, options = {})
options = DEFAULT_PARAMS.merge(options)
feed = open(format(FEED_URLS[:user_uploads], user, options.to_param)).read
YouTube::Parser.parse(feed)
end

end

end
33 changes: 33 additions & 0 deletions lib/youtube/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module YouTube

class Parser

def self.parse(feed)
res = []
doc = Hpricot(feed)
(doc/"entry").each do |entry|
res << parse_entry(entry)
end
return res
end

def self.parse_entry(entry)
video = Video.new
video.id = entry.at("media:group yt:videoid").inner_html
video.title = entry.at("title").inner_html
video.published = Time.zone.parse(entry.at("published").inner_html)
video.updated = Time.zone.parse(entry.at("updated").inner_html)
video.uploaded = Time.zone.parse(entry.at("media:group yt:uploaded").inner_html)
video.author = entry.at("author name").inner_html
video.description = entry.at("media:group media:description").inner_html
video.link = entry.at("media:group media:player").attributes['url']
video.thumbnail = entry.at("media:group media:thumbnail").attributes['url']
video.rating = entry.at("gd:rating").attributes
video.keywords = entry.at("media:group media:keywords").inner_html.split(',')
video.duration = entry.at("media:group yt:duration").attributes['seconds'].to_i
return video
end

end

end
23 changes: 23 additions & 0 deletions lib/youtube/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module YouTube

class Video
attr_accessor :id, :title, :author, :description, :link, :thumbnail
attr_accessor :published, :updated, :uploaded, :rating, :keywords, :duration

def embed(options = {})
options[:width] ||= 480
options[:height] ||= 295
options[:hl] ||= 'de'
return <<-HTML
<object width="#{options[:width]}" height="#{options[:height]}">
<param name="movie" value="http://www.youtube.com/v/#{id}&hl=#{options[:hl]}&fs=1&rel=0"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/#{id}&hl=#{options[:hl]}&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="#{options[:width]}" height="#{options[:height]}">
</embed>
</object>
HTML
end
end

end
77 changes: 2 additions & 75 deletions lib/youtube_client.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,3 @@
module YouTube

class Client

OPEN_URI_OPTIONS = {
:proxy => nil
}

FEED_OPTIONS = {
"v" => 2,
"max-results" => 5,
"format" => 5
}

FEED_URLS = {
:user_uploads => "http://gdata.youtube.com/feeds/api/users/%s/uploads?%s"
}

def self.user_uploads(user, options = {})
options = FEED_OPTIONS.merge(options)
feed = open(format(FEED_URLS[:user_uploads], user, options.to_param), OPEN_URI_OPTIONS).read
YouTube::Parser.parse(feed)
end

end

class Video
attr_accessor :id, :title, :author, :description, :link, :thumbnail
attr_accessor :published, :updated, :uploaded, :rating, :keywords, :duration

def embed(options = {})
options[:width] ||= 480
options[:height] ||= 295
options[:hl] ||= 'de'
return <<-HTML
<object width="#{options[:width]}" height="#{options[:height]}">
<param name="movie" value="http://www.youtube.com/v/#{id}&hl=#{options[:hl]}&fs=1&rel=0"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/#{id}&hl=#{options[:hl]}&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="#{options[:width]}" height="#{options[:height]}">
</embed>
</object>
HTML
end
end

class Parser

def self.parse(feed)
res = []
doc = Hpricot(feed)
(doc/"entry").each do |entry|
res << parse_entry(entry)
end
return res
end

def self.parse_entry(entry)
video = Video.new
video.id = entry.at("media:group yt:videoid").inner_html
video.title = entry.at("title").inner_html
video.published = DateTime.strptime(entry.at("published").inner_html, "%Y-%m-%dT%H:%M:%S.000Z")
video.updated = DateTime.strptime(entry.at("updated").inner_html, "%Y-%m-%dT%H:%M:%S.000Z")
video.uploaded = DateTime.strptime(entry.at("media:group yt:uploaded").inner_html, "%Y-%m-%dT%H:%M:%S.000Z")
video.author = entry.at("author name").inner_html
video.description = entry.at("media:group media:description").inner_html
video.link = entry.at("media:group media:player").attributes['url']
video.thumbnail = entry.at("media:group media:thumbnail").attributes['url']
video.rating = entry.at("gd:rating").attributes
video.keywords = entry.at("media:group media:keywords").inner_html.split(',')
video.duration = entry.at("media:group yt:duration").attributes['seconds'].to_i
return video
end

end
Dir.glob(File.join(File.dirname(__FILE__), 'youtube', '*.rb')).each do |file|
require file
end
39 changes: 0 additions & 39 deletions test/.svn/entries

This file was deleted.

Loading

0 comments on commit 64aab10

Please sign in to comment.