<commit_before>#!/usr/bin/env node

var minimist = require('minimist');
var RendererFinder = require('renderer-finder');
var internalIp = require('internal-ip');
var getPort = require('get-port');
var fs = require('fs');
var http = require('http');
var rangeParser = require('range-parser');
var keypress = require('keypress');
var mime = require('mime');
var noop = function () {};
var opts = minimist(process.argv.slice(2));
var MediaRendererClient = require('upnp-mediarenderer-client');
var smfs = require('static-file-server');
var path = require('path');
//require.main === module


function DIDLMetadata(url, type, title, subtitle) {
  var DIDL = '';
  DIDL = DIDL + '<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sec="http://www.sec.co.kr/">';
  DIDL = DIDL + '  <item id="f-0" parentID="0" restricted="0">';
  DIDL = DIDL + '    <dc:title>' + title + '</dc:title>';
  if (subtitle){
    DIDL = DIDL + '    <sec:CaptionInfo sec:type="srt">' + subtitle + '</sec:CaptionInfo>';
    DIDL = DIDL + '    <sec:CaptionInfoEx sec:type="srt">' + subtitle + '</sec:CaptionInfoEx>';
    DIDL = DIDL + '    <res protocolInfo="http-get:*:text/srt:*">' + subtitle +'</res>';
  }
  DIDL = DIDL + '    <upnp:class>object.item.videoItem</upnp:class>';
  DIDL = DIDL + '    <res protocolInfo="http-get:*:' + type + ':DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000" sec:URIType="public">' + url +'</res>';
  DIDL = DIDL + '  </item>';
  DIDL = DIDL + '</DIDL-Lite>';
  return DIDL;
}

var discover = function (cb) {
  var finder = new RendererFinder();

  finder.findOne(function (err, info, msg) {
    clearTimeout(to);
    cb(err, msg.Location);
  });

  var to = setTimeout(function () {
    finder.stop();
    clearTimeout(to);
    cb(new Error('device not found'));
  }, 5000);
};


