How to access Youtube_it ruby query results? - ruby

I am trying to implement the youtube_it youtube api wrapper for ruby and have it working except I'm stumped as to how the query results should be accessed.
Here is my query:
client.videos_by(:query => "penguin", :max_results => 1)
Submitting request [url=http://gdata.youtube.com/feeds/api/videos?max-results=1&start-index=1&vq=penguin].
=> #<YouTubeIt::Response::VideoSearch:0xb6c41b14 #feed_id="http://gdata.youtube.com/feeds/api/videos", #updated_at=Wed Nov 03 18:01:39 UTC 2010, #videos=[#<YouTubeIt::Model::Video:0xb6c424d8 #thumbnails=[#<YouTubeIt::Model::Thumbnail:0xb6c6b694 #url="http://i.ytimg.com/vi/oSbLpQEZP1Y/2.jpg", #width=120, #height=90, #time="00:01:34">, #<YouTubeIt::Model::Thumbnail:0xb6c6b248 #url="http://i.ytimg.com/vi/oSbLpQEZP1Y/1.jpg", #width=120, #height=90, #time="00:00:47">, #<YouTubeIt::Model::Thumbnail:0xb6c6a988 #url="http://i.ytimg.com/vi/oSbLpQEZP1Y/3.jpg", #width=120, #height=90, #time="00:02:21">, #<YouTubeIt::Model::Thumbnail:0xb6c69e34 #url="http://i.ytimg.com/vi/oSbLpQEZP1Y/0.jpg", #width=320, #height=240, #time="00:01:34">], #categories=[#<YouTubeIt::Model::Category:0xb6ca5d6c #term="Music", #label="Music">], #noembed=false, #racy=false, #favorite_count=7862, #duration=188, #author=#<YouTubeIt::Model::Author:0xb6c9942c #name="wili", #uri="http://gdata.youtube.com/feeds/api/users/wili">, #updated_at=Tue Nov 02 08:45:25 UTC 2010, #longitude=nil, #position=nil, #view_count=1682350, #html_content="penguin", #media_content=[#<YouTubeIt::Model::Content:0xb6c770d4 #url="http://www.youtube.com/v/oSbLpQEZP1Y?f=videos&app=youtube_gdata", #duration=188, #format=#<YouTubeIt::Model::Video::Format:0xb656d108 #name=:swf, #format_code=5>, #default=true, #mime_type="application/x-shockwave-flash">, #<YouTubeIt::Model::Content:0xb6c766d4 #url="rtsp://v5.cache3.c.youtube.com/CiILENy73wIaGQlWPxkBpcsmoRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp", #duration=188, #format=#<YouTubeIt::Model::Video::Format:0xb656d11c #name=:rtsp, #format_code=1>, #default=false, #mime_type="video/3gpp">, #<YouTubeIt::Model::Content:0xb6c75d38 #url="rtsp://v8.cache3.c.youtube.com/CiILENy73wIaGQlWPxkBpcsmoRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp", #duration=188, #format=#<YouTubeIt::Model::Video::Format:0xb656d0f4 #name=:three_gpp, #format_code=6>, #default=false, #mime_type="video/3gpp">], #description="penguin", #latitude=nil, #title="penguin", #published_at=Mon May 08 18:11:01 UTC 2006, #player_url="http://www.youtube.com/watch?v=oSbLpQEZP1Y&feature=youtube_gdata_player", #rating=#<YouTubeIt::Model::Rating:0xb6c5eb4c #min=1, #max=5, #average=4.676985, #rater_count=2746>, #keywords=["pigloo", "penguin"], #video_id="http://gdata.youtube.com/feeds/api/videos/oSbLpQEZP1Y", #where=nil>], #total_result_count=291282, #offset=1, #max_result_count=1>
I would like to retrieve the URL and thumbnail links. Any ideas?

I don't have a great deal of knowledge of this particular gem, but your answer should at least be close to this. You can access the object directly through the videos accessor, which will give you the video object, on which thumbnails each have a url. so you could do the following:
reply = client.videos_by(:query => "penguin", :max_results => 1)
reply.videos.first.thumbnails.first.url # the thumbnail for the first video
reply.videos.first.player_url # The website for the video
reply.videos.first.media_content.first.url # direct embed url
It might be useful to search for some ruby beginners guides to help catch you up to speed as well. Good luck!

william's answer is correct, when you do it
client.videos_by(:query => "penguin", :max_results => 1)
this return an array called videos, so you just need iterate it
client.videos.each do |video|
video.title
video.thumbnails
video.video_id
end
good luck!

Related

Google calendar API issue - daylight saving

I made an application using the Google calendar API, to evaluate events and finally also adds an event to a calendar. Now we come close to the switch to daylight saving time I have the problem: the API is shifting time for one hour.
Example:
$event = new Google_Service_Calendar_Event(array(
'summary' => 'title',
'description' => 'description',
'start' => array(
'dateTime' => '2018-04-05T12:00:00+1:00',
'timeZone' => 'Europe/Amsterdam',
),
'end' => array(
'dateTime' => '2018-04-05T15:00:00+1:00',
'timeZone' => 'Europe/Amsterdam',
)
));
$event = $cal->events->insert($room_calendar_id, $event);
When I execute this code the result is an event that runs from 13:00 till 16:00 hrs. When I use these parameters via the Google calendar API try-out function it gives the same result. But in the latter case it comes back with following JSON response:
"start": {
"dateTime": "2018-04-20T13:00:00+02:00",
"timeZone": "Europe/Amsterdam"
},
"end": {
"dateTime": "2018-04-20T16:00:00+02:00",
"timeZone": "Europe/Amsterdam"
}
So weird it shows an offset of +02:00.
I would expect the API does not change the time and offset parameters.
When I do the same for 2018-03-20 it works fine, so daylight saving should cause this issue in my opinion.
Can someone give more information on how I should add an event without considering the daylight saving influence?
Timezones - such as Europe/Amsterdam - have Daylight Saving effects, and you can't help avoiding them, because DST is part of the things controled by timezone definitions.
And during DST, the offset used in Amsterdam is changed from +01:00 to +02:00. So this is not weird, it's actually the expected behaviour.
What google API is doing - probably - is adjusting an invalid offset (2018-04-05T12:00:00+01:00) to a valid one (2018-04-05T13:00:00+02:00) - btw, both correspond to the same UTC instant, so the conversion is fine.
This doesn't happen in March 20th because DST haven't started yet - in 2018, it starts on March 25th.
If you're dealing with timezones, there's no way to ignore DST effects, because DST is part of the timezone definition.

Specify query parameter for a single HTTP method

To illustrate my problem, I made a condensed example from the Apiary.io blueprint tutorial.
FORMAT: 1A
# Gist Fox API
# Group Gist
Gist-related resources of *Gist Fox API*.
## Gists Collection [/gists{?since}]
### List All Gists [GET]
+ Parameters
+ since (optional, string) ... Timestamp in ISO 8601 format: `YYYY-MM-DDTHH:MM:SSZ` Only gists updated at or after this time are returned.
+ Response 200
{
items: []
}
### Create a Gist [POST]
To create a new Gist simply provide a JSON hash of the *description* and *content* attributes for the new Gist.
+ Request (application/json)
{
"description": "Description of Gist",
"content": "String content"
}
+ Response 201
{
}
Then in my apiary documentation I get the following:
GET /gists{?since}
POST /gists{?since}
However, for me it makes sense to have the since query parameter only for the GET request. Unfortunately I didn't find a way to achieve this result:
GET /gists{?since}
POST /gists
Is it something possible?
Update
(Thursday, 23 Oct 2014)
The fix has been deployed; could you please give it a try and let me know if everything works as expected?
The bad news
It is our (Apiary) bug and you're not doing anything wrong :-(
The good news
It is a known bug we are currently working on and it is going to be fixed with the end of this week (Sunday, 19 Oct 2014) :-)

