libs/bin/ytd/ytd.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 2.57

Size

Lines of Code 85
Function Count 7

Duplication

Duplicated Lines 85
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 56
dl 85
loc 85
rs 10
c 0
b 0
f 0
wmc 18
mnd 11
bc 11
fnc 7
bpm 1.5713
cpm 2.5714
noi 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
var fs = require('fs');
2
var YoutubeMp3Downloader = require('youtube-mp3-downloader');
3
4
var file_put_contents = function(filename, content) {
5
  var typedata = typeof content;
6
  if (Array.isArray(content)) {
7
    typedata = 'array';
8
  }
9
  if (typedata == 'object' || Array.isArray(content)) {
10
    content = JSON.stringify(content, null, 4);
11
  }
12
  fs.writeFile(filename, content, {
13
    flag: 'w'
14
  }, function(err) {
15
    if (err)
16
      return console.error(err);
17
    fs.readFile(filename, 'utf-8', function(err, data) {
18
      if (err)
19
        return console.error(err);
20
      console.log(data);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
21
    });
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
22
  });
23
}
24
var create_folder = function(dir) {
25
  if (!fs.existsSync(dir)) {
26
    fs.mkdirSync(dir, {
27
      recursive: true
28
    });
29
  }
30
}
31
32
var arg = '';
33
var VID;
34
for (let j = 0; j < process.argv.length; j++) {
35
  arg = process.argv[j];
36
}
37
38
if (arg == 'install') {
39
  create_folder('log');
40
  create_folder('log/progress');
41
  create_folder('log/error');
42
  create_folder('log/progress');
43
  create_folder('mp3');
44
  return;
45
}
46
47
var YD = new YoutubeMp3Downloader({
48
  'ffmpegPath': 'D:\\bin\\ffmpeg\\bin\\ffmpeg.exe', // Where is the FFmpeg binary located?
49
  'outputPath': 'mp3/', // Where should the downloaded and encoded files be stored?
50
  'youtubeVideoQuality': 'highest', // What video quality should be used?
51
  'queueParallelism': 2, // How many parallel downloads/encodes should be started?
52
  'progressTimeout': 2000 // How long should be the interval of the progress reports
53
});
54
55
//Download video and save as MP3 file
56
YD.download(arg);
57
YD.on('finished', function(err, data) {
58
  //console.log(JSON.stringify(data));
59
  var file_finish = 'finish.json';
60
  if (VID) {
0 ignored issues
show
Bug introduced by
The variable VID seems to be never initialized.
Loading history...
61
    file_finish = `${VID}.json`;
62
  }
63
  if (data.hasOwnProperty('videoId')) {
64
    file_finish = `${data.videoId}.json`;
65
  }
66
  file_put_contents(`log/finish/${file_finish}`, data);
67
});
68
69
YD.on('error', function(error) {
70
  //console.log(error);
71
  var file_error = 'error.json';
72
  if (VID) {
0 ignored issues
show
Bug introduced by
The variable VID seems to be never initialized.
Loading history...
73
    file_error = `${VID}.json`;
74
  }
75
  file_put_contents(`log/error/${file_error}`, error);
76
});
77
78
YD.on('progress', function(progress) {
79
  //console.log(JSON.stringify(progress));
80
  var file_progress = 'progress.json';
81
  if (progress.hasOwnProperty('videoId')) {
82
    file_progress = `${progress.videoId}.json`;
83
  }
84
  file_put_contents(`log/progress/${file_progress}`, progress);
85
});