From b3f05f8329622a207e893dae3e884912dfc2ce43 Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 24 Apr 2018 22:35:55 -0500 Subject: [PATCH] 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. --- .gitignore | 59 ++++++++++++++++++++++++++++++++++ LICENSE | 21 ++++++++++++ README.md | 1 + example.js | 8 +++++ index.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 17 ++++++++++ 6 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 example.js create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..00cbbdf --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ab60297 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9fd6d7 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# node-digitalshift diff --git a/example.js b/example.js new file mode 100644 index 0000000..83128bd --- /dev/null +++ b/example.js @@ -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) +}) diff --git a/index.js b/index.js new file mode 100644 index 0000000..430450b --- /dev/null +++ b/index.js @@ -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()}) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..3db6dd3 --- /dev/null +++ b/package.json @@ -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": { + } +}