Skip to content

Commit

Permalink
2.0.0: Tons of improvements. Sync history (finally), get images from …
Browse files Browse the repository at this point in the history
…tmdb.
  • Loading branch information
MrMamen committed Jun 28, 2017
1 parent cbeb124 commit 3b80f23
Show file tree
Hide file tree
Showing 49 changed files with 10,167 additions and 502 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ build
*.pem
coverage
app.zip
config.json
config.json
.idea/
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ To run tests
npm test
```

### Credits
<h3 align="center">
<a href="https://tegon.github.io/traktflix/">
<img src="https://raw.githubusercontent.com/mrmamen/traktNRK/master/tmdb-api-logo.png" alt="TMDB" width="150">
</a>
<a href="https://tegon.github.io/traktflix/">
<img src="https://raw.githubusercontent.com/mrmamen/traktNRK/master/trakt-api-logo.png" alt="TMDB" width="150">
</a>
</h3>

This product uses the TMDb API but is not endorsed or certified by TMDb. <br>
This product uses the Trakt.tv API.

[LICENSE](LICENSE)
23 changes: 23 additions & 0 deletions app/history-sync.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="styles/build/vendor.css">
<link rel="stylesheet" type="text/css" href="styles/build/popup.css">
</head>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header">
<div class="mdl-layout__header-row">
<span class="mdl-layout-title">NRK history sync</span>
</div>
</header>
<main class="mdl-layout__content">
<div class="page-content" id="viewing-activity-app"></div>
</main>
</div>
<script type="text/javascript" src="scripts/build/vendor.js"></script>
<script type="text/javascript" src="scripts/build/history-sync.js"></script>
</body>
</html>
6 changes: 4 additions & 2 deletions app/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_appName__",
"short_name": "__MSG_appShortName__",
"version": "1.3.1",
"version": "2.0.0",
"manifest_version": 2,
"description": "__MSG_appDescription__",
"icons": {
Expand Down Expand Up @@ -44,7 +44,9 @@
"storage",
"unlimitedStorage",
"http://trakt.tv/*",
"https://www.google-analytics.com/"
"https://www.google-analytics.com/",
"*://*.nrk.no/*",
"notifications"
],
"web_accessible_resources": [
"images/svg/*.svg"
Expand Down
11 changes: 10 additions & 1 deletion app/scripts/src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Analytics = require('./analytics.js');
var Oauth = require('../oauth.js');
var Settings = require('../settings.js');
var service = analytics.getService('traktNRK');
var rollbar = require('../rollbar.js');
var tracker = service.getTracker(Settings.analyticsId);
Analytics.setTracker(tracker);

Expand Down Expand Up @@ -56,5 +57,13 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
chrome.storage.local.clear(sendResponse);
return true;
break;
case 'showErrorNotification':
chrome.notifications.create({
type: 'basic',
iconUrl: 'images/traktflix-icon-128.png',
title: 'An error has occurred :(',
message: request.message
});
break;
}
});
});
41 changes: 35 additions & 6 deletions app/scripts/src/content/content-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var ItemParser = require('./item-parser.js');
var Search = require('./search.js');
var Scrobble = require('./scrobble.js');
var Rollbar = require('../rollbar.js');
var ChromeStorage = require('../chrome-storage.js');

function ContentController() {
this.item = null;
Expand All @@ -25,18 +27,20 @@ ContentController.prototype = {
});
},

onSearchError: function(status, response) {
onSearchError: function(status, response, options) {
this.sendAnalyticsEvent({ name: 'onSearchError', value: status + ' - ' + this.item.title });
console.error('traktNRK: Search error', status, response, this.item.title);
console.log('traktNRK: Search error', status, response, options, this.item.title);
this.reportError('Search', status, response, options);
},

onScrobbleSuccess: function() {
this.sendAnalyticsEvent({ name: 'Scrobble', value: 'onSuccess' });
},

onScrobbleError: function() {
onScrobbleError: function(status, response, options) {
this.sendAnalyticsEvent({ name: 'Scrobble', value: 'onError' });
console.error('traktNRK: Scrobble error');
console.log('traktNRK: Scrobble error', status, response, options);
this.reportError('Scrobble', status, response, options);
},

storeItem: function(item) {
Expand Down Expand Up @@ -94,10 +98,35 @@ ContentController.prototype = {
});
},

showErrorNotification: function(message) {
chrome.runtime.sendMessage({ type: 'showErrorNotification', message: message });
},

reportError: function(type, status, response, options) {
if (status === 404) {
this.showErrorNotification("Oh snap! We couldn't find what you're watching in Trakt.tv. However if you are logged into NRK, you can now sync your history.");
Rollbar.info('traktNRK: ' + type + ' error. Did not find item.', this.item);
} else if (status === 0) {
// status 0 usually means an response without CORS
// It could be a 401, so we check if the user has an access_token saved
ChromeStorage.get(null, function(data) {
if (!!data.access_token) {
this.showErrorNotification("We couldn't talk to Trakt.tv servers. We're trying to fix it, please try again later");
Rollbar.warning('traktflix: ' + type + ' error', { status: status, response: response, options: options });
} else {
this.showErrorNotification("Looks like you're not logged in. Please open the extension and login with your Trakt.tv account");
}
}.bind(this));
} else {
this.showErrorNotification("We couldn't talk to Trakt.tv servers. We're trying to fix it, please try again later");
Rollbar.warning('traktflix: ' + type + ' error', { status: status, response: response, options: options });
}
},

getCurrentItem: function() {
if (this.item && this.item.type === 'show') {
if (this.item && this.scrobble && this.item.type === 'show') {
return { item: this.scrobble.item.episode };
} else if (this.item && this.item.type === 'movie') {
} else if (this.item && this.scrobble && this.item.type === 'movie') {
return { item: this.scrobble.item.movie };
}
}
Expand Down
17 changes: 1 addition & 16 deletions app/scripts/src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

var WatchEvents = require('./watch-events.js');
var ContentController = require('./content-controller.js');
var Sync = require('./sync.js');
var controller = new ContentController();
var sync = new Sync();
var rollbar = require('../rollbar.js');

var events = new WatchEvents({
onPlay: controller.onPlay.bind(controller),
Expand All @@ -13,23 +12,9 @@ var events = new WatchEvents({
});

events.startListeners();
sync.needToSync(function(needToSync) {
if (needToSync) {
sync.start(function(success) {
console.log('sync completed', success);
});
}
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type == 'getCurrentItem') {
sendResponse(controller.getCurrentItem());
}
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type == 'startSync') {
sync.start(sendResponse);
return true;
}
});
8 changes: 6 additions & 2 deletions app/scripts/src/content/scrobble.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ function Scrobble(options) {
if (options.type === 'show') {
this.item = { episode: options.response };
} else {
this.item = { movie: options.response.movie };
var movie = options.response.movie || {};
movie.type = 'movie';
this.item = { movie: movie };
}

this.onProgressChange();
Expand All @@ -19,8 +21,10 @@ function Scrobble(options) {

Scrobble.prototype = {
startProgressTimeout: function() {
this.progressChangeInterval = setInterval(function() {
this.progressChangeInterval = setTimeout(function() {
this.onProgressChange();
clearTimeout(this.progressChangeInterval);
this.startProgressTimeout();
}.bind(this), 1000);
},

Expand Down
55 changes: 25 additions & 30 deletions app/scripts/src/content/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ function Search(options) {

Search.prototype = {
getUrl: function() {
return this.url + '?type=' + this.item.type + '&query=' + encodeURIComponent(this.item.title);
return this.url + '/' + this.item.type + '?query=' + encodeURIComponent(this.item.title);
},

getEpisodeUrl: function(slug) {
if (this.item.episode) {
return this.showsUrl + '/' + slug + '/seasons/' + this.item.season
+ '/episodes/' + this.item.episode + '?extended=images';
+ '/episodes/' + this.item.episode;
} else {
return this.showsUrl + '/' + slug + '/seasons/' + this.item.season
+ '?extended=images';
return this.showsUrl + '/' + slug + '/seasons/' + this.item.season;
}
},

Expand All @@ -30,60 +29,56 @@ Search.prototype = {
url: this.getUrl(),
success: function(response) {
var data = JSON.parse(response)[0];
if (data == undefined) {
if (data === undefined) {
options.error.call(this, 404);
} else {
options.success.call(this, data);
}
},
error: function(status, response) {
options.error.call(this, status, response);
error: function(status, response, opts) {
options.error.call(this, status, response, opts);
}
});
},

findEpisodeByTitle: function(response, options) {
findEpisodeByTitle: function(show, response, options) {
var episodes = JSON.parse(response);
var episode;

for (var i = 0; i < episodes.length; i++) {
if (episodes[i].title.toLowerCase() === this.item.epTitle.toLowerCase()) {
if (this.item.epTitle && episodes[i].title && episodes[i].title.toLowerCase() === this.item.epTitle.toLowerCase()) {
episode = episodes[i];
break;
}
}

if (episode) {
options.success.call(this, episode);
options.success.call(this, Object.assign(episode, show));
} else {
options.error.call(this, 404, 'Episode not found.');
options.error.call(this, 404, 'Episode not found.', {show: show, item: this.item});
}
},

findEpisode: function(options) {
this.findItem({
success: function(response) {
if (!response) {
options.error.call(this, "200", response);
} else {
Request.send({
method: 'GET',
url: this.getEpisodeUrl(response['show']['ids']['slug']),
success: function(resp) {
if (this.item.episode) {
options.success.call(this, JSON.parse(resp));
} else {
this.findEpisodeByTitle(resp, options);
}
}.bind(this),
error: function(st, resp) {
options.error.call(this, st, resp);
Request.send({
method: 'GET',
url: this.getEpisodeUrl(response['show']['ids']['slug']),
success: function(resp) {
if (this.item.episode) {
options.success.call(this, Object.assign(JSON.parse(resp), response));
} else {
this.findEpisodeByTitle(response, resp, options);
}
});
}
}.bind(this),
error: function(st, resp, opts) {
options.error.call(this, st, resp, opts);
}
});
}.bind(this),
error: function(status, response) {
options.error.call(this, status, response);
error: function(status, response, opts) {
options.error.call(this, status, response, opts);
}
});
},
Expand Down
Loading

0 comments on commit 3b80f23

Please sign in to comment.