This commit is contained in:
Tommy Mikkelsen 2022-07-13 14:27:41 +02:00
parent 0ba49ac8bb
commit 0c79112169
5 changed files with 246 additions and 114 deletions

View file

@ -201,7 +201,7 @@
"Name": "Download",
"Description": "The @:Modules.Download.Name module allows you to download medias, as well as local assets, from servers where you have been granted download rights",
"SelSrv": "Select Server",
"TT-Server": "Select the server to download from"
"TT-Server": "Select the server to download from. If a server can't be selected, you have not been granted download rights from it"
},
"ET": {
"Description": "@:Modules.ET.Name allows you to export detail information about the media in your libraries",

View file

@ -5,15 +5,11 @@
<img src="@/assets/WebTools-32.png" class="d-inline-block align-top" alt="Kitten">
{{ $t("Common.AppName") }}
</b-navbar-brand>
<b-navbar-nav class="ml-auto mr-2">
<b-nav-form>
<b-form-select v-model="selectedOption" @input="selected" size="sm" class="mr-sm-2">
<option
v-for="option in pserver"
v-for="option in pmsServers"
:value="option"
:key="option.clientIdentifier">
{{ option.name }}
@ -26,73 +22,27 @@
</b-navbar-nav>
<b-navbar-nav class="ml-auto mr-2">
<b-img :src="getAvatar()" rounded="circle" alt="Circle image" width="32" height="32" class="m1"></b-img>
<b-img :src="getAvatar()" rounded="circle" alt="Circle image" width="32" height="32" class="m1"></b-img>
</b-navbar-nav>
</b-navbar>
</div>
<!--
<section class="hero is-dark is-small">
<div class="hero-body">
<div class="level">
<div class="level-right">
<div class="level-item">
<figure class="image is-48x48">
<img src="@/assets/WebTools.png">
</figure>
<div id="title" >
<p id="top_title" class="title is-size-3">{{ $t("Common.AppName") }}</p>
</div>
</div>
</div>
<div class="level-item">
<div class="select is-dark">
<b-select v-bind:placeholder="$t('Common.SelServer')"
v-model="selectedOption"
@input="selected">
<option
v-for="option in pserver"
:value="option"
:key="option.clientIdentifier">
{{ option.name }}
</option>
</b-select>
</div>
<b-button id="sync-button" @click="fetchServers" type="is-warning"
icon-left="fas fa-sync" icon-pack="fas" class="is-pulled-right" >
</b-button>
</div>
<div class="level-left">
<div class="level-item">
<p id="plexname">{{ getPlexName() }}</p>
<figure class="image is-48x48" id="avatar">
<img id="avatar" :src="getAvatar()">
</figure>
</div>
</div>
</div>
</div>
</section> -->
</template>
<script>
import store from '../../store';
import { ptv } from '../modules/General/plextv';
import { wtconfig } from '../modules/General/wtutils';
const log = require('electron-log');
console.log = log.log;
export default {
data(){
return {
selectedOption: []
selectedOption: [],
pmsServers: []
}
},
methods: {
@ -100,10 +50,6 @@ export default {
log.info("[Headers.vue] (fetchServers) fetching servers")
this.$store.dispatch('fetchPlexServers', store.getters.getAuthToken);
},
active2(e) {
log.info("[Headers.vue] (active2) active2 called")
this.active = e;
},
selected: function () {
log.info(`[Headers.vue] (selected) Selected server: ${this.selectedOption.name}`)
ptv.checkServerConnect(this.selectedOption)
@ -117,16 +63,21 @@ export default {
},
getPlexName(){
return this.$store.getters.getPlexName
},
async getValidPMSSrv() {
log.info("[Headers.vue] (getValidPMSSrv) fetching servers");
// Fetch all servers from plex.tv
//await this.$store.dispatch('fetchPlexServers', store.getters.getAuthToken);
await ptv.fetchPMSServers();
// Filter result
const result = await ptv.getPMSServers( wtconfig.get("Developer.showNotOwned", false) );
console.log('Ged 17-3', JSON.stringify(result))
this.pmsServers = result;
}
},
created(){
log.info("[Header.vue] (created) menu created")
this.$store.dispatch('fetchPlexServers');
},
computed: {
pserver(){
return this.$store.getters.getPlexServers
}
log.info("[Header.vue] (created) menu created");
this.getValidPMSSrv();
}
}
</script>

View file

@ -29,6 +29,7 @@
//import i18n from '../../../i18n';
//import store from '../../../store';
//import { wtconfig, wtutils } from '../General/wtutils';
import { ptv } from '../General/plextv'
const log = require("electron-log");
export default {
@ -36,7 +37,8 @@
return {
PageName: "Download",
selSrvOptions: [],
selSrvWait: true
selSrvWait: true,
selSrv: null
};
},
created() {
@ -45,8 +47,27 @@
},
methods: {
// Get a list of servers, that we can download from
getValidServers(){
console.log('Ged 44-3 get valid servers')
async getValidServers(){
log.info(`[download.vue] (getValidServers) - Start getting valid servers`);
this.selSrvWait = false;
// Get all servers
let allPMSSrv = await ptv.getPMSServers( true );
// Walk each of them, to get the options
for (var idx in allPMSSrv){
if ( !allPMSSrv[idx]['PMSInfo'] ){
await ptv.checkServerOptions(allPMSSrv[idx]);
}
}
// Get all servers again, but this time with updated info
allPMSSrv = await ptv.getPMSServers( true );
for (idx in allPMSSrv){
let option = {}
option['text'] = allPMSSrv[idx]['name'];
option['value'] = allPMSSrv[idx]['clientIdentifier'];
option['disabled'] = (allPMSSrv[idx]['PMSInfo']['Sync'] === false);
this.selSrvOptions.push(option);
}
this.selSrvWait = true;
}
},
watch: {

View file

@ -1,11 +1,158 @@
const log = require('electron-log');
import axios from 'axios'
import store from '../../../store';
import { wtutils, wtconfig } from '../General/wtutils';
const ptv = new class PTV {
constructor() {
}
async checkServerOptions(server) {
log.verbose(`[plextv.js] (checkServerOptions) - Checking address for server: ${server.name}`);
let PMSAddress = '';
let PMSInfo = {};
//let local = false;
let sync = false;
let allPMSServer = await this.getPMSServers( true );
let uris = [];
// let uri;
// If server is owned, we check local connection first
if (server.owned){
uris.push(server.connections.filter(function(x){return x.local==true})[0]['uri']);
} else {
uris.push(server.connections.filter(function(x){return x.local==false})[0]['uri']);
}
// Now add remaining connections
for (var connIdx in server.connections)
{
if ( uris.indexOf(server.connections[connIdx]['uri']) == -1 ){
uris.push(server.connections[connIdx]['uri']);
}
}
log.debug(`[plextv.js] (checkServerOptions) - Valid addresses for ${server.name} are: ${JSON.stringify(uris)}`)
// uris now contains an ordered list of addresses to test
for (var i = 0; i < uris.length; i++) {
//let val = server.connections[i];
let result = false;
log.verbose(`[plextv.js] (checkServerOptions) Checking: ${server.name} on uri: ${uris[i]}`);
let baseurl = uris[i];
let header = wtutils.PMSHeader;
header['X-Plex-Token'] = server.accessToken;
await axios.get(baseurl, {
headers: header,
timeout: 3000
})
.then(response => {
if(response.status == 200){
log.verbose(`[plextv.js] (checkServerOptions) - Address ${baseurl} is alive`);
log.verbose(`[plextv.js] (checkServerOptions) - Need to check if correct one`);
const machineIdentifier = response.data['MediaContainer']['machineIdentifier'];
if (machineIdentifier == server.clientIdentifier){
log.verbose(`[plextv.js] (checkServerOptions) - Server found as: ${baseurl}`);
PMSAddress = baseurl;
sync = response.data['MediaContainer']['allowSync'];
result = true;
}
}
}).catch((error) => {
if (error.response) {
// The request was made and server responded with a status code
// that falls out of the range of 2xx
log.warn(`[plextv.js] (checkServerOptions) ${error.response.status}`)
} else if (error.request) {
// The request was made but no response was received
log.warn(`[plextv.js] (checkServerOptions) No response recieved`);
}
else {
// Something happened in setting up the request that triggered an Error
log.warn(`[plextv.js] (checkServerOptions) ${error.message}`);
}
}
)
if (result){ break; }
}
/*
// Start with the local address check first
for (var i = 0; i < server.connections.length; i++) {
let val = server.connections[i];
let result = false;
log.verbose(`[plextv.js] (checkServerOptions) Checking: ${val.protocol}://${val.address}:${val.port}`);
let baseurl = val.protocol + '://' + val.address + ':' + val.port;
let header = wtutils.PMSHeader;
header['X-Plex-Token'] = server.accessToken;
await axios.get(baseurl, {
headers: header,
timeout: 3000
})
.then(response => {
if(response.status == 200){
log.verbose(`[plextv.js] (checkServerOptions) Address ${baseurl} is alive, so check if local`);
if ( val.local == true){
log.verbose(`[plextv.js] (checkServerOptions) It's a local server, so need to check if correct one`);
const machineIdentifier = response.data['MediaContainer']['machineIdentifier'];
if (machineIdentifier == server.clientIdentifier){
log.verbose(`[plextv.js] (checkServerOptions) Local server found as: ${baseurl}`);
PMSAddress = baseurl;
local = true;
sync = response.data['MediaContainer']['allowSync'];
result = true;
}
}
else
{
// only if we didn't find the local one?
if ( local == false)
{
log.verbose(`[plextv.js] (checkServerOptions) No local server found yet, so checking ${baseurl}`)
const machineIdentifier = response.data['MediaContainer']['machineIdentifier'];
if (machineIdentifier == server.clientIdentifier){
log.verbose(`[plextv.js] (checkServerOptions) Remote server found as: ${baseurl}`);
PMSAddress = baseurl;
local = false;
sync = response.data['MediaContainer']['allowSync'];
result = true;
}
}
}
}
}).catch((error) => {
if (error.response) {
// The request was made and server responded with a status code
// that falls out of the range of 2xx
log.warn(`[plextv.js] (checkServerOptions) ${error.response.status}`)
} else if (error.request) {
// The request was made but no response was received
log.warn(`[plextv.js] (checkServerOptions) No response recieved`);
}
else {
// Something happened in setting up the request that triggered an Error
log.warn(`[plextv.js] (checkServerOptions) ${error.message}`);
}
}
)
if (result){ break; }
}
*/
const idx = allPMSServer.map(function(x) {return x.clientIdentifier; }).indexOf(server.clientIdentifier);
PMSInfo['Address'] = PMSAddress;
// PMSInfo['Local'] = local;
PMSInfo['Sync'] = sync;
allPMSServer[idx]["PMSInfo"] = PMSInfo
store.commit("UPDATE_PLEX_SERVERS", allPMSServer);
return
}
async checkServerConnect(server) {
log.verbose(`[plextv.js] (checkServerConnect) Checking address for server: ${server.name}`);
// Set WaitState
@ -70,5 +217,61 @@ const ptv = new class PTV {
store.commit("UPDATE_PLEX_SELECTED_SERVER_STATUS", false);
return PMSAddress
}
async fetchPMSServers(){
let header = wtutils.PMSHeader;
header['X-Plex-Token'] = store.getters.getAuthToken;
await axios({
method: 'get',
url: `${wtutils.plexTVApi}v2/resources`,
headers: header,
params: {
'includeHttps' : '1',
'includeRelay': '0'
}
})
.then((response) => {
let result=[];
log.debug('[plextv.js] (fetchPlexServers) Response from fetchPlexServers recieved');
const showNotOwned = wtconfig.get('Developer.showNotOwned', false);
if (showNotOwned){
log.debug('[plextv.js] (fetchPlexServers) fetchPlexServers : See not owned servers as well');
}
response.data.forEach((req) => {
if (req.product == "Plex Media Server") {
let pmsServer = {};
pmsServer['name'] = req.name;
pmsServer['accessToken'] = req.accessToken;
pmsServer['connections'] = req.connections;
pmsServer['clientIdentifier'] = req.clientIdentifier;
pmsServer['owned'] = req.owned;
result.push(pmsServer);
}
})
store.commit('UPDATE_PLEX_SERVERS', result);
return true;
})
.catch(function (error) {
if (error.response) {
log.error('fetchPlexServers: ' + error.response.data);
alert(error.response.data.errors[0].code + " " + error.response.data.errors[0].message);
} else if (error.request) {
log.error('fetchPlexServers: ' + error.request);
} else {
log.error('fetchPlexServers: ' + error.message);
}
});
}
async getPMSServers( showNotOwned = false ){
const PMSServers = await store.getters.getPlexServers;
let result = [];
for (const idx in PMSServers)
{
if ( true == (showNotOwned || PMSServers[idx].owned ) )
{
result.push(PMSServers[idx]);
}
}
return result;
}
}
export {ptv};

View file

@ -1,6 +1,6 @@
import axios from 'axios';
import router from '../../router'
import {wtconfig, wtutils} from '../../components/modules/General/wtutils'
import {wtutils} from '../../components/modules/General/wtutils'
import i18n from '../../i18n';
const log = require('electron-log');
@ -83,49 +83,6 @@ const actions = {
}
});
},
fetchPlexServers({ commit, getters }) {
let header = wtutils.PMSHeader;
header['X-Plex-Token'] = getters.getAuthToken;
axios({
method: 'get',
url: `${wtutils.plexTVApi}v2/resources`,
headers: header,
params: {
'includeHttps' : '1',
'includeRelay': '0'
}
})
.then((response) => {
let result=[];
log.debug('[plextv.js] (fetchPlexServers) Response from fetchPlexServers recieved');
const showNotOwned = wtconfig.get('Developer.showNotOwned', false);
if (showNotOwned){
log.debug('[plextv.js] (fetchPlexServers) fetchPlexServers : See not owned servers as well');
}
response.data.forEach((req) => {
if (req.product == "Plex Media Server") {
let pmsServer = {};
pmsServer['name'] = req.name;
pmsServer['accessToken'] = req.accessToken;
pmsServer['connections'] = req.connections;
pmsServer['clientIdentifier'] = req.clientIdentifier;
pmsServer['owned'] = req.owned;
result.push(pmsServer);
}
})
commit('UPDATE_PLEX_SERVERS', result);
})
.catch(function (error) {
if (error.response) {
log.error('fetchPlexServers: ' + error.response.data);
alert(error.response.data.errors[0].code + " " + error.response.data.errors[0].message);
} else if (error.request) {
log.error('fetchPlexServers: ' + error.request);
} else {
log.error('fetchPlexServers: ' + error.message);
}
});
},
loginToPlex({ commit }, payload){
log.info("[plextv.js] (loginToPlex) loginToPlex called")
var url = `${wtutils.plexTVApi}v2/users/signin`;