Skip to content

Instantly share code, notes, and snippets.

@brianburridge
Created May 21, 2012 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianburridge/2764323 to your computer and use it in GitHub Desktop.
Save brianburridge/2764323 to your computer and use it in GitHub Desktop.
def Tumblr.youtube_views(post)
begin
doc = Hpricot.parse(post)
video_count = 0
if doc.present?
embed_doc = doc.at("embed")
embed_doc = doc.at("iframe") if embed_doc.class != Hpricot::Elem
if !embed_doc.nil?
video_url = embed_doc.attributes["src"]
video_id = video_url.split("/")[4]
video_id = video_id.split("?")[0]
cache_name = "youtube_#{video_id}"
client = YouTubeIt::Client.new
dev_key = "dev_key_here"
client = YouTubeIt::Client.new(:dev_key => dev_key)
video_count = Rails.cache.fetch(cache_name, :expires_in => 1.hour) {
video_count = client.video_by(video_id).view_count
video_count.present? ? video_count.to_i : 0
}
end
end
rescue => e
Airbrake.notify(e)
video_count = Rails.cache.fetch(cache_name, :expires_in => 1.hour) {
0
}
rescue Timeout::Error => e
Airbrake.notify(e)
video_count = Rails.cache.fetch(cache_name, :expires_in => 1.hour) {
0
}
end
return video_count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment