lita-youtube-me 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b452c59b9836a8899cbf89e470dd184f5782658
4
- data.tar.gz: c2b7a1c05505d331542fdc0f3fbbde9886232b74
3
+ metadata.gz: 84e50b2bf564204b460ec3da6d9b267aab005701
4
+ data.tar.gz: 816c091089bbf1ad1eb6c816003b6f28894cde66
5
5
  SHA512:
6
- metadata.gz: 26e367d1fe3800f44cf5b43def7b1ce396ad52ddbad3ca2b4de514790cd16557c5ae6736889959f3db9ec69e76b17e4458ab4509da8d1a94c32c4a152ef001b7
7
- data.tar.gz: 3690cbfb65f64948e4b35f9b164cbe6dfb97f471ce8cc8c8e2302e5f9e52afbcb4f8e9b248fd7f6d7c60787136475c898f710c42b216826126871dfa0b8bd4f2
6
+ metadata.gz: 9c5b96f5cc3a162f7a02fdeb76260ca56d98f4ed44ebbdb5d1f64a9c687a2cd58599ece5c8d05fe3558826fc24a86bfca6852ff6e22f8b5328c175183ffbba6b
7
+ data.tar.gz: c9d9a3686daafd6057a7c6eba8adda27e5b3fc6bec140838ea8ac1aaf24835d0495071d94dfe4c2e17e74eee518373c8d966a9f5d9425078aa04f62a07aee5ec
data/README.md CHANGED
@@ -12,11 +12,18 @@ gem "lita-youtube-me"
12
12
 
13
13
  ## Configuration
14
14
 
15
+ This plugin requires an API key for the [YouTube Data API (v3)](https://developers.google.com/youtube/v3/).
16
+
17
+ ``` ruby
18
+ Lita.configure do |config|
19
+ config.handlers.youtube_me.api_key = "FooBarBazLuhrmann"
20
+ end
21
+ ```
22
+
15
23
  ### Optional attributes
16
24
  * `video_info` (boolean) - When set to `true`, Lita will return additional information (title, duration, etc.) about the video. Default: `false`
17
25
  * `detect_urls` (boolean) - When set to `true`, Lita will return additional information about any YouTube URLs it detects. Default: `false`
18
26
 
19
- ### Example
20
27
  ``` ruby
21
28
  Lita.configure do |config|
22
29
  config.handlers.youtube_me.video_info = true
@@ -1,15 +1,17 @@
1
1
  require "date"
2
+ require "iso8601"
2
3
 
3
4
  module Lita
4
5
  module Handlers
5
6
  class YoutubeMe < Handler
6
- API_URL = "https://gdata.youtube.com/feeds/api/videos"
7
+ API_URL = "https://www.googleapis.com/youtube/v3"
7
8
 
9
+ config :api_key, type: String, required: true
8
10
  config :video_info, types: [TrueClass, FalseClass], default: false
9
11
  config :detect_urls, types: [TrueClass, FalseClass], default: false
10
12
 
11
13
  route(/^(?:youtube|yt)(?: me)?\s+(.*)/i, :find_video, command: true, help: {
12
- "youtube (me) QUERY" => "Gets a youtube video."
14
+ "youtube (me) QUERY" => "Gets a YouTube video."
13
15
  })
14
16
  # Detect YouTube links in non-commands and display video info
15
17
  route(/\byoutube\.com\/watch\?v=([^?&#\s]+)/i, :display_info, command: false)
@@ -17,40 +19,52 @@ module Lita
17
19
 
18
20
  def find_video(response)
19
21
  query = response.matches[0][0]
20
- http_response = http.get(API_URL,
22
+ http_response = http.get(
23
+ "#{API_URL}/search",
21
24
  q: query,
22
- orderBy: "relevance",
23
- "max-results" => 15,
24
- alt: "json"
25
+ order: "relevance",
26
+ maxResults: 15,
27
+ part: "snippet",
28
+ key: config.api_key
25
29
  )
26
- videos = MultiJson.load(http_response.body)["feed"]["entry"]
30
+ return if http_response.status != 200
31
+ videos = MultiJson.load(http_response.body)["items"]
27
32
  video = videos.sample
28
- video["link"].each do |link|
29
- if link["rel"] == "alternate" && link["type"] == "text/html"
30
- response.reply link["href"].split("&").first
31
- end
32
- end
33
+ id = video["id"]["videoId"]
34
+ response.reply "https://www.youtube.com/watch?v=#{id}"
33
35
  if config.video_info
34
- response.reply info(video)
36
+ response.reply info(id)
35
37
  end
36
38
  end
37
39
 
38
40
  def display_info(response)
39
41
  if config.detect_urls
40
42
  id = response.matches[0][0]
41
- http_response = http.get("#{API_URL}/#{id}", alt: "json")
42
- video = MultiJson.load(http_response.body)["entry"]
43
- response.reply info(video)
43
+ info_string = info(id)
44
+ unless info_string.nil?
45
+ response.reply info_string
46
+ end
44
47
  end
45
48
  end
46
49
 
47
- def info(video)
48
- title = video["title"]["$t"]
49
- uploader = video["author"][0]["name"]["$t"]
50
- date = DateTime.iso8601(video["published"]["$t"]).strftime("%F")
50
+ def info(id)
51
+ http_response = http.get(
52
+ "#{API_URL}/videos",
53
+ id: id,
54
+ part: "contentDetails,snippet,statistics",
55
+ key: config.api_key
56
+ )
57
+ return nil if http_response.status != 200
58
+ videos = MultiJson.load(http_response.body)["items"]
59
+ return nil if videos.empty?
60
+ video = videos[0]
61
+ title = video["snippet"]["title"]
62
+ uploader = video["snippet"]["channelTitle"]
63
+ date = DateTime.iso8601(video["snippet"]["publishedAt"]).strftime("%F")
51
64
 
52
65
  # Format time, only show hours if necessary
53
- sec = video["media$group"]["yt$duration"]["seconds"].to_i
66
+ duration = ISO8601::Duration.new(video["contentDetails"]["duration"])
67
+ sec = duration.to_seconds.to_i
54
68
  min = sec / 60
55
69
  hr = min / 60
56
70
  time = "%d:%02d" % [min, sec%60]
@@ -59,7 +73,7 @@ module Lita
59
73
  end
60
74
 
61
75
  # Abbreviate view count, based on http://stackoverflow.com/a/2693484
62
- n = video["yt$statistics"]["viewCount"].to_i
76
+ n = video["statistics"]["viewCount"].to_i
63
77
  i = 0
64
78
  while n >= 1e3 do
65
79
  n /= 1e3
@@ -67,8 +81,10 @@ module Lita
67
81
  end
68
82
  views = "%.#{n.to_s.length>3?1:0}f%s" % [n.round(1), " kMBT"[i]]
69
83
 
70
- # Calculate rating, accounting for YouTube's 1-5 score range
71
- rating = "%.f" % [(video.fetch("gd$rating", {}).fetch("average", 1) - 1) / 4 * 100]
84
+ # Calculate rating percentage
85
+ likes = video["statistics"]["likeCount"].to_f
86
+ dislikes = video["statistics"]["dislikeCount"].to_f
87
+ rating = "%.f" % (likes / (likes + dislikes) * 100)
72
88
 
73
89
  "#{title} [#{time}] by #{uploader} on #{date} (#{views.strip} views, #{rating}% liked)"
74
90
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-youtube-me"
3
- spec.version = "0.0.3"
3
+ spec.version = "0.0.4"
4
4
  spec.authors = ["Taylor Lapeyre"]
5
5
  spec.email = ["taylorlapeyre@gmail.com"]
6
6
  spec.description = %q{A Lita handler that replies with a youtube URL when given a query.}
@@ -15,6 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.require_paths = ["lib"]
16
16
 
17
17
  spec.add_runtime_dependency "lita", ">= 4.0"
18
+ spec.add_runtime_dependency "iso8601", ">= 0.8.6"
18
19
 
19
20
  spec.add_development_dependency "bundler", "~> 1.3"
20
21
  spec.add_development_dependency "rake"
@@ -1,6 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Handlers::YoutubeMe, lita_handler: true do
4
+ before do
5
+ registry.config.handlers.youtube_me.api_key = ENV["YOUTUBE_KEY"]
6
+ end
7
+
4
8
  it { is_expected.to route_command("youtube me something") }
5
9
  it { is_expected.to route_command("youtube me something").to(:find_video) }
6
10
 
@@ -57,4 +61,23 @@ describe Lita::Handlers::YoutubeMe, lita_handler: true do
57
61
  send_message("https://www.youtube.com/watch?v=nG7RiygTwR4")
58
62
  expect(replies.count).to eq 0
59
63
  end
64
+
65
+ it "does not send a message when the detected YouTube URL does not lead to a valid video" do
66
+ registry.config.handlers.youtube_me.detect_urls = true
67
+ send_message("https://www.youtube.com/watch?v=foo")
68
+ expect(replies.count).to eq 0
69
+ end
70
+
71
+ it "does not return a video in response to a query when the API key is invalid" do
72
+ registry.config.handlers.youtube_me.api_key = "this key doesn't work"
73
+ send_command("youtube me soccer")
74
+ expect(replies.count).to eq 0
75
+ end
76
+
77
+ it "does not display video info for detected YouTube URLs when the API key is invalid" do
78
+ registry.config.handlers.youtube_me.detect_urls = true
79
+ registry.config.handlers.youtube_me.api_key = "this key doesn't work"
80
+ send_message("https://www.youtube.com/watch?v=nG7RiygTwR4")
81
+ expect(replies.count).to eq 0
82
+ end
60
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-youtube-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Lapeyre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: iso8601
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.6
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement