Advertisement
Guest User

setting.js

a guest
Mar 24th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Q = require('q'),
  2.     os = require('os'),
  3.     path = require('path'),
  4.     _ = require('underscore'),
  5.     data_path = require('nw.gui').App.dataPath;
  6.  
  7. /** Default settings **/
  8. var Settings = {};
  9.  
  10. // User interface
  11. Settings.language = '';
  12. Settings.translateSynopsis = true;
  13. Settings.coversShowRating = true;
  14. Settings.watchedCovers = 'fade';
  15. Settings.showAdvancedSettings = false;
  16.  
  17. Settings.postersMinWidth = 134;
  18. Settings.postersMaxWidth = 294;
  19. Settings.postersMinFontSize = 0.8;
  20. Settings.postersMaxFontSize = 1.3;
  21. Settings.postersSizeRatio = (196 / 134);
  22. Settings.postersWidth = Settings.postersMinWidth;
  23. Settings.postersJump = [134, 154, 174, 194, 214, 234, 254, 274, 294];
  24.  
  25. //Playback
  26. Settings.alwaysFullscreen = false;
  27. Settings.playNextEpisodeAuto = true;
  28. Settings.chosenPlayer = 'local';
  29.  
  30. // Advanced UI
  31. Settings.alwaysOnTop = false;
  32. Settings.theme = 'Official_-_Dark_theme';
  33. Settings.ratingStars = true; //trigger on click in details
  34. Settings.startScreen = 'Movies';
  35. Settings.lastTab = '';
  36.  
  37. // Quality
  38. Settings.shows_default_quality = '720p';
  39. Settings.movies_default_quality = '1080p';
  40. Settings.moviesShowQuality = false;
  41. Settings.movies_quality = 'all';
  42.  
  43. // Subtitles
  44. Settings.subtitle_language = 'none';
  45. Settings.subtitle_size = '28px';
  46. Settings.subtitle_color = '#ffffff';
  47. Settings.subtitle_decoration = 'Outline';
  48. Settings.subtitle_font = 'Arial';
  49.  
  50. // More options
  51. Settings.httpApiPort = 8008;
  52. Settings.httpApiUsername = 'popcorn';
  53. Settings.httpApiPassword = 'popcorn';
  54.  
  55. // Trakt.tv
  56. Settings.traktToken = '';
  57. Settings.traktTokenRefresh = '';
  58. Settings.traktTokenTTL = '';
  59. Settings.traktTvVersion = '0.0.2';
  60. Settings.traktLastSync = '';
  61. Settings.traktLastActivities = '';
  62. Settings.traktSyncOnStart = true;
  63. Settings.traktPlayback = true;
  64.  
  65. // TVShow Time
  66. Settings.tvstAccessToken = '';
  67.  
  68. // Advanced options
  69. Settings.connectionLimit = 200;
  70. Settings.dhtLimit = 500;
  71. Settings.streamPort = 0; // 0 = Random
  72. Settings.tmpLocation = path.join(os.tmpDir(), 'Popcorn-Time');
  73. Settings.databaseLocation = path.join(data_path, 'data');
  74. Settings.deleteTmpOnClose = true;
  75. Settings.automaticUpdating = true;
  76. Settings.events = true;
  77. Settings.minimizeToTray = false;
  78. Settings.bigPicture = false;
  79.  
  80. // Features
  81. Settings.activateTorrentCollection = true;
  82. Settings.activateWatchlist = true;
  83. Settings.activateVpn = false;
  84. Settings.activateRandomize = true;
  85. Settings.onlineSearchEngine = 'KAT';
  86.  
  87. // Ratio
  88. Settings.totalDownloaded = 0;
  89. Settings.totalUploaded = 0;
  90.  
  91. // VPN
  92. Settings.vpn = false;
  93. Settings.vpnUsername = '';
  94. Settings.vpnPassword = '';
  95.  
  96. Settings.tvAPI = [{
  97.     url: 'https://popcorntimece.ch/api/v2/',
  98.     strictSSL: true
  99. },{
  100.     url: 'https://api-fetch.website/tv',
  101.     strictSSL: true
  102. }, {
  103.     url: 'https://odgoglfi7uddahby.onion.to/',
  104.     strictSSL: true
  105. }, {
  106.     url: 'http://tv.ytspt.re/',
  107.     strictSSL: false
  108. }];
  109.  
  110. Settings.ytsAPI = [{
  111.     uri: 'https://api-fetch.website/movies',
  112.     strictSSL: true
  113. }, {
  114.     uri: 'https://cloudflare.com/',
  115.     headers: {
  116.         'Host': 'xor.image.yt',
  117.         'User-Agent': 'Mozilla/5.0 (Linux) AppleWebkit/534.30 (KHTML, like Gecko) PT/3.8.0'
  118.     },
  119.     strictSSL: true
  120. }, {
  121.     uri: 'http://cloudflare.com/',
  122.     headers: {
  123.         'Host': 'xor.image.yt',
  124.         'User-Agent': 'Mozilla/5.0 (Linux) AppleWebkit/534.30 (KHTML, like Gecko) PT/3.8.0'
  125.     },
  126.     strictSSL: false
  127. }];
  128.  
  129. Settings.updateEndpoint = {
  130.     url: 'https://popcorntime.sh/',
  131.     index: 0,
  132.     proxies: [{
  133.         url: 'https://popcorntime.sh/'
  134.     }]
  135. };
  136.  
  137. // App Settings
  138. Settings.version = false;
  139. Settings.dbversion = '0.1.0';
  140. Settings.font = 'tahoma';
  141. Settings.defaultWidth = Math.round(window.screen.availWidth * 0.8);
  142. Settings.defaultHeight = Math.round(window.screen.availHeight * 0.8);
  143.  
  144. // Miscellaneous
  145. Settings.playerSubPosition = '0px';
  146. Settings.playerVolume = '1';
  147. Settings.tv_detail_jump_to = 'next';
  148.  
  149.  
  150. var ScreenResolution = {
  151.     get SD() {
  152.         return window.screen.width < 1280 || window.screen.height < 720;
  153.     },
  154.     get HD() {
  155.         return window.screen.width >= 1280 && window.screen.width < 1920 || window.screen.height >= 720 && window.screen.height < 1080;
  156.     },
  157.     get FullHD() {
  158.         return window.screen.width >= 1920 && window.screen.width < 2000 || window.screen.height >= 1080 && window.screen.height < 1600;
  159.     },
  160.     get UltraHD() {
  161.         return window.screen.width >= 2000 || window.screen.height >= 1600;
  162.     },
  163.     get QuadHD() {
  164.         return window.screen.width >= 3000 || window.screen.height >= 1800;
  165.     },
  166.     get Standard() {
  167.         return window.devicePixelRatio <= 1;
  168.     },
  169.     get Retina() {
  170.         return window.devicePixelRatio > 1;
  171.     }
  172. };
  173.  
  174. var AdvSettings = {
  175.  
  176.     get: function (variable) {
  177.         if (typeof Settings[variable] !== 'undefined') {
  178.             return Settings[variable];
  179.         }
  180.  
  181.         return false;
  182.     },
  183.  
  184.     set: function (variable, newValue) {
  185.         Database.writeSetting({
  186.                 key: variable,
  187.                 value: newValue
  188.             })
  189.             .then(function () {
  190.                 Settings[variable] = newValue;
  191.             });
  192.     },
  193.  
  194.     setup: function () {
  195.         AdvSettings.performUpgrade();
  196.         return AdvSettings.getHardwareInfo();
  197.     },
  198.  
  199.     getHardwareInfo: function () {
  200.         if (/64/.test(process.arch)) {
  201.             AdvSettings.set('arch', 'x64');
  202.         } else {
  203.             AdvSettings.set('arch', 'x86');
  204.         }
  205.  
  206.         switch (process.platform) {
  207.         case 'darwin':
  208.             AdvSettings.set('os', 'mac');
  209.             break;
  210.         case 'win32':
  211.             AdvSettings.set('os', 'windows');
  212.             break;
  213.         case 'linux':
  214.             AdvSettings.set('os', 'linux');
  215.             break;
  216.         default:
  217.             AdvSettings.set('os', 'unknown');
  218.             break;
  219.         }
  220.  
  221.         return Q();
  222.     },
  223.  
  224.     getNextApiEndpoint: function (endpoint) {
  225.         if (endpoint.index < endpoint.proxies.length - 1) {
  226.             endpoint.index++;
  227.         } else {
  228.             endpoint.index = 0;
  229.         }
  230.         endpoint.ssl = undefined;
  231.         _.extend(endpoint, endpoint.proxies[endpoint.index]);
  232.         return endpoint;
  233.     },
  234.  
  235.     checkApiEndpoints: function (endpoints) {
  236.         return Q.all(_.map(endpoints, function (endpoint) {
  237.             return AdvSettings.checkApiEndpoint(endpoint);
  238.         }));
  239.     },
  240.  
  241.     checkApiEndpoint: function (endpoint, defer) {
  242.         if (Settings.automaticUpdating === false) {
  243.             return;
  244.         }
  245.         var tls = require('tls'),
  246.             http = require('http'),
  247.             uri = require('url');
  248.  
  249.         defer = defer || Q.defer();
  250.  
  251.         endpoint.ssl = undefined;
  252.         _.extend(endpoint, endpoint.proxies[endpoint.index]);
  253.  
  254.         var url = uri.parse(endpoint.url);
  255.         win.debug('Checking %s endpoint', url.hostname);
  256.  
  257.         if (endpoint.ssl === false) {
  258.             var timeoutWrapper = function (req) {
  259.                 return function () {
  260.                     win.warn('[%s] Endpoint timed out',
  261.                         url.hostname);
  262.                     req.abort();
  263.                     tryNextEndpoint();
  264.                 };
  265.             };
  266.             var request = http.get({
  267.                 hostname: url.hostname
  268.             }, function (res) {
  269.                 res.once('data', function (body) {
  270.                     clearTimeout(timeout);
  271.                     res.removeAllListeners('error');
  272.                     // Doesn't match the expected response
  273.                     if (!_.isRegExp(endpoint.fingerprint) || !endpoint.fingerprint.test(body.toString('utf8'))) {
  274.                         win.warn('[%s] Endpoint fingerprint %s does not match %s',
  275.                             url.hostname,
  276.                             endpoint.fingerprint,
  277.                             body.toString('utf8'));
  278.                         tryNextEndpoint();
  279.                     } else {
  280.                         defer.resolve();
  281.                     }
  282.                 }).once('error', function (e) {
  283.                     win.warn('[%s] Endpoint failed [%s]',
  284.                         url.hostname,
  285.                         e.message);
  286.                     clearTimeout(timeout);
  287.                     tryNextEndpoint();
  288.                 });
  289.             });
  290.  
  291.             var fn = timeoutWrapper(request);
  292.             var timeout = setTimeout(fn, 5000);
  293.         } else {
  294.             tls.connect(443, url.hostname, {
  295.                 servername: url.hostname,
  296.                 rejectUnauthorized: false
  297.             }, function () {
  298.                 this.setTimeout(0);
  299.                 this.removeAllListeners('error');
  300.                 if (!this.authorized ||
  301.                     this.authorizationError ||
  302.                     this.getPeerCertificate().fingerprint !== endpoint.fingerprint) {
  303.                     // "These are not the certificates you're looking for..."
  304.                     // Seems like they even got a certificate signed for us :O
  305.                     win.warn('[%s] Endpoint fingerprint %s does not match %s',
  306.                         url.hostname,
  307.                         endpoint.fingerprint,
  308.                         this.getPeerCertificate().fingerprint);
  309.                     tryNextEndpoint();
  310.                 } else {
  311.                     defer.resolve();
  312.                 }
  313.                 this.end();
  314.             }).once('error', function (e) {
  315.                 win.warn('[%s] Endpoint failed [%s]',
  316.                     url.hostname,
  317.                     e.message);
  318.                 this.setTimeout(0);
  319.                 tryNextEndpoint();
  320.             }).once('timeout', function () {
  321.                 win.warn('[%s] Endpoint timed out',
  322.                     url.hostname);
  323.                 this.removeAllListeners('error');
  324.                 this.end();
  325.                 tryNextEndpoint();
  326.             }).setTimeout(5000);
  327.         }
  328.  
  329.         function tryNextEndpoint() {
  330.             if (endpoint.index < endpoint.proxies.length - 1) {
  331.                 endpoint.index++;
  332.                 AdvSettings.checkApiEndpoint(endpoint, defer);
  333.             } else {
  334.                 endpoint.index = 0;
  335.                 endpoint.ssl = undefined;
  336.                 _.extend(endpoint, endpoint.proxies[endpoint.index]);
  337.                 defer.resolve();
  338.             }
  339.         }
  340.  
  341.         return defer.promise;
  342.     },
  343.  
  344.     performUpgrade: function () {
  345.         // This gives the official version (the package.json one)
  346.         gui = require('nw.gui');
  347.         var currentVersion = gui.App.manifest.version;
  348.  
  349.         if (currentVersion !== AdvSettings.get('version')) {
  350.             // Nuke the DB if there's a newer version
  351.             // Todo: Make this nicer so we don't lose all the cached data
  352.             var cacheDb = openDatabase('cachedb', '', 'Cache database', 50 * 1024 * 1024);
  353.  
  354.             cacheDb.transaction(function (tx) {
  355.                 tx.executeSql('DELETE FROM subtitle');
  356.                 tx.executeSql('DELETE FROM metadata');
  357.             });
  358.  
  359.             // Add an upgrade flag
  360.             window.__isUpgradeInstall = true;
  361.         }
  362.         AdvSettings.set('version', currentVersion);
  363.         AdvSettings.set('releaseName', gui.App.manifest.releaseName);
  364.     },
  365. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement