randydev commited on
Commit
3866897
·
verified ·
1 Parent(s): be7f7de

Create youtube/util.js

Browse files
Files changed (1) hide show
  1. lib/youtube/util.js +37 -0
lib/youtube/util.js ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import crypto from 'crypto';
2
+
3
+ export function parseFileSize(size) {
4
+ const sized = parseFloat(size);
5
+ return (isNaN(sized) ? 0 : sized) * (
6
+ /GB/i.test(size)
7
+ ? 1000000
8
+ : /MB/i.test(size)
9
+ ? 1000
10
+ : /KB/i.test(size)
11
+ ? 1
12
+ : /bytes?/i.test(size)
13
+ ? 0.001
14
+ : /B/i.test(size)
15
+ ? 0.1
16
+ : 0
17
+ );
18
+ }
19
+
20
+ const SUFFIX = 'f24c8c73d48b7686ed11a3bf97983f6f7eb6395f19268184aae742e93683c00c';
21
+ export function generateHash(url) {
22
+ const hash = crypto.createHash('sha256');
23
+ const data = url + Date.now() + SUFFIX;
24
+ hash.update(data);
25
+ return hash.digest('hex');
26
+ }
27
+
28
+ export function time2Number(time) {
29
+ let [hours, minutes, seconds] = time.split(':').map(Number);
30
+ if (!seconds) { // '00:07'
31
+ [minutes, seconds] = [hours, minutes];
32
+ hours = 0;
33
+ }
34
+ return hours * 3600
35
+ + minutes * 60
36
+ + seconds * 1;
37
+ }