<commit_before>// import store from '../store'
const {ipcMain} = require('electron')
const WebTorrent = require('webtorrent')
const client = new WebTorrent()
// const mp3Duration = require('mp3-duration')
// const async = require('async')
var server = null
var torrentId = null
var filesToPlay = []
var currentFileOnPlay = null

ipcMain.on('addMagnet', (event, arg) => {
  client.add(arg, function (torrent) {
    var files = []
    torrent.files.forEach(function (file) {
      if (file.name.indexOf('.mp3') !== -1 || file.name.indexOf('.ogg') !== -1 ||
          file.name.indexOf('.flac') !== -1) {
        files.push({
          title: file.name.replace('.mp3', ''),
          torrent: torrent.infoHash,
          index: torrent.files.indexOf(file),
          duration: '--:--',
          playing: 'false'
        })
        filesToPlay.push({
          title: file.name.replace('.mp3', ''),
          torrent: torrent.infoHash,
          index: torrent.files.indexOf(file)
        })
      }
    })

    event.sender.send('updateSongs', files)
  })
})

ipcMain.on('requestPlay', (event, args) => {
  if (server === null) {
    server = client.get(args[0]).createServer()
    server.listen(9999)
    torrentId = args[0]
    event.sender.send('canPlay', args)
  } else if (torrentId !== args[0]) {
    server.close()
    server = client.get(args[0]).createServer()
    server.listen(9999)
    torrentId = args[0]
    event.sender.send('canPlay', args)
  } else {
    event.sender.send('canPlay', args)
  }

  currentFileOnPlay = {
    title: args[2],
    torrent: args[0],
    index: args[1]
  }
})

ipcMain.on('playEnded', (event, args) => {
  var title
  var newIndex = args[1] + 1
  console.log(filesToPlay[filesToPlay.indexOf(currentFileOnPlay) + 1])
  if (filesToPlay[filesToPlay.indexOf(currentFileOnPlay) + 1].torrent === args[0]) {
    title = filesToPlay[filesToPlay.indexOf(currentFileOnPlay) + 1].title
    currentFileOnPlay = {
      title: title,
      torrent: args[0],
      index: args[1]
    }
    event.sender.send('canPlay', [args[0], newIndex, title])
  } else {
    currentFileOnPlay = filesToPlay[filesToPlay.indexOf(currentFileOnPlay) + 1]
    torrentId = currentFileOnPlay.torrent
    server.close()
    server = client.get(torrentId).createServer()
    server.listen(9999)
    event.sender.send('canPlay', [torrentId, currentFileOnPlay.index, currentFileOnPlay.title])
  }
})

ipcMain.on('playPrevious', (event, args) => {
  var title
  var newIndex = args[1] - 1
  if (torrentId !== args[0]) {
    server.close()
    server = client.get(args[0]).createServer()
    server.listen(9999)
    title = client.get(args[0]).files[args[1]].name
    event.sender.send('canPlay', args.concat([title]))
  } else {
    title = client.get(args[0]).files[args[1] + 1].name
    console.log(args.concat([title]))
    event.sender.send('canPlay', [args[0], newIndex, title])
  }
})

ipcMain.on('updateTorrentStatus', (event, args) => {
  var status = []
  client.torrents.forEach(function (torrent) {
    status.push({
      title: torrent.name,
      hash: torrent.infoHash,
      progress: Math.floor(torrent.progress * 100)
    })
  })

  event.sender.send('newStatus', status)
})

module.exports = client<commit_msg>request play works now perfectly and it's being used for next and previous requests<commit_after>// import store from '../store'
const {ipcMain} = require('electron')
const WebTorrent = require('webtorrent')
const client = new WebTorrent()
// const mp3Duration = require('mp3-duration')
// const async = require('async')
var server = null
var torrentId = null
var filesToPlay = []
var currentFileOnPlay = null

ipcMain.on('addMagnet', (event, arg) => {
  // TODO: Code duplicated.
  client.add(arg, function (torrent) {
    var files = []
    torrent.files.forEach(function (file) {
      if (file.name.indexOf('.mp3') !== -1 || file.name.indexOf('.ogg') !== -1 ||
          file.name.indexOf('.flac') !== -1) {
        files.push({
          title: file.name.replace('.mp3', ''),
          torrent: torrent.infoHash,
          index: torrent.files.indexOf(file),
          duration: '--:--',
          playing: 'false'
        })
        filesToPlay.push({
          title: file.name.replace('.mp3', ''),
          torrent: torrent.infoHash,
          index: torrent.files.indexOf(file)
        })
      }
    })

    event.sender.send('updateSongs', files)
  })
})

ipcMain.on('requestPlay', (event, song) => {
  console.log(song)
  if (server === null) {
    server = client.get(song.torrent).createServer()
    server.listen(9999)
    torrentId = song.torrent
    event.sender.send('canPlay', song)
  } else if (torrentId !== song.torrent) {
    server.close()
    server = client.get(song.torrent).createServer()
    server.listen(9999)
    torrentId = song.torrent
    event.sender.send('canPlay', song)
  } else {
    event.sender.send('canPlay', song)
  }
})

ipcMain.on('updateTorrentStatus', (event, args) => {
  var status = []
  client.torrents.forEach(function (torrent) {
    status.push({
      title: torrent.name,
      hash: torrent.infoHash,
      progress: Math.floor(torrent.progress * 100)
    })
  })

  event.sender.send('newStatus', status)
})

ipcMain.on('dragedTorrent', (event, args) => {
  args.forEach(function (file) {
    client.add(file, function (torrent) {
      var files = []
      torrent.files.forEach(function (file) {
        if (file.name.indexOf('.mp3') !== -1 || file.name.indexOf('.ogg') !== -1 ||
            file.name.indexOf('.flac') !== -1) {
          files.push({
            title: file.name.replace('.mp3', ''),
            torrent: torrent.infoHash,
            index: torrent.files.indexOf(file),
            duration: '--:--',
            playing: 'false'
          })
          filesToPlay.push({
            title: file.name.replace('.mp3', ''),
            torrent: torrent.infoHash,
            index: torrent.files.indexOf(file)
          })
        }
      })

      event.sender.send('updateSongs', files)
    })
  })
})
module.exports = client
{'fraction_non_alphanumeric': 0.1702268587514489, 'fraction_numerical': 0.01324722636198046, 'mean_word_length': 2.805923125393825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2045415', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1966, 'n_words': 363}