-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a742039
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Surya Widi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Hubot Youtube Live Chat Adapter | ||
|
||
## Usage | ||
- Create a new bot, follow the instruction [here](https://hubot.github.com/docs/) | ||
- Add hubot-youtube-live to your bot | ||
``` | ||
npm install hubot-youtube-live --save | ||
``` | ||
- run hubot with the Youtube adaptor | ||
``` | ||
bin/hubot -a youtube-live | ||
``` | ||
|
||
## Configuration | ||
This adapter requires this following environment variables | ||
|
||
- ```YOUTUBE_CLIENT_ID``` | ||
- ```YOUTUBE_CLIENT_SECRET``` | ||
- ```YOUTUBE_OAUTH2_REFRESH_TOKEN``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "hubot-youtube-live", | ||
"version": "1.0.1", | ||
"description": "Youtube adapter for hubot", | ||
"main": "./src/youtube", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/yuyuvn/hubot-youtube-live.git" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"googleapis": "^5.2.1" | ||
}, | ||
|
||
"devDependencies": { | ||
"coffee-script": "~1.7.1", | ||
"hubot": "~2.13" | ||
}, | ||
"engines": { | ||
"node": ">=5.8.0", | ||
"npm": ">=3.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{Adapter, TextMessage} = require 'hubot' | ||
|
||
params = | ||
clientId: process.env.YOUTUBE_CLIENT_ID | ||
clientSecret: process.env.YOUTUBE_CLIENT_SECRET | ||
redirect: process.env.YOUTUBE_REDIRECT || "http://localhost" | ||
rate: process.env.YOUTUBE_RATE || 3000 | ||
code: process.env.YOUTUBE_OAUTH2_REFRESH_TOKEN | ||
scope: ['https://www.googleapis.com/auth/youtube', | ||
'https://www.googleapis.com/auth/youtube.force-ssl', | ||
'https://www.googleapis.com/auth/youtube.readonly'] | ||
|
||
google = require('googleapis') | ||
oauth2Client = new google.auth.OAuth2 params.clientId, params.clientSecret, params.redirect | ||
|
||
class Youtube extends Adapter | ||
constructor: (@robot) -> | ||
super @robot | ||
|
||
send: (envelope, strings...) -> | ||
for str in strings | ||
@api.insert envelope.room, str, (err) => @robot.logger.error err if err? | ||
|
||
reply: (envelope, strings...) -> | ||
name = envelope.user.name | ||
@send envelope, strings.map((str) -> "@#{name} #{str}")... | ||
|
||
run: -> | ||
@api = new YoutubeChat | ||
@api.login params.code, (err) => | ||
return @robot.logger.error err if err? | ||
@emit "connected" | ||
|
||
@api.listen (err, chat) => | ||
return @robot.logger.error err if err? | ||
return unless chat.snippet.type == "textMessageEvent" | ||
|
||
user = @robot.brain.userForId chat.snippet.authorChannelId, | ||
room: chat.snippet.liveChatId | ||
name: chat.authorDetails.displayName | ||
text = chat.snippet.textMessageDetails.messageText | ||
@robot.logger.debug "#{user.name}[#{user.room}]: #{text}" | ||
@receive new TextMessage user, text, chat.id | ||
|
||
class YoutubeChat | ||
getLiveChat: (broadcast_id, pageToken=null, cb) -> | ||
[cb, pageToken] = [pageToken, null] unless cb? | ||
|
||
filter = | ||
part: "id,authorDetails,snippet" | ||
fields: "items(authorDetails/displayName,id,snippet),nextPageToken" | ||
liveChatId: broadcast_id | ||
maxResults: 2000 | ||
filter.pageToken = pageToken if pageToken? | ||
|
||
@youtube.liveChatMessages.list filter, (err, response) => | ||
return cb(err) if err? | ||
|
||
for item in response.items | ||
date = new Date(item.snippet.publishedAt) | ||
continue if @broadcasts[broadcast_id].lastPublished? and @broadcasts[broadcast_id].lastPublished >= date | ||
cb err, item if item.snippet.authorChannelId != @bot_channel | ||
if response.nextPageToken? | ||
setTimeout () => | ||
@getLiveChat broadcast_id, response.nextPageToken, cb | ||
, params.rate | ||
|
||
|
||
getBroadcastList: (pageToken=null, cb) -> | ||
[cb, pageToken] = [pageToken, null] unless cb? | ||
|
||
filter = | ||
part: "snippet" | ||
fields: "items/snippet/liveChatId,nextPageToken" | ||
broadcastType: "all" | ||
maxResults: 50 | ||
if process.env.HUBOT_YOUTUBE_FILTER_IDS? | ||
filter.id = process.env.HUBOT_YOUTUBE_FILTER_IDS | ||
else if process.env.HUBOT_YOUTUBE_FILTER_STATUS? | ||
filter.broadcastStatus = process.env.HUBOT_YOUTUBE_FILTER_STATUS | ||
else | ||
filter.mine = true | ||
filter.pageToken = pageToken if pageToken? | ||
|
||
@youtube.liveBroadcasts.list filter, (err, response) => | ||
return cb(err) if err? | ||
|
||
return unless response.items? | ||
for item in response.items | ||
item = item.snippet | ||
@broadcasts[item.liveChatId] = lastPublished: new Date() if item.liveChatId? | ||
if response.nextPageToken? | ||
setTimeout () => | ||
@getBroadcastList response.nextPageToken, cb | ||
, params.rate | ||
else | ||
cb() | ||
|
||
login: (token, cb) -> | ||
oauth2Client.setCredentials refresh_token: token | ||
@youtube = google.youtube | ||
version: 'v3' | ||
auth: oauth2Client | ||
|
||
@youtube.channels.list part: "id", mine: true, (err, res) => | ||
return cb(err) if err? | ||
@bot_channel = res.items[0].id if res.items? and res.items[0]? | ||
@broadcasts = [] | ||
@getBroadcastList cb | ||
|
||
listen: (cb) -> | ||
for broadcast_id in Object.keys @broadcasts | ||
@getLiveChat broadcast_id, cb | ||
|
||
insert: (broadcast, message, cb) -> | ||
body = snippet: | ||
liveChatId: broadcast | ||
type: "textMessageEvent" | ||
textMessageDetails: messageText: message | ||
@youtube.liveChatMessages.insert part: "snippet", resource: body, cb | ||
|
||
exports.use = (robot) -> | ||
new Youtube robot |