×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Ruby
Posted by: Joshua Binder
Added: Mar 19, 2013 2:03 PM
Views: 25
Tags: no tags
Points to keep in mind
- When using the ActiveResource::CustomMethods (get/post) make sure you are using "find_custom" and not "find"
- Disable the logging of ActiveResource, since the following line leads to exception if logger is enabled.


## line 111, connection.rb
logger.info "--> #{result.code} #{result.message} (#{result.body ? result.body : 0}b %.2fs)" % time if logger
  1.  
  2.  
  3. #### Create classes for YouTube resources.
  4. class Video < ActiveYouTube
  5.   self.site = "http://gdata.youtube.com/feeds/api"
  6.  
  7.   ## To search by categories and tags
  8.   def self.search_by_tags (*options)
  9.     from_urls = []
  10.     if options.last.is_a? Hash
  11.       excludes = options.slice!(options.length-1)
  12.       if excludes[:exclude].kind_of? Array
  13.         from_urls << excludes[:exclude].map{|keyword| "-"+keyword}.join("/")
  14.       else
  15.         from_urls << "-"+excludes[:exclude]
  16.       end
  17.     end
  18.     from_urls << options.find_all{|keyword| keyword =~ /^[a-z]/}.join("/")
  19.     from_urls << options.find_all{|category| category =~ /^[A-Z]/}.join("%7C")
  20.     from_urls.delete_if {|x| x.empty?}
  21.     self.find(:all,:from=>"/feeds/api/videos/-/"+from_urls.reverse.join("/"))
  22.   end
  23. end
  24.  
  25. class User < ActiveYouTube
  26.   self.site = "http://gdata.youtube.com/feeds/api"
  27. end
  28.  
  29. class Standardfeed < ActiveYouTube
  30.   self.site = "http://gdata.youtube.com/feeds/api"
  31. end
  32.  
  33. class Playlist < ActiveYouTube
  34.   self.site = "http://gdata.youtube.com/feeds/api"
  35. end
  36.  
  37.  
  38.  
  39. ##### Examples #######
  40.  
  41. #### VIDEO
  42.   ## search for videos
  43.   search = Video.find(:first, :params => {:vq => 'ruby', :"max-results" => '5'})
  44.   puts search.entry.length
  45.  
  46.   ## video information of id = ZTUVgYoeN_o
  47.   vid = Video.find("ZTUVgYoeN_o")
  48.   puts vid.group.content[0].url
  49.  
  50.   ## video comments
  51.   comments = Video.find_custom("ZTUVgYoeN_o").get(:comments)
  52.   puts comments.entry[0].link[2].href
  53.  
  54.   ## searching with category/tags
  55.   results = Video.search_by_tags("Comedy")
  56.   puts results[0].entry[0].title
  57.   # more examples:
  58.   # Video.search_by_tags("Comedy", "dog")
  59.   # Video.search_by_tags("News","Sports","football", :exclude=>["Comedy","soccer"])
  60.   # Video.search_by_tags("News","Sports","football", :exclude=>"soccer")
  61.  
  62.  
  63. #### STANDARDFEED
  64.   ## retrieving standard feeds
  65.   most_viewed = Standardfeed.find(:most_viewed, :params => {:time => 'today'})
  66.   puts most_viewed.entry[0].group.content[0].url
  67.  
  68. #### USER
  69.   ## user's profile - guthrie
  70.   user_profile = User.find("guthrie")
  71.   puts user_profile.link[1].href
  72.  
  73.   ## user's playlist - john
  74.   user_playlist = User.find_custom("john").get(:playlists)
  75.   puts user_playlist.link[1].href
  76.  
  77.   ## user's upload or favorites
  78.   rick_video = User.find_custom("rick").get(:uploads)
  79.   puts rick_video.entry[0].group.content[0].url
  80.  
  81.   ## user's subscription
  82.   user_subscriptions = User.find_custom("guthrie").get(:subscriptions)
  83.   puts user_subscriptions.to_yaml
  84.  
  85. #### PLAYLIST
  86.   ## get playlist - multiple elements in playlist
  87.   playlist = Playlist.find("EBF5D6DC4589D7B7")
  88.   puts playlist.entry[0].group.content[0].url
  89.  
  90.   ## get playlist - single element in playlist
  91.   playlist = Playlist.find("45C563323B344971")
  92.   puts playlist.entry[0].group.content[0].url
  93.