Yourub is a gem for searching YouTube videos using the YouTube Data API v3.
Add this line to your application's Gemfile:
gem 'yourub'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yourub
Get a developer key as explained here After that, you have 2 ways to use the client. If you want to use it in your rails app, create a app/config/yourub.yml file as follow:
yourub_defaults: &yourub_defaults
developer_key: 'YoUrDevEl0PerKey'
application_name: "yourub"
application_version: "0.2"
log_level: WARN
development:
<<: *yourub_defaults
production:
<<: *yourub_defaults
test:
<<: *yourub_defaults
If you want to use it from the console, or in another program, you can simply pass an hash containing the needed options. Change 'yourub' with the name of your application.
options = { developer_key: 'mySecretKey',
application_name: 'yourub',
application_version: 2.0,
log_level: 3 }
client = Yourub::Client.new(options)search always requires a non-empty :query. Optional filters narrow results.
Search by text only:
client.search(query: "aliens") do |v|
puts v
endRecent sports-related videos in Germany (category is matched by name, see below):
client.search(query: "highlights", country: "DE", category: "sports", order: "date") do |v|
puts v
end:query — Required. Non-empty string (after stripping whitespace).
:country — Optional. One or more ISO 3166-1 alpha-2 codes, e.g. "US" or "IT,DE". Codes are validated against Yourub::CountryCodes::ISO_3166_1_ALPHA2 (also exposed as Yourub::Validator::COUNTRIES / client.countries).
:category — Optional. If the name of a category is set, Yourub resolves a category_id from the name: it calls list_video_categories, (cached on the client), finds the first item whose title contains your string (case-insensitive). If the name does not match any category, ArgumentError lists id: title lines of the available categories.
:count_filter — Optional hash, e.g. { views: ">= 100" } or { views: "== 600" } (applied after fetching full video resources).
:max_results — Optional integer from 1 to 50.
:order — Optional. One of: date, rating, relevance, title, videoCount, viewCount. Default relevance.
search— Runs a YouTube search and yields each video hash that passes optional filters. Requires:queryand typically a block.
client.search(query: "space missions") { |v| puts v["id"] }get— Fetches one video’s metadata (snippet+statistics).
client.get("G2b0OIkTraI")-
list_video_categoriesCalls YouTube
videoCategories.listusing onlyregionCode(viaregion_code:). The API does not use category IDs in this helper; you pass an ISO 3166-1 alpha-2 region when you want that country’s category list.region_code: Optional. If omitted or blank, the request is sent withoutregionCode, which matches the documented default catalog (United States).hl: Optional. Sets thehlquery parameter so snippet titles in the response use the requested locale (e.g. Spanish labels for Spain).Caching: Responses are cached per client instance, keyed by
region_codeandhl, because category metadata changes rarely. Repeat calls with the same arguments return the same in-memory result without another HTTP request.Return value: A
Google::Apis::YoutubeV3::VideoCategoryListResponse(fromgoogle-apis-youtube_v3). Use#itemsto iterate categories (each has#idand#snippet).
client = Yourub::Client.new(options)
# Default catalog (documented as US when region is not specified)
us_like = client.list_video_categories
# Explicit region
de_categories = client.list_video_categories(region_code: "DE")
# Region + localized labels
es_labels = client.list_video_categories(region_code: "ES", hl: "es")-
flush_video_categories_cache!Clears all cached
list_video_categoriesresults for this client. Call this if you need to force a fresh fetch (e.g. after a long-lived process or for tests).
client.flush_video_categories_cache!