lita-youtube-me 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e33154d4d4e9746167beb96df87aecf48af0b060
4
- data.tar.gz: 13db137790abdf97bf826fe36f78ce506c08921b
3
+ metadata.gz: 1b452c59b9836a8899cbf89e470dd184f5782658
4
+ data.tar.gz: c2b7a1c05505d331542fdc0f3fbbde9886232b74
5
5
  SHA512:
6
- metadata.gz: 8a4575541adfc20ce3c9865d5b6e28b9d6e14df945323c0f08c490f1eeb5525c7aaf8787d2b474b4014fb0e66d4653139d39ec784ca3c2035ca90c0ae73afe5a
7
- data.tar.gz: 6beedb87488f65e6c3ef94fb7dec7fc6355a8e00cb7c30319ae17b7494ac75a4287fb2898f330c6dbd347ea0ef842c40e611727273287af79c4e1486bd230dd5
6
+ metadata.gz: 26e367d1fe3800f44cf5b43def7b1ce396ad52ddbad3ca2b4de514790cd16557c5ae6736889959f3db9ec69e76b17e4458ab4509da8d1a94c32c4a152ef001b7
7
+ data.tar.gz: 3690cbfb65f64948e4b35f9b164cbe6dfb97f471ce8cc8c8e2302e5f9e52afbcb4f8e9b248fd7f6d7c60787136475c898f710c42b216826126871dfa0b8bd4f2
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # lita-youtube-me
2
2
 
3
- Replies with a video that matches a string.
3
+ Replies with a video that matches a string. Optionally detects YouTube URLs and returns title, duration, etc.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,11 +10,47 @@ Add lita-youtube to your Lita instance's Gemfile:
10
10
  gem "lita-youtube-me"
11
11
  ```
12
12
 
13
+ ## Configuration
14
+
15
+ ### Optional attributes
16
+ * `video_info` (boolean) - When set to `true`, Lita will return additional information (title, duration, etc.) about the video. Default: `false`
17
+ * `detect_urls` (boolean) - When set to `true`, Lita will return additional information about any YouTube URLs it detects. Default: `false`
18
+
19
+ ### Example
20
+ ``` ruby
21
+ Lita.configure do |config|
22
+ config.handlers.youtube_me.video_info = true
23
+ config.handlers.youtube_me.detect_urls = true
24
+ end
25
+ ```
26
+
13
27
  ## Usage
14
28
 
29
+ The following are all equivalent ways of asking Lita to search for a video about "soccer":
15
30
  ```
16
31
  @bot: youtube me soccer
17
32
  @bot: youtube soccer
