Skip to content

Commit

Permalink
Added content
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesFrost committed Oct 10, 2016
1 parent 9b8ccfc commit 002bc0a
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

# Created by https://www.gitignore.io/api/win,windows,macos,sublimetext,node

#!! ERROR: win is undefined. Use list command to see defined gitignore types !!#

### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### SublimeText ###
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# workspace files are user-specific
*.sublime-workspace

# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project

# sftp configuration file
sftp-config.json

# Package control specific files
Package Control.last-run
Package Control.ca-list
Package Control.ca-bundle
Package Control.system-ca-bundle
Package Control.cache/
Package Control.ca-certs/
bh_unicode_properties.cache

# Sublime-github package stores a github token in this file
# https://packagecontrol.io/packages/sublime-github
GitHub.sublime-settings


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const _oauth = require( 'googleapis' ).auth.OAuth2;

exports.connect = function( oauth, accessToken, liveChatId, callback )
{
const oauthClient = _getOAuthClient( oauth );

oauthClient.setCredentials({
access_token: accessToken
});

const _responseHandler = _curryHandleResponse( callback );

const stream = require( './src/stream.js' );

stream._init( oauthClient, liveChatId, _responseHandler );

return stream;
};

const _curryHandleResponse = function( callback )
{
return function( err, response )
{
_handleResponse( err, response, callback );
};
};

const _handleResponse = function( err, response, callback )
{
if( err )
{
callback( err );
return;
}

response.items.map(function( thisMessage )
{
callback( err, thisMessage );
});
};

const _getOAuthClient = function( oauth )
{
return new _oauth(oauth.client_id, oauth.client_secret, oauth.redirect_uris[0]);
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "youtube-stream",
"version": "0.0.1",
"description": "Stream Youtube live stream chat",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "James Frost <[email protected]> (http://jamesfrost.me)",
"license": "MIT",
"dependencies": {
"googleapis": "^14.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/JamesFrost/youtube-stream"
},
"keywords": [
"youtube",
"live",
"stream",
"chat",
"messaging"
]
}
45 changes: 45 additions & 0 deletions src/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const _youtube = require( 'googleapis' ).youtube( 'v3' );
var _currentStep;

exports._init = function( auth, liveChatId, callback )
{
_step( auth, liveChatId, undefined, callback );
};

const _step = function( auth, liveChatId, pageToken, callback )
{
_getMessages(auth, liveChatId, pageToken, function( err, response )
{
callback( err, response );

if( err )
{
console.log( err );
}

const pollingInterval = response == undefined ? 10000 : response.pollingIntervalMillis;
const pageToken = response == undefined ? undefined : response.nextPageToken;

if( _currentStep === false )
return;

_currentStep = setTimeout(function()
{
_step( auth, liveChatId, pageToken, callback );

}, pollingInterval);

console.log( 'Polling in: ', pollingInterval );
});
};

const _getMessages = function( accessToken, liveChatId, pageToken, callback )
{
_youtube.liveChatMessages.list({ part: 'snippet', liveChatId: liveChatId, auth: accessToken, pageToken : pageToken, maxResults : 2000 }, callback );
};

exports.stop = function()
{
clearTimeout( _currentStep );
_currentStep = false;
};

0 comments on commit 002bc0a

Please sign in to comment.