Initial commit

Basic structure and proof of concept for talking with Shift via Node. Will eventually expose the remaining (known) APIs, but this was to get myself familiar with promises, http, streams, etc.
This commit is contained in:
2018-04-24 22:35:55 -05:00
committed by Parnic
commit b3f05f8329
6 changed files with 196 additions and 0 deletions

59
.gitignore vendored Normal file
View File

@ -0,0 +1,59 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.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
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018
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.

1
README.md Normal file
View File

@ -0,0 +1 @@
# node-digitalshift

8
example.js Normal file
View File

@ -0,0 +1,8 @@
const ShiftRequest = require('./index')
let s = new ShiftRequest()
s.login().then(() => {
return s.divisionStandings(4702)
}).then((standings) => {
console.log(standings.teams)
})

90
index.js Normal file
View File

@ -0,0 +1,90 @@
const http = require('http')
const querystring = require('querystring')
const zlib = require('zlib')
let apiKey = 'YXBpLnNoaWZ0c3RhdHMuY29tLDE5YjhhZGIwNDVjZjAxMzJhM2E5N2VmZDQ1YTRj'
class Request {
request(options) {
return new Promise((resolve, reject) => {
http.get(options, (response) => {
const { statusCode } = response
const contentType = response.headers['content-type']
let error
if (statusCode !== 200) {
error = new Error(`Request Failed.\nStatus Code: ${statusCode}`)
} else if (!/^application\/json/.test(contentType)) {
error = new Error(`Invalid content-type.\nExpected application/json but received ${contentType}`)
}
if (error) {
response.resume()
reject(error)
return
}
let rawData = ''
const gunzip = zlib.createGunzip()
gunzip.on('data', (chunk) => {
rawData += chunk
}).on('end', () => {
try {
const parsedData = JSON.parse(rawData)
resolve(parsedData)
} catch (e) {
reject(e)
}
})
response.pipe(gunzip)
}).on('error', (e) => {
reject(e)
})
})
}
}
module.exports = class ShiftRequest extends Request {
request(options) {
return super.request({
hostname: 'api.shiftstats.com',
path: this.url(options.url, options.query),
headers: options.headers
})
}
basicHeaders() {
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'com.digitalshift.hockeyshift',
'Accept-Language': 'en-US',
'Accept-Encoding': 'gzip,deflate',
'Connection': 'keep-alive',
}
}
headers() {
if (typeof this.headersWithTicket === 'undefined') {
this.headersWithTicket = Object.assign({'Authorization': `StatsAuth ticket="${this.ticketHash}"`}, this.basicHeaders())
}
return this.headersWithTicket
}
url(path, query) {
return `/${path}?${querystring.stringify(query)}`
}
login() {
return this.request({ url: 'login', query: {key: apiKey}, headers: this.basicHeaders() }).then((json) => {
this.ticketHash = json.ticket.hash
return json
})
}
divisionStandings(division_id, type = 'Regular Season') {
return this.request({ url: `division/${division_id}/standings`, query: {type: type}, headers: this.headers()})
}
}

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "node-digitalshift",
"description": "Library for talking with DigitalShift network sites (HockeyShift, BasketballShift, etc.)",
"version": "0.0.1",
"main": "index.js",
"license": "MIT",
"repository": "https://github.com/parnic/node-digitalshift.git",
"main": "index.js",
"keywords": [
"digitalshift",
"sports",
"hockey",
"hockeyshift"
],
"dependencies": {
}
}