Open URI - Invalid URI Error, encoding/escaping not affecting

I'm building out a YahooFinance Api and keep hitting a brick wall when trying to use open URI.
Code:
uri = ("http://ichart.finance.yahoo.com/table.csv?s=#{URI.escape(code)}&a=#{start_month}&b=#{start_day}&c=#{start_year}&d=#{end_month}&e=#{end_day}&f=#{end_year}&g=d&ignore=.csv")
puts "#{uri}"
conn = open(uri)
Error:
`split': bad URI(is not URI?): http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv} (URI::InvalidURIError)
I have tried URI.unescape(code) which outputs code as ^IXIC, as well as leaving any URI methods out and code will come through as %5EIXIC.
After reading around on stack overflow, I've tried both of these methods to no avail:
uri = URI.parse(URI.encode(url.strip))
safeurl = URI.encode(url.strip)
Even after looking through the code for another ruby yahoo-finance gem, here, I can't seem to find a solution. Any help is greatly appreciated. Thanks
EDIT: I am able to use open(uri) when I manually enter in the url in single quotes. Do double quotes, (used for inserting ruby objects), play a role here?
Don't try to inject variables into URLs. If they contain characters that need to be encoded per the spec, they won't be by interpolation. Instead, take advantage of the right tools for the job, like Ruby's URI class or the Addressable::URI gem.
See "How to post a URL containting curly braces and colons" for how to do this using well tested wheels.
In your situation, something like this will work:
require 'uri'
code = 'qwer3456*&^%'
start_month = 1
start_day = 1
start_year = 2014
end_month = 12
end_day = 31
end_year = 2015
uri = URI.parse("http://ichart.finance.yahoo.com/table.csv")
uri.query = URI.encode_www_form(
{
'g' => 'd',
'ignore' => '.csv',
's' => code,
'a' => start_month,
'b' => start_day,
'c' => start_year,
'd' => end_month,
'e' => end_day,
'f' => end_year
}
)
uri.to_s # => "http://ichart.finance.yahoo.com/table.csv?g=d&ignore=.csv&s=qwer3456*%26%5E%25&a=1&b=1&c=2014&d=12&e=31&f=2015"
The code works for me though I don't think the API endpoint is correct:
[1] pry(main)> uri = URI("http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv")
=> #<URI::HTTP:0x007fd63a2fff40 URL:http://ichart.finance.yahoo.com/table.csv?s=%255EIXIC&a=00&b=1&c=1994&d=09&e=14&f=2014&g=d&ignore=.csv>
[3] pry(main)> Net::HTTP.get(uri)
=> "<!doctype html public \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html><head><title>Yahoo! - 404 Not Found</title><style>\n/* nn4 hide */ \n/*/*/\nbody {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}\nhtml>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}\np {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}\n#ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}\nh1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}\nform {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}\n#s1p {width:15em;margin-right:.1em;}\nform span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;} \nform .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}\n/* end nn4 hide */\n</style></head>\n<body><div id=\"doc\">\n<div id=\"ygma\"><img\nsrc=http://l.yimg.com/a/i/yahoo.gif\nwidth=147 height=31 border=0 alt=\"Yahoo!\"><div><a\nhref=\"http://us.rd.yahoo.com/404/*http://www.yahoo.com\">Yahoo!</a>\n - Help</div></div>\n<div id=\"bd\"><h1>Sorry, the page you requested was not found.</h1>\n<p>Please check the URL for proper spelling and capitalization. If\nyou're having trouble locating a destination on Yahoo!, try visiting the\n<strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://www.yahoo.com\">Yahoo! home\npage</a></strong> or look through a list of <strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/\">Yahoo!'s\nonline services</a></strong>. Also, you may find what you're looking for\nif you try searching below.</p>\n<form name=\"s1\" action=\"http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search\"><fieldset>\n<legend><label for=\"s1p\">Search the Web</label></legend>\n<input type=\"text\" size=30 name=\"p\" id=\"s1p\" title=\"enter search terms here\">\n<input type=\"submit\" value=\"Search\">\n<span>advanced search <span class=sep>|</span> most popular</span>\n</fieldset></form>\n<p class=\"more\">Please try <strong><a\nhref=\"http://us.rd.yahoo.com/404/*http://help.yahoo.com\">Yahoo!\nHelp Central</a></strong> if you need more assistance.</p>\n</div><div id=\"ft\"><p>Copyright © 2014 Yahoo! Inc.\nAll rights reserved. <a\nhref=\"http://us.rd.yahoo.com/404/*http://privacy.yahoo.com\">Privacy\nPolicy</a> - <a\nhref=\"http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/\">Terms\nof Service</a></p></div>\n</div></body></html>\n"
Looks like your problem is the ignore=.csv part.
I mean this is probably trying to encode it as a domain extension. Probably you should remove the dot to solve the problem.

With Zabbix API, how do I get the values of items/resources rather than just the ID's?

I have some data in a Custom Screen in Zabbix, and would like to pull the data from the screen via the API. I'm using this Ruby gem: https://github.com/express42/zabbixapi
I'm able to successfully connect and query, but the results I'm getting are not very useful:
p zbx.query(
:method => "item.get",
:params => {
:itemids => "66666",
:output => "extend"
}
)
# [{"itemid"=>"66666", "type"=>"0", "snmp_community"=>"", "snmp_oid"=>"", "hostid"=>"77777", "name"=>"Fro Packages", "key_"=>"system.sw.packages[davekey1|davekey2|davekey3|davekey4]", "delay"=>"300", "history"=>"90", "trends"=>"365", "status"=>"0", "value_type"=>"1", "trapper_hosts"=>"", "units"=>"", "multiplier"=>"0", "delta"=>"0", "snmpv3_securityname"=>"", "snmpv3_securitylevel"=>"0", "snmpv3_authpassphrase"=>"", "snmpv3_privpassphrase"=>"", "formula"=>"1", "error"=>"", "lastlogsize"=>"0", "logtimefmt"=>"", "templateid"=>"88888", "valuemapid"=>"0", "delay_flex"=>"", "params"=>"", "ipmi_sensor"=>"", "data_type"=>"0", "authtype"=>"0", "username"=>"", "password"=>"", "publickey"=>"", "privatekey"=>"", "mtime"=>"0", "flags"=>"0", "filter"=>"", "interfaceid"=>"25", "port"=>"", "description"=>"", "inventory_link"=>"0", "lifetime"=>"30", "snmpv3_authprotocol"=>"0", "snmpv3_privprotocol"=>"0", "state"=>"0", "snmpv3_contextname"=>""}]
You can see that it's returning a bunch of ID's for the items, including the correct keys, but I can't seem to get the actual plain text values, which is the data I'm interested in.
I started with the screen_id, then got the screenitem_id, now the item_id, but I don't seem to be getting any closer to what I want!
Thanks for any help
Getting items or getting hosts means getting their description, not the data. is You are after history. Reading the actual Zabbix user manual and API docs is highly recommended.

Using Ruby & Github API to filter commits by date

I am using the ruby gem 'octokit' which implements the Github API v3. Mostly works great but I cannot seem to filter by date. I believe I have the syntax and time format correct, but it appears my option is ignored and the API returns the past 35 entries regardless of the since or until dates.
Here's a minimal reproducible example (after installing the octokit gem).
require 'octokit'
require 'time'
#day = "2012-09-27"
#until = DateTime.parse(#date).iso8601
#since = (DateTime.parse(#day) - 60*60*48).iso8601
a = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => #since, :until => #until})
see the date of the output of last entry
a.last.commit.author.date
explicit day doesn't work either
b = Octokit.commits({:username => "cboettig", :repo => "labnotebook", :since => "2012-09-27T00:00:00+00:00"})
b.last.commit.author.date
The date I get in both examples is from August, outside the specified range given. What did I miss?
Background: I'm trying to write a little Jekyll plugin that uses the API to return commits made to a specified repo on the day of the post.
joeyw gives a great answer to this question here.
The second argument should be the sha or branch, and options should be the third argument, e.g.
Octokit.commits("cboettig/labnotebook", "master", :since => "2012-09-28T00:00:00+00:00").length
or
Octokit.commits("cboettig/labnotebook", nil, :since => "2012-09-28T00:00:00+00:00").length
works just fine. Here's my corresponding jekyll plugin

Resources