You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var fs = require("fs");
  2. var http = require("http");
  3. var https = require("https");
  4. var pathlib = require("path");
  5. var urllib = require("url");
  6. var fsutil = require("../fsutil");
  7. var notify = require("../notify");
  8. var player = require("./player");
  9. var httpStream = require("./http-stream");
  10. var torrentStreamer = require("./torrent-streamer");
  11. var subtitleFinder = require("./subtitle-finder");
  12. exports.httpPath = player.httpPath;
  13. exports.cleanupFiles = [];
  14. var app;
  15. var conf
  16. exports.init = function(_app, _conf) {
  17. app = _app;
  18. conf = _conf;
  19. player.init(app, conf);
  20. httpStream.init(app, conf);
  21. torrentStreamer.init(app, conf);
  22. subtitleFinder.init(app, conf);
  23. }
  24. /*
  25. * Filename is optional; in case you want to provide a filename for subtitles
  26. * but want a different path
  27. */
  28. exports.playFile = function(path, cb, filename) {
  29. filename = filename || pathlib.basename(path);
  30. notify("Playing file", filename);
  31. // Find file's length
  32. fs.stat(path, (err, stat) => {
  33. if (err) {
  34. console.trace(err);
  35. return cb();
  36. }
  37. // Find subtitles
  38. subtitleFinder.find(stat.size, filename, subFile => {
  39. // Play!
  40. player.play(path, subFile, cb);
  41. });
  42. });
  43. }
  44. exports.playUrl = function(url, cb) {
  45. notify("Playing url...", url);
  46. // Just play, we won't bother finding subtitles
  47. player.play(url, null, cb);
  48. }
  49. exports.playTorrent = function(magnet, cb) {
  50. notify("Playing torrent...");
  51. // Stream torrent
  52. torrentStreamer.stream(magnet, (err, filesize, filename) => {
  53. if (err)
  54. return cb(err);
  55. // Find subtitles
  56. subtitleFinder.find(filesize, filename, subFile => {
  57. // Play!
  58. notify("Starting playback.", filename);
  59. player.play(app.getAddress()+httpStream.httpPath, subFile, cb);
  60. });
  61. });
  62. }
  63. exports.playTorrentPage = function(url, cb) {
  64. function findMagnet(str) {
  65. var rx = /['"](magnet:[^'"]+)['"]/;
  66. var match = str.match(rx);
  67. if (!match)
  68. return null;
  69. return match[1];
  70. }
  71. notify("Finding magnet on torrent page...", url);
  72. var urlobj = urllib.parse(url);
  73. var o = urlobj.protocol === "https:" ? https : http;
  74. o.request(urlobj, res => {
  75. var str = "";
  76. res
  77. .on("data", d => str += d )
  78. .on("error", err => {
  79. notify("Error downloading page!", err.toString());
  80. console.trace(err);
  81. cb();
  82. })
  83. .on("end", () => {
  84. var magnet = findMagnet(str);
  85. if (!magnet) {
  86. notify("No magnet link on page!");
  87. cb();
  88. return;
  89. }
  90. exports.playTorrent(magnet, cb);
  91. });
  92. }).end();
  93. }
  94. exports.isPlaying = player.isPlaying;
  95. player.onstop = function() {
  96. torrentStreamer.stop();
  97. httpStream.stop();
  98. exports.cleanupFiles.forEach(f => {
  99. try {
  100. fs.unlink(f, err => { if (err) console.trace(err) });
  101. } catch (err) {
  102. console.log(err.toString());
  103. }
  104. });
  105. exports.cleanupFiles = [];
  106. fsutil.rmdir(conf.tmpdir+"/torrent-stream");
  107. }