33
+ @bot: yt me soccer
34
+ @bot: yt soccer
35
+ ```
36
+
37
+ Lita's default response will be a YouTube URL:
38
+ ```
39
+ <bot> https://www.youtube.com/watch?v=SZJldG6bP1s
40
+ ```
41
+
42
+ Enabling the config variable `video_info` will add another line to Lita's response:
43
+ ```
44
+ <bot> https://www.youtube.com/watch?v=SZJldG6bP1s
45
+ <bot> Soccer Kid Thug [0:23] by turdmalone on 2015-04-10 (49.5k views, 96% liked)
46
+ ```
47
+
48
+ Enabling `detect_urls` will make Lita return information about any YouTube URLs it detects:
49
+ ```
50
+ <jack> hey @jill check out the new game of thrones trailer https://www.youtube.com/watch?v=dQw4w9WgXcQ lol
51
+ <bot> Rick Astley - Never Gonna Give You Up [3:33] by RickAstleyVEVO on 2009-10-25 (115.2M views, 94% liked)
52
+ <jill> nope, not falling for it this time!
53
+ <jack> drat
18
54
  ```
19
55
 
20
56
  ## License
@@ -1,14 +1,22 @@
1
+ require "date"
2
+
1
3
  module Lita
2
4
  module Handlers
3
5
  class YoutubeMe < Handler
4
6
  API_URL = "https://gdata.youtube.com/feeds/api/videos"
5
7
 
6
- route(/youtube( me)? (.*)/i, :find_video, command: true, help: {
8
+ config :video_info, types: [TrueClass, FalseClass], default: false
9
+ config :detect_urls, types: [TrueClass, FalseClass], default: false
10
+
11
+ route(/^(?:youtube|yt)(?: me)?\s+(.*)/i, :find_video, command: true, help: {
7
12
  "youtube (me) QUERY" => "Gets a youtube video."
8
13
  })
14
+ # Detect YouTube links in non-commands and display video info
15
+ route(/\byoutube\.com\/watch\?v=([^?&#\s]+)/i, :display_info, command: false)
16
+ route(/\byoutu\.be\/([^?&#\s]+)/i, :display_info, command: false)
9
17
 
10
18
  def find_video(response)
11
- query = response.matches[0][1]
19
+ query = response.matches[0][0]
12
20
  http_response = http.get(API_URL,
13
21
  q: query,
14
22
  orderBy: "relevance",
@@ -19,9 +27,50 @@ module Lita
19
27
  video = videos.sample
20
28
  video["link"].each do |link|
21
29
  if link["rel"] == "alternate" && link["type"] == "text/html"
22
- response.reply link["href"]
30
+ response.reply link["href"].split("&").first
23
31
  end
24
32
  end
33
+ if config.video_info
34
+ response.reply info(video)
35
+ end
36
+ end
37
+
38
+ def display_info(response)
39
+ if config.detect_urls
40
+ 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)
44
+ end
45
+ end
46
+
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")
51
+
52
+ # Format time, only show hours if necessary
53
+ sec = video["media$group"]["yt$duration"]["seconds"].to_i
54
+ min = sec / 60
55
+ hr = min / 60
56
+ time = "%d:%02d" % [min, sec%60]
57
+ if hr > 0
58
+ time = "%d:%02d:%02d" % [hr, min%60, sec%60]
59
+ end
60
+
61
+ # Abbreviate view count, based on http://stackoverflow.com/a/2693484
62
+ n = video["yt$statistics"]["viewCount"].to_i
63
+ i = 0
64
+ while n >= 1e3 do
65
+ n /= 1e3
66
+ i += 1
67
+ end
68
+ views = "%.#{n.to_s.length>3?1:0}f%s" % [n.round(1), " kMBT"[i]]
69
+
70
+ # Calculate rating, accounting for YouTube's 1-5 score range
71
+ rating = "%.f" % [(video.fetch("gd$rating", {}).fetch("average", 1) - 1) / 4 * 100]
72
+
73
+ "#{title} [#{time}] by #{uploader} on #{date} (#{views.strip} views, #{rating}% liked)"
25
74
  end
26
75
  end
27
76
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-youtube-me"
3
- spec.version = "0.0.2"
3
+ spec.version = "0.0.3"
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.}
@@ -7,10 +7,54 @@ describe Lita::Handlers::YoutubeMe, lita_handler: true do
7
7
  it { is_expected.to route_command("youtube something") }
8
8
  it { is_expected.to route_command("youtube something").to(:find_video) }
9
9
 
10
+ it { is_expected.to route_command("yt me something") }
11
+ it { is_expected.to route_command("yt me something").to(:find_video) }
12
+
13
+ it { is_expected.to route_command("yt something") }
14
+ it { is_expected.to route_command("yt something").to(:find_video) }
15
+
16
+ it { is_expected.to route("https://www.youtube.com/watch?v=nG7RiygTwR4&feature=youtube_gdata") }
17
+ it { is_expected.to route("https://www.youtube.com/watch?v=nG7RiygTwR4&feature=youtube_gdata").to(:display_info) }
18
+
19
+ it { is_expected.to route("taco https://youtu.be/nG7RiygTwR4?t=9m13s taco") }
20
+ it { is_expected.to route("taco https://youtu.be/nG7RiygTwR4?t=9m13s taco").to(:display_info) }
21
+
10
22
  it "can find a youtube video with a query" do
11
23
  send_command("youtube me soccer")
12
24
  expect(replies.count).to eq 1
13
25
  expect(replies.last).to_not be_nil
14
26
  expect(replies.last).to match(/youtube\.com/)
15
27
  end
28
+
29
+ it "displays info for a requested video when the video_info config variable is true" do
30
+ registry.config.handlers.youtube_me.video_info = true
31
+ send_command("yt soccer")
32
+ expect(replies.count).to eq 2
33
+ expect(replies.first).to_not be_nil
34
+ expect(replies.first).to match(/youtube\.com/)
35
+ expect(replies.last).to_not be_nil
36
+ expect(replies.last).to match(/views/)
37
+ end
38
+
39
+ it "does not display info for a requested video when the video_info config variable is false" do
40
+ registry.config.handlers.youtube_me.video_info = false
41
+ send_command("youtube me soccer")
42
+ expect(replies.count).to eq 1
43
+ expect(replies.last).to_not be_nil
44
+ expect(replies.last).to match(/youtube\.com/)
45
+ end
46
+
47
+ it "displays video info for detected YouTube URLs when the detect_urls config variable is true" do
48
+ registry.config.handlers.youtube_me.detect_urls = true
49
+ send_message("taco taco https://www.youtube.com/watch?v=nG7RiygTwR4 taco taco")
50
+ expect(replies.count).to eq 1
51
+ expect(replies.first).to_not be_nil
52
+ expect(replies.first).to match(/10 minutes of DJ Mbenga saying Tacos \[10:02\] by RickFreeloader on 2011-09-12 \(\S+ views, \d+% liked\)/)
53
+ end
54
+
55
+ it "does not display video info for detected YouTube URLs when the detect_urls config variable is false" do
56
+ registry.config.handlers.youtube_me.detect_urls = false
57
+ send_message("https://www.youtube.com/watch?v=nG7RiygTwR4")
58
+ expect(replies.count).to eq 0
59
+ end
16
60
  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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Lapeyre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-20 00:00:00.000000000 Z
11
+ date: 2015-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.2.2
122
+ rubygems_version: 2.4.5
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: A Lita handler that replies with a youtube URL when given a query.