ytifl 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +19 -0
  3. data/README.md +23 -0
  4. data/bin/ytifl +68 -0
  5. data/ytifl.gemspec +16 -0
  6. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cecab15f968f93ee440427157d4b80928fe0718
4
+ data.tar.gz: be0d34d0919b45236dcbba0b539cb08231420e33
5
+ SHA512:
6
+ metadata.gz: 522f85b7c088dca848cd6ecefbd82515a3fe697dde8f15d996daa4fa328d62b213571835dd0eedfb735e7879d572230053189e172c0569ff23ee9bd24d32a7ce
7
+ data.tar.gz: 262bf253971a67e7df829546a26ad020a4f2a84230aea0f39133a478ca0e4a128b2f9e35684b0ed470b797f9f0d2de46dfb42fb68f94f92226b031602ed1e19a
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015 Brandon Mulcahy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ YTIFL
2
+ =====
3
+ YouTube: I'm Feeling Lucky. Prints the URL of the first result of a YouTube
4
+ search for the given terms.
5
+
6
+ Installation
7
+ ------------
8
+ gem install ytifl
9
+
10
+ Usage
11
+ -----
12
+ Usage: ytifl [options] term ...
13
+
14
+ Print the URL of the first result of a YouTube search for the given terms.
15
+
16
+ Options:
17
+ -h, --help Print this message and exit
18
+ --version Print version information and exit
19
+
20
+ Example
21
+ -------
22
+ $ ytifl story from north america
23
+ https://www.youtube.com/watch?v=ms2klX-puUU
data/bin/ytifl ADDED
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cgi'
4
+ require 'json'
5
+ require 'net/http'
6
+ require 'optparse'
7
+
8
+ API_KEY = 'AIzaSyDR5xOXOViVLMUyWWJM1iQefTaRiKkJfqs'
9
+ VERSION = '1.0.0'
10
+
11
+ # print a message to standard error and exit with nonzero status
12
+ def die(msg)
13
+ warn msg
14
+ exit false
15
+ end
16
+
17
+ # parse command-line options
18
+ opts = OptionParser.new do |opts|
19
+ opts.banner += ' term ...'
20
+
21
+ opts.separator ""
22
+ opts.separator "Print the URL of the first result of a YouTube search for " \
23
+ "the given terms."
24
+ opts.separator ""
25
+ opts.separator "Options:"
26
+
27
+ opts.on('-h', '--help', 'Print this message and exit') do
28
+ puts opts
29
+ exit
30
+ end
31
+
32
+ opts.on('--version', 'Print version information and exit') do
33
+ puts "ytifl #{VERSION}"
34
+ exit
35
+ end
36
+ end
37
+
38
+ begin
39
+ opts.parse!
40
+ rescue OptionParser::ParseError
41
+ die(opts)
42
+ end
43
+
44
+ die(opts) if ARGV.empty?
45
+
46
+ # assemble query from command-line arguments
47
+ query = CGI.escape(ARGV.join(' '))
48
+ uri = URI('https://www.googleapis.com/youtube/v3/search?' \
49
+ "key=#{API_KEY}&part=id&maxResults=1&q=#{query}&type=video")
50
+
51
+ # make HTTP request and parse response; handle exceptions
52
+ begin
53
+ result = JSON.parse(Net::HTTP.get(uri))
54
+ rescue StandardError => err
55
+ die(err)
56
+ end
57
+
58
+ # handle error conditions
59
+ if result.has_key?('error')
60
+ error = result['error']['errors'][0]
61
+ die('Error making search API call: ' \
62
+ "#{error['message']}: #{error['domain']}: #{error['reason']}")
63
+ elsif result['items'].empty?
64
+ die("No results for query: #{query}")
65
+ end
66
+
67
+ # print result URL
68
+ puts "https://www.youtube.com/watch?v=#{result['items'][0]['id']['videoId']}"
data/ytifl.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'ytifl'
3
+ s.version = '1.0.0'
4
+ s.date = '2015-05-29'
5
+ s.summary = 'Print the URL of the first result of a YouTube search'
6
+ s.description = "YouTube: I'm Feeling Lucky. Prints the URL of the first " \
7
+ "result of a YouTube search for the given terms."
8
+ s.authors = ['Brandon Mulcahy']
9
+ s.email = 'brandon@jangler.info'
10
+ s.files = `git ls-files`.split
11
+ s.homepage = 'https://github.com/jangler/ytifl'
12
+ s.license = 'MIT'
13
+
14
+ s.executables << 'ytifl'
15
+ s.required_ruby_version = '>= 2.0.0'
16
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ytifl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Mulcahy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'YouTube: I''m Feeling Lucky. Prints the URL of the first result of a
14
+ YouTube search for the given terms.'
15
+ email: brandon@jangler.info
16
+ executables:
17
+ - ytifl
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - bin/ytifl
24
+ - ytifl.gemspec
25
+ homepage: https://github.com/jangler/ytifl
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.5
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Print the URL of the first result of a YouTube search
49
+ test_files: []