├── README.md ├── .eslintignore ├── .commit-template ├── .github ├── FUNDING.yml └── workflows │ ├── build.yml │ └── release.yml ├── .vscode ├── extensions.json └── settings.json ├── commitlint.config.js ├── .eslintrc.js ├── package.json ├── .gitignore ├── tsconfig.json └── src └── kb-plex-api-client.ts /README.md: -------------------------------------------------------------------------------- 1 | kb-plex-api-client 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | .eslintrc.js -------------------------------------------------------------------------------- /.commit-template: -------------------------------------------------------------------------------- 1 | type(scope): subject 2 | 3 | description -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: 2 | - https://paypal.me/thatkookooguy?locale.x=en_US -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "rbbit.typescript-hero" 4 | ] 5 | } -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ '@commitlint/config-angular' ], 3 | rules: { 4 | 'type-enum': [ 5 | 2, 6 | 'always', [ 7 | 'build', 8 | 'chore', 9 | 'ci', 10 | 'docs', 11 | 'feat', 12 | 'fix', 13 | 'perf', 14 | 'refactor', 15 | 'revert', 16 | 'style', 17 | 'test' 18 | ] 19 | ] 20 | } 21 | }; -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: "Build" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | - next 8 | pull_request: 9 | branches: 10 | - master 11 | - next 12 | 13 | jobs: 14 | build: 15 | name: Test build 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v1 19 | - name: Build 20 | run: | 21 | npm install 22 | npm run build -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | branches: 5 | # - dummy 6 | - master 7 | - next 8 | jobs: 9 | release: 10 | name: Release 11 | runs-on: ubuntu-18.04 12 | steps: 13 | - name: Checkout 14 | uses: actions/checkout@v2 15 | with: 16 | fetch-depth: 0 17 | - name: Setup Node.js 18 | uses: actions/setup-node@v1 19 | with: 20 | node-version: 12 21 | - name: Install dependencies 22 | run: | 23 | npm ci 24 | npm install 25 | npm run build 26 | - name: Release 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 30 | run: npm run semantic-release -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended' 10 | ], 11 | root: true, 12 | env: { 13 | node: true, 14 | jest: true, 15 | }, 16 | rules: { 17 | "@typescript-eslint/naming-convention": [ 18 | "error", 19 | { 20 | "selector": "interface", 21 | "format": ["PascalCase"], 22 | "custom": { 23 | "regex": "^I[A-Z]", 24 | "match": true 25 | } 26 | } 27 | ], 28 | '@typescript-eslint/explicit-function-return-type': 'off', 29 | '@typescript-eslint/explicit-module-boundary-types': 'off', 30 | '@typescript-eslint/no-explicit-any': 'off', 31 | }, 32 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@kibibit/kb-plex-api-client", 3 | "version": "0.0.0-development", 4 | "description": "plex.tv api client for node.js & browser using axios", 5 | "types": "lib/kb-plex-api-client.d.ts", 6 | "main": "lib/kb-plex-api-client.js", 7 | "files": [ 8 | "/lib" 9 | ], 10 | "scripts": { 11 | "build": "tsc", 12 | "generate-barrels": "barrelsby --delete -d ./src -l below -q", 13 | "semantic-release:setup": "semantic-release-cli setup", 14 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"", 15 | "lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 16 | "test": "echo \"Error: no test specified\" && exit 1", 17 | "semantic-release": "semantic-release" 18 | }, 19 | "author": "thatkookooguy ", 20 | "license": "MIT", 21 | "devDependencies": { 22 | "@commitlint/cli": "^11.0.0", 23 | "@commitlint/config-angular": "^11.0.0", 24 | "@commitlint/config-conventional": "^11.0.0", 25 | "@types/lodash": "^4.14.167", 26 | "@typescript-eslint/eslint-plugin": "^4.13.0", 27 | "@typescript-eslint/parser": "^4.13.0", 28 | "all-contributors-cli": "^6.19.0", 29 | "commitizen": "^4.2.2", 30 | "cz-conventional-changelog": "^3.3.0", 31 | "eslint": "^7.17.0", 32 | "husky": "^4.3.7", 33 | "semantic-release": "^17.3.1", 34 | "semantic-release-cli": "^5.4.1", 35 | "ts-node": "^9.1.1", 36 | "typescript": "^4.1.3" 37 | }, 38 | "repository": { 39 | "type": "git", 40 | "url": "https://github.com/Kibibit/kb-plex-api-client.git" 41 | }, 42 | "dependencies": { 43 | "axios": "^0.21.1", 44 | "lodash": "^4.17.20" 45 | }, 46 | "config": { 47 | "commitizen": { 48 | "path": "./node_modules/cz-conventional-changelog" 49 | } 50 | }, 51 | "husky": { 52 | "hooks": { 53 | "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true", 54 | "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 55 | } 56 | }, 57 | "publishConfig": { 58 | "access": "public" 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "workbench.colorCustomizations": { 3 | "activityBar.activeBackground": "#4a0081", 4 | "activityBar.activeBorder": "#9f5b00", 5 | "activityBar.background": "#4a0081", 6 | "activityBar.foreground": "#e7e7e7", 7 | "activityBar.inactiveForeground": "#e7e7e799", 8 | "activityBarBadge.background": "#9f5b00", 9 | "activityBarBadge.foreground": "#e7e7e7", 10 | "statusBar.background": "#2d004e", 11 | "statusBar.foreground": "#e7e7e7", 12 | "statusBarItem.hoverBackground": "#4a0081", 13 | "titleBar.activeBackground": "#2d004e", 14 | "titleBar.activeForeground": "#e7e7e7", 15 | "titleBar.inactiveBackground": "#2d004e99", 16 | "titleBar.inactiveForeground": "#e7e7e799" 17 | }, 18 | "peacock.color": "#2d004e", 19 | "editor.tabSize": 2, 20 | "editor.insertSpaces": true, 21 | "editor.detectIndentation": false, 22 | "editor.rulers": [80], 23 | "editor.fontFamily": "Hack, Menlo, Monaco, 'Courier New', monospace", 24 | "editor.matchBrackets": "always", 25 | // Terminal 26 | "terminal.integrated.fontFamily": "Hack, Menlo, Monaco, 'Courier New', monospace", 27 | "terminal.integrated.fontSize": 14, 28 | // Workbench 29 | "workbench.colorTheme": "Andromeda", 30 | "workbench.editor.showIcons": true, 31 | "workbench.iconTheme": "vs-seti", 32 | // Bracket Pair Colorizer 33 | "bracketPairColorizer.colorMode": "Consecutive", 34 | "bracketPairColorizer.forceUniqueOpeningColor": true, 35 | "bracketPairColorizer.showBracketsInGutter": true, 36 | "window.title": "${activeEditorShort}${separator}${rootName} [kibibit]", 37 | "typescriptHero.imports.stringQuoteStyle": "'", 38 | "typescriptHero.imports.grouping": [ 39 | "Plains", 40 | "Modules", 41 | "/^@kb-/", 42 | "Workspace" 43 | 44 | ], 45 | "typescriptHero.imports.organizeOnSave": true, 46 | "typescriptHero.imports.multiLineTrailingComma": false, 47 | "editor.codeActionsOnSave": { 48 | "source.fixAll.eslint": true 49 | } 50 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | yarn-debug.log* 8 | yarn-error.log* 9 | lerna-debug.log* 10 | 11 | # Diagnostic reports (https://nodejs.org/api/report.html) 12 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 13 | 14 | # Runtime data 15 | pids 16 | *.pid 17 | *.seed 18 | *.pid.lock 19 | 20 | # Directory for instrumented libs generated by jscoverage/JSCover 21 | lib-cov 22 | 23 | # Coverage directory used by tools like istanbul 24 | coverage 25 | *.lcov 26 | 27 | # nyc test coverage 28 | .nyc_output 29 | 30 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 31 | .grunt 32 | 33 | # Bower dependency directory (https://bower.io/) 34 | bower_components 35 | 36 | # node-waf configuration 37 | .lock-wscript 38 | 39 | # Compiled binary addons (https://nodejs.org/api/addons.html) 40 | build/Release 41 | 42 | # Dependency directories 43 | node_modules/ 44 | jspm_packages/ 45 | 46 | # Snowpack dependency directory (https://snowpack.dev/) 47 | web_modules/ 48 | 49 | # TypeScript cache 50 | *.tsbuildinfo 51 | 52 | # Optional npm cache directory 53 | .npm 54 | 55 | # Optional eslint cache 56 | .eslintcache 57 | 58 | # Microbundle cache 59 | .rpt2_cache/ 60 | .rts2_cache_cjs/ 61 | .rts2_cache_es/ 62 | .rts2_cache_umd/ 63 | 64 | # Optional REPL history 65 | .node_repl_history 66 | 67 | # Output of 'npm pack' 68 | *.tgz 69 | 70 | # Yarn Integrity file 71 | .yarn-integrity 72 | 73 | # dotenv environment variables file 74 | .env 75 | .env.test 76 | 77 | # parcel-bundler cache (https://parceljs.org/) 78 | .cache 79 | .parcel-cache 80 | 81 | # Next.js build output 82 | .next 83 | out 84 | 85 | # Nuxt.js build / generate output 86 | .nuxt 87 | dist 88 | 89 | # Gatsby files 90 | .cache/ 91 | # Comment in the public line in if your project uses Gatsby and not Next.js 92 | # https://nextjs.org/blog/next-9-1#public-directory-support 93 | # public 94 | 95 | # vuepress build output 96 | .vuepress/dist 97 | 98 | # Serverless directories 99 | .serverless/ 100 | 101 | # FuseBox cache 102 | .fusebox/ 103 | 104 | # DynamoDB Local files 105 | .dynamodb/ 106 | 107 | # TernJS port file 108 | .tern-port 109 | 110 | # Stores VSCode versions used for testing VSCode extensions 111 | .vscode-test 112 | 113 | # yarn v2 114 | .yarn/cache 115 | .yarn/unplugged 116 | .yarn/build-state.yml 117 | .yarn/install-state.gz 118 | .pnp.* 119 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Basic Options */ 6 | // "incremental": true, /* Enable incremental compilation */ 7 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ 8 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ 9 | // "lib": [], /* Specify library files to be included in the compilation. */ 10 | // "allowJs": true, /* Allow javascript files to be compiled. */ 11 | // "checkJs": true, /* Report errors in .js files. */ 12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 13 | "declaration": true, /* Generates corresponding '.d.ts' file. */ 14 | "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 15 | "sourceMap": true, /* Generates corresponding '.map' file. */ 16 | // "outFile": "./", /* Concatenate and emit output to single file. */ 17 | "outDir": "./lib", /* Redirect output structure to the directory. */ 18 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 19 | // "composite": true, /* Enable project compilation */ 20 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 21 | "removeComments": true, /* Do not emit comments to output. */ 22 | // "noEmit": true, /* Do not emit outputs. */ 23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 26 | 27 | /* Strict Type-Checking Options */ 28 | "strict": true, /* Enable all strict type-checking options. */ 29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 30 | // "strictNullChecks": true, /* Enable strict null checks. */ 31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 32 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 33 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 34 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 35 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 36 | 37 | /* Additional Checks */ 38 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 39 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 40 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 41 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 42 | // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ 43 | 44 | /* Module Resolution Options */ 45 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 46 | "baseUrl": "./src", /* Base directory to resolve non-absolute module names. */ 47 | "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 48 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 49 | // "typeRoots": [], /* List of folders to include type definitions from. */ 50 | // "types": [], /* Type declaration files to be included in compilation. */ 51 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 52 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 53 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 54 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 55 | 56 | /* Source Map Options */ 57 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 58 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 59 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 60 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 61 | 62 | /* Experimental Options */ 63 | "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 64 | "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 65 | 66 | /* Advanced Options */ 67 | "skipLibCheck": true, /* Skip type checking of declaration files. */ 68 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 69 | }, 70 | "exclude": ["node_modules", "lib", "**/*.spec.ts"] 71 | } 72 | -------------------------------------------------------------------------------- /src/kb-plex-api-client.ts: -------------------------------------------------------------------------------- 1 | import axios, { AxiosInstance } from 'axios'; 2 | import { concat, get } from 'lodash'; 3 | 4 | type TKeyValue = { [key: string]: string | number }; 5 | interface IWindowOpen { 6 | (url?: string, target?: string, features?: string, replace?: boolean): Window | null; 7 | } 8 | 9 | export interface IChannel { 10 | number: number; 11 | } 12 | export interface IDvrDevice { 13 | key: string; 14 | } 15 | 16 | export interface IDvr { 17 | key: string; 18 | Device: IDvrDevice[]; 19 | 20 | } 21 | 22 | export interface IOptions { 23 | appName: string; 24 | token: string; 25 | protocol?: string; 26 | host?: string; 27 | port?: number; 28 | 29 | } 30 | export class PlexApiClient { 31 | public appName: string; 32 | private httpClient: AxiosInstance; 33 | public token: string; 34 | private headers: { [key: string]: string }; 35 | 36 | constructor(opts: IOptions) { 37 | this.appName = opts.appName; 38 | this.token = opts.token; 39 | 40 | this.headers = { 41 | 'Accept': 'application/json', 42 | 'X-Plex-Device': this.appName, 43 | 'X-Plex-Device-Name': this.appName, 44 | 'X-Plex-Product': this.appName, 45 | 'X-Plex-Version': '0.1', 46 | 'X-Plex-Client-Identifier': 'rg14zekk3pa5zp4safjwaa8z', 47 | 'X-Plex-Platform': 'Chrome', 48 | 'X-Plex-Platform-Version': '80.0' 49 | }; 50 | 51 | this.httpClient = axios.create({ 52 | timeout: 3000, 53 | baseURL: `${ opts.protocol || 'http' }://${ opts.host || '127.0.0.1' }:${ opts.port || 32400 }`, 54 | headers: { 55 | "X-Initialized-At": Date.now().toString() 56 | } 57 | }); 58 | 59 | this.httpClient.interceptors.request.use((config) => { 60 | config.headers = { 61 | ...this.headers, 62 | ...config.headers 63 | }; 64 | 65 | if (this.token) { 66 | config.headers['X-Plex-Token'] = this.token; 67 | } 68 | // Do something before request is sent 69 | return config; 70 | }, (error) => { 71 | // Do something with request error 72 | return Promise.reject(error); 73 | }); 74 | 75 | this.httpClient.interceptors.response.use((response) => { 76 | const token = get(response.data, 'user.authToken'); 77 | if (token) { 78 | this.token = token; 79 | } 80 | return response; 81 | }, (error) => Promise.reject(error)); 82 | } 83 | 84 | private static authHeaders(appName = 'kbPlexApiClient') { 85 | return { 86 | 'Accept': 'application/json', 87 | 'X-Plex-Product': appName, 88 | 'X-Plex-Version': 'Plex OAuth', 89 | 'X-Plex-Client-Identifier': 'rg14zekk3pa5zp4safjwaa8z', 90 | 'X-Plex-Model': 'Plex OAuth' 91 | }; 92 | } 93 | 94 | static async webLogin(plex: Partial, open: IWindowOpen): Promise { 95 | const headers = PlexApiClient.authHeaders(plex.appName); 96 | const { data } = await axios.post('https://plex.tv/api/v2/pins?strong=true', {}, { headers }); 97 | open('https://app.plex.tv/auth/#!?clientID=rg14zekk3pa5zp4safjwaa8z&context[device][version]=Plex OAuth&context[device][model]=Plex OAuth&code=' + data.code + '&context[device][product]=Plex Web'); 98 | 99 | return await PlexApiClient.waitForWindowResponse(plex, data); 100 | } 101 | 102 | static async waitForWindowResponse(plex: Partial, plexLogin: { id: string; }): Promise { 103 | const headers = PlexApiClient.authHeaders(plex.appName); 104 | return new Promise((resolve, reject) => { 105 | let limit = 120000 // 2 minute time out limit 106 | const poll = 2000 // check every 2 seconds for token 107 | const interval = setInterval(async () => { 108 | const { data } = await axios.get(`https://plex.tv/api/v2/pins/${ plexLogin.id }`, { headers }); 109 | limit -= poll; 110 | if (limit <= 0) { 111 | clearInterval(interval); 112 | reject(new Error('Timed Out. Failed to sign in a timely manner (2 mins)')); 113 | } 114 | 115 | if (data.authToken !== null) { 116 | clearInterval(interval); 117 | plex.token = data.authToken; 118 | const client = new PlexApiClient(plex as IOptions); 119 | const _res = await client.Get('/') 120 | data.name = _res.friendlyName; 121 | resolve(client); 122 | } 123 | }, poll); 124 | }); 125 | } 126 | 127 | async SignIn(login: string, password: string) { 128 | const data = new FormData(); 129 | data.append('user', JSON.stringify({ login, password })); 130 | const res = await this.httpClient.post('https://plex.tv/users/sign_in.json', { data }); 131 | if (res.status !== 201) { 132 | throw new Error(`Plex 'SignIn' Error - Username/Email and Password is incorrect!.`); 133 | } 134 | 135 | const token = this.token; 136 | return { token }; 137 | } 138 | 139 | async Get(path: string, optionalHeaders: TKeyValue = {}) { 140 | const res = await this.httpClient.get(path, { headers: optionalHeaders }); 141 | if (res.status !== 200) { 142 | throw new Error(`Plex 'Get' request failed. URL: ${path}`); 143 | } 144 | 145 | return res.data.MediaContainer; 146 | } 147 | async Put(path: string, params: TKeyValue = {}, optionalHeaders: TKeyValue = {}) { 148 | const res = await this.httpClient.put(path, {}, { headers: optionalHeaders, params }); 149 | 150 | if (res.status !== 200) { 151 | throw new Error(`Plex 'Get' request failed. URL: ${path}`); 152 | } 153 | 154 | return res.data; 155 | } 156 | 157 | async Post(path: string, params: TKeyValue = {}, optionalHeaders: TKeyValue = {}) { 158 | const res = await this.httpClient.put(path, {}, { headers: optionalHeaders, params }); 159 | 160 | if (res.status !== 200) { 161 | throw new Error(`Plex 'Get' request failed. URL: ${path}`); 162 | } 163 | 164 | return res.data; 165 | } 166 | 167 | async GetDVRS() { 168 | const result = await this.Get('/livetv/dvrs'); 169 | let dvrs = result.Dvr as IDvr[]; 170 | dvrs = dvrs || []; 171 | return dvrs; 172 | } 173 | 174 | async refreshGuide(_dvrs: IDvr[]) { 175 | const dvrs = _dvrs || await this.GetDVRS(); 176 | 177 | for (let i = 0; i < dvrs.length; i++) { 178 | try { 179 | await this.Post(`/livetv/dvrs/${dvrs[i].key}/reloadGuide`); 180 | } catch (err) { 181 | console.error(err); 182 | } 183 | } 184 | } 185 | 186 | async refreshChannels(channels: IChannel[], _dvrs: IDvr[]) { 187 | const dvrs = typeof _dvrs !== 'undefined' ? _dvrs : await this.GetDVRS(); 188 | const channelNumbers = channels.map((channel) => channel.number); 189 | const qs: TKeyValue = {}; 190 | 191 | qs.channelsEnabled = channelNumbers.join(','); 192 | channelNumbers.forEach((channelNum) => { 193 | qs[`channelMapping[${ channelNum }]`] = channelNum; 194 | qs[`channelMappingByKey[${ channelNum }]`] = channelNum; 195 | }); 196 | const devices = concat([], ...dvrs.map((dvr) => dvr.Device)); 197 | 198 | await Promise.all(devices.map((device) => this.Put(`/media/grabbers/devices/${ device.key }/channelmap`, qs))); 199 | } 200 | } 201 | --------------------------------------------------------------------------------