module.exports = {
  listRenderer: function(cb){
    var finder = new RendererFinder();

    finder.on('found', function(info, msg, desc){
      cb(undefined, info, msg, desc);
    });

    finder.on('error', function(err){
      cb(err);
    });

    return finder.start(true);
  },
  renderMedia: function(file, type, address, subtitle){
    var cli = null;
    
    if (address){
      startSender(null, address);
    }else{
      discover(startSender);
    }

    function startSender(err, loc) {
      if (err) {
        console.log(err);
        process.exit();
      }
      cli = new MediaRendererClient(loc);
      var subtitlePath = subtitle;
      var filePath = file;
      var stat = fs.statSync(filePath);
      stat.type = stat.type || mime.lookup(filePath);
      var firstHeaders = {
        'Access-Control-Allow-Origin': '*',
        'transferMode.dlna.org': 'Streaming',
        'contentFeatures.dlna.org': 'DLNA.ORG_PN=AVC_MP4_HP_HD_AAC;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000'
      };

      //If the is a subtitle to load, load that first and the the media
      //This is requires to provide the subtitle headers
      smfs.serve(subtitlePath ? subtitlePath : filePath, {
        headers: firstHeaders
      },function(err, firstUrl){
        if (subtitlePath){
          firstHeaders['CaptionInfo.sec'] = firstUrl;
          smfs.serve(filePath, {
            headers: firstHeaders
          }, function(err, secondUrl){
            runDLNA(secondUrl, firstUrl, stat);
          }, firstUrl);
        }else{
          runDLNA(firstUrl, null, stat);
        }
      });

      function runDLNA(fileUrl, subUrl, stat){

        keypress(process.stdin);
        process.stdin.setRawMode(true);
        process.stdin.resume();
        var isPlaying = false;

        cli.load(fileUrl, {
          autoplay: true,
          contentType: stat.type,
          metadata: DIDLMetadata(fileUrl, stat.type, path.basename(filePath), subUrl)
        }, function (err, result) {
          if (err) {
            console.log(err.message);
            //process.exit();
          }
          console.log('playing: ', path.basename(filePath));
          console.log('use your space-key to toggle between play and pause');
        });

        cli.on('playing', function () {
          isPlaying = true;
        });

        cli.on('paused', function () {
          isPlaying = false;
        });

        cli.on('stopped', function () {
          process.exit();
        });


        process.stdin.on('keypress', function (ch, key) {
          if (key && key.name && key.name === 'space') {
            if (isPlaying) {
              cli.pause();
            } else {
              cli.play();
            }
          }

          if (key && key.ctrl && key.name === 'c') {
            process.exit();
          }
        });
      }<commit_msg>Fix some process errors<commit_after>#!/usr/bin/env node

var minimist = require('minimist');
var RendererFinder = require('renderer-finder');
var internalIp = require('internal-ip');
var getPort = require('get-port');
var fs = require('fs');
var http = require('http');
var rangeParser = require('range-parser');
var keypress = require('keypress');
var mime = require('mime');
var noop = function () {};
var opts = minimist(process.argv.slice(2));
var MediaRendererClient = require('upnp-mediarenderer-client');
var smfs = require('static-file-server');
var path = require('path');
//require.main === module


function DIDLMetadata(url, type, title, subtitle) {
  var DIDL = '';
  DIDL = DIDL + '<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sec="http://www.sec.co.kr/">';
  DIDL = DIDL + '  <item id="f-0" parentID="0" restricted="0">';
  DIDL = DIDL + '    <dc:title>' + title + '</dc:title>';
  if (subtitle){
    DIDL = DIDL + '    <sec:CaptionInfo sec:type="srt">' + subtitle + '</sec:CaptionInfo>';
    DIDL = DIDL + '    <sec:CaptionInfoEx sec:type="srt">' + subtitle + '</sec:CaptionInfoEx>';
    DIDL = DIDL + '    <res protocolInfo="http-get:*:text/srt:*">' + subtitle +'</res>';
  }
  DIDL = DIDL + '    <upnp:class>object.item.videoItem</upnp:class>';
  DIDL = DIDL + '    <res protocolInfo="http-get:*:' + type + ':DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000" sec:URIType="public">' + url +'</res>';
  DIDL = DIDL + '  </item>';
  DIDL = DIDL + '</DIDL-Lite>';
  return DIDL;
}

var discover = function (cb) {
  var finder = new RendererFinder();

  finder.findOne(function (err, info, msg) {
    clearTimeout(to);
    cb(err, msg.Location);
  });

  var to = setTimeout(function () {
    finder.stop();
    clearTimeout(to);
    cb(new Error('device not found'));
  }, 5000);
};


module.exports = {
  listRenderer: function(cb){
    var finder = new RendererFinder();

    finder.on('found', function(info, msg, desc){
      cb(undefined, info, msg, desc);
    });

    finder.on('error', function(err){
      cb(err);
    });

    return finder.start(true);
  },
  renderMedia: function(file, type, address, subtitle){
    var cli = null;

    if (address){
      startSender(null, address);
    }else{
      discover(startSender);
    }

    function startSender(err, loc) {
      if (err) {
        console.log(err);
        if (require.main === module)
          process.exit();
      }
      cli = new MediaRendererClient(loc);
      var subtitlePath = subtitle;
      var filePath = file;
      var stat = fs.statSync(filePath);
      stat.type = stat.type || mime.lookup(filePath);
      var firstHeaders = {
        'Access-Control-Allow-Origin': '*',
        'transferMode.dlna.org': 'Streaming',
        'contentFeatures.dlna.org': 'DLNA.ORG_PN=AVC_MP4_HP_HD_AAC;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000'
      };

      //If the is a subtitle to load, load that first and the the media
      //This is requires to provide the subtitle headers
      smfs.serve(subtitlePath ? subtitlePath : filePath, {
        headers: firstHeaders
      },function(err, firstUrl){
        if (subtitlePath){
          firstHeaders['CaptionInfo.sec'] = firstUrl;
          smfs.serve(filePath, {
            headers: firstHeaders
          }, function(err, secondUrl){
            runDLNA(secondUrl, firstUrl, stat);
          }, firstUrl);
        }else{
          runDLNA(firstUrl, null, stat);
        }
      });

      function runDLNA(fileUrl, subUrl, stat){

        if (require.main === module){
          keypress(process.stdin);
          process.stdin.setRawMode(true);
          process.stdin.resume();
        }
        var isPlaying = false;

        cli.load(fileUrl, {
          autoplay: true,
          contentType: stat.type,
          metadata: DIDLMetadata(fileUrl, stat.type, path.basename(filePath), subUrl)
        }, function (err, result) {
          if (err) {
            console.log(err.message);
            //process.exit();
          }
          console.log('playing: ', path.basename(filePath));
          console.log('use your space-key to toggle between play and pause');
        });

        cli.on('playing', function () {
          isPlaying = true;
        });

        cli.on('paused', function () {
          isPlaying = false;
        });

        cli.on('stopped', function () {
          if (require.main === module)
            process.exit();
        });

        if (require.main === module){
          process.stdin.on('keypress', function (ch, key) {
            if (key && key.name && key.name === 'space') {
              if (isPlaying) {
                cli.pause();
              } else {
                cli.play();
              }
            }

            if (key && key.ctrl && key.name === 'c') {
              process.exit();
            }
          });
        }
      }
{'fraction_non_alphanumeric': 0.1634351842452543, 'fraction_numerical': 0.01725713125571008, 'mean_word_length': 2.487433628318584, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2778631', 'n_tokens_mistral': 3420, 'n_tokens_neox': 3188, 'n_words': 643}