Skip to content

Commit

Permalink
Added option
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jan 7, 2020
1 parent 9e91edb commit 5687a7f
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 134 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ To get an API-Key you have to go to [console.developers.google.com](https://cons

## Changelog

### 0.0.4

* (klein0r) Added more options

### 0.0.3

* (klein0r) Support for multiple channels
Expand Down
68 changes: 38 additions & 30 deletions admin/index_m.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,47 @@
</head>
<body>
<div class="m adapter-container">
<div class="col s12">
<ul class="tabs">
<li class="tab col s2"><a href="#tab-main" class="translate active">main settings</a></li>
<li class="tab col s2"><a href="#tab-channels" class="translate">channels</a></li>
</ul>
</div>
<div id="tab-main" class="col s12 page">
<div class="row">
<div class="col s12 m4 l2">
<img src="youtube.png" class="logo">
</div>
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab col s2"><a href="#tab-main" class="translate active">main settings</a></li>
<li class="tab col s2"><a href="#tab-channels" class="translate">channels</a></li>
</ul>
</div>
<div class="row">
<div class="col s6 input-field">
<input type="text" class="value" id="apiKey" />
<label for="apiKey" class="translate">api key</label>
<div id="tab-main" class="col s12 page">
<div class="row">
<div class="col s12 m4 l2">
<img src="youtube.png" class="logo">
</div>
</div>
<div class="row">
<div class="col s6 input-field">
<input type="text" class="value" id="apiKey" />
<label for="apiKey" class="translate">api key</label>
</div>
</div>
<div class="row">
<div class="col s6 input-field">
<input id="enableVideoInformation" class="value" type="checkbox"/>
<span for="enableVideoInformation" class="translate">enable video information</span>
</div>
</div>
</div>
</div>
<div id="tab-channels" class="col s12 page">
<div class="row">
<div class="col s12" id="channels">
<a class="btn-floating waves-effect waves-light blue table-button-add"><i class="material-icons">add</i></a>
<div class="table-values-div">
<table class="table-values">
<thead>
<tr>
<th data-name="name" style="width: 45%" class="translate">custom channel name</th>
<th data-name="id" style="width: 45%" class="translate">channel id</th>
<th data-buttons="delete" style="width: 10%"></th>
</tr>
</thead>
</table>
<div id="tab-channels" class="col s12 page">
<div class="row">
<div class="col s12" id="channels">
<a class="btn-floating waves-effect waves-light blue table-button-add"><i class="material-icons">add</i></a>
<div class="table-values-div">
<table class="table-values">
<thead>
<tr>
<th data-name="name" style="width: 45%" class="translate">custom channel name</th>
<th data-name="id" style="width: 45%" class="translate">channel id</th>
<th data-buttons="delete" style="width: 10%"></th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
Expand Down
12 changes: 8 additions & 4 deletions io-package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"common": {
"name": "youtube",
"version": "0.0.3",
"version": "0.0.4",
"news": {
"0.0.4": {
"en": "Added more options",
"de": "Mehr Optionen hinzugefügt"
},
"0.0.3": {
"en": "Support for multiple channels",
"de": "Unterstützung für mehrere Kanäle"
}
},
"0.0.2": {
"en": "improved error handling",
"de": "Fehlerbehandlung verbessert"
Expand Down Expand Up @@ -43,7 +47,7 @@
"readme": "https://github.com/klein0r/ioBroker.youtube/blob/master/README.md",
"loglevel": "info",
"mode": "schedule",
"schedule": "*/5 * * * *",
"schedule": "0 0 * * *",
"type": "misc-data",
"compact": true,
"materialize": true,
Expand All @@ -53,7 +57,7 @@
},
"native": {
"apiKey": "",
"channelId": ""
"enableVideoInformation": false
},
"objects": [],
"instanceObjects": [
Expand Down
200 changes: 101 additions & 99 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Youtube extends utils.Adapter {

const self = this;
const apiKey = this.config.apiKey;
const enableVideoInformation = this.config.enableVideoInformation;

self.log.debug('youtube/v3/channels - Request init - ' + id);

Expand Down Expand Up @@ -166,109 +167,110 @@ class Youtube extends utils.Adapter {
}
);

// Fill latest video information
request(
{
url: 'https://www.googleapis.com/youtube/v3/search?part=id,snippet&type=video&order=date&maxResults=5&channelId=' + id + '&key=' + apiKey,
json: true,
timeout: 4500
},
(error, response, content) => {
self.log.debug('youtube/v3/search Request done');
self.log.debug('received data (' + response.statusCode + '): ' + JSON.stringify(content));

if (!error && response.statusCode == 200) {

if (content && Object.prototype.hasOwnProperty.call(content, 'items') && Array.isArray(content['items']) && content['items'].length > 0) {
for (let i = 0; i < content['items'].length; i++) {

const v = content['items'][i];
const path = cpath + '.video.' + i + '.';

self.setObjectNotExists(path, {
type: 'channel',
common: {
name: 'Video data ' + (i + 1)
},
native: {}
});

self.setObjectNotExists(path + 'id', {
type: 'state',
common: {
name: 'Id',
type: 'string',
role: 'media.playid',
read: true,
write: false
},
native: {}
});
self.setState(path + 'id', {val: v.id.videoId, ack: true});

self.setObjectNotExists(path + 'url', {
type: 'state',
common: {
name: 'URL',
type: 'string',
role: 'url.blank',
read: true,
write: false
},
native: {}
});
self.setState(path + 'url', {val: 'https://youtu.be/' + v.id.videoId, ack: true});

self.setObjectNotExists(path + 'title', {
type: 'state',
common: {
name: 'Title',
type: 'string',
role: 'media.title',
read: true,
write: false
},
native: {}
});
self.setState(path + 'title', {val: v.snippet.title, ack: true});

self.setObjectNotExists(path + 'published', {
type: 'state',
common: {
name: 'Published',
type: 'string',
role: 'media.date',
read: true,
write: false
},
native: {}
});
self.setState(path + 'published', {val: v.snippet.publishedAt, ack: true});

self.setObjectNotExists(path + 'description', {
type: 'state',
common: {
name: 'Description',
type: 'string',
role: 'state',
read: true,
write: false
},
native: {}
});
self.setState(path + 'description', {val: v.snippet.description, ack: true});
if (enableVideoInformation) {
// Fill latest video information
request(
{
url: 'https://www.googleapis.com/youtube/v3/search?part=id,snippet&type=video&order=date&maxResults=5&channelId=' + id + '&key=' + apiKey,
json: true,
timeout: 4500
},
(error, response, content) => {
self.log.debug('youtube/v3/search Request done');
self.log.debug('received data (' + response.statusCode + '): ' + JSON.stringify(content));

if (!error && response.statusCode == 200) {

if (content && Object.prototype.hasOwnProperty.call(content, 'items') && Array.isArray(content['items']) && content['items'].length > 0) {
for (let i = 0; i < content['items'].length; i++) {

const v = content['items'][i];
const path = cpath + '.video.' + i + '.';

self.setObjectNotExists(path, {
type: 'channel',
common: {
name: 'Video data ' + (i + 1)
},
native: {}
});

self.setObjectNotExists(path + 'id', {
type: 'state',
common: {
name: 'Id',
type: 'string',
role: 'media.playid',
read: true,
write: false
},
native: {}
});
self.setState(path + 'id', {val: v.id.videoId, ack: true});

self.setObjectNotExists(path + 'url', {
type: 'state',
common: {
name: 'URL',
type: 'string',
role: 'url.blank',
read: true,
write: false
},
native: {}
});
self.setState(path + 'url', {val: 'https://youtu.be/' + v.id.videoId, ack: true});

self.setObjectNotExists(path + 'title', {
type: 'state',
common: {
name: 'Title',
type: 'string',
role: 'media.title',
read: true,
write: false
},
native: {}
});
self.setState(path + 'title', {val: v.snippet.title, ack: true});

self.setObjectNotExists(path + 'published', {
type: 'state',
common: {
name: 'Published',
type: 'string',
role: 'media.date',
read: true,
write: false
},
native: {}
});
self.setState(path + 'published', {val: v.snippet.publishedAt, ack: true});

self.setObjectNotExists(path + 'description', {
type: 'state',
common: {
name: 'Description',
type: 'string',
role: 'state',
read: true,
write: false
},
native: {}
});
self.setState(path + 'description', {val: v.snippet.description, ack: true});
}
} else {
self.log.warn('youtube/v3/search - received empty response - check channel id');
}
} else if (error) {
self.log.warn(error);
} else {
self.log.warn('youtube/v3/search - received empty response - check channel id');
self.log.error('youtube/v3/search - Status Code: ' + response.statusCode + ' / Content: ' + JSON.stringify(content));
}

} else if (error) {
self.log.warn(error);
} else {
self.log.error('youtube/v3/search - Status Code: ' + response.statusCode + ' / Content: ' + JSON.stringify(content));
}
}
);
);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.youtube",
"version": "0.0.3",
"version": "0.0.4",
"description": "ioBroker YouTube Adapter",
"author": {
"name": "Matthias Kleine",
Expand Down

0 comments on commit 5687a7f

Please sign in to comment.