Nick Ficano
commited on
Commit
·
8da3a6a
1
Parent(s):
1bc4125
pep8
Browse files- pytube/models.py +2 -0
- pytube/utils.py +18 -21
pytube/models.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from __future__ import unicode_literals
|
| 2 |
from os.path import normpath, isfile
|
| 3 |
from os import remove
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
from __future__ import unicode_literals
|
| 4 |
from os.path import normpath, isfile
|
| 5 |
from os import remove
|
pytube/utils.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
import re
|
| 3 |
|
|
@@ -13,24 +15,22 @@ class FullPaths(argparse.Action):
|
|
| 13 |
|
| 14 |
|
| 15 |
def safe_filename(text, max_length=200):
|
| 16 |
-
"""
|
| 17 |
-
Sanitizes filenames for many operating systems.
|
| 18 |
|
| 19 |
-
|
| 20 |
-
text -- The unsanitized pending filename.
|
| 21 |
"""
|
| 22 |
-
#Quickly truncates long filenames.
|
| 23 |
truncate = lambda text: text[:max_length].rsplit(' ', 0)[0]
|
| 24 |
|
| 25 |
-
#Tidy up ugly formatted filenames.
|
| 26 |
text = text.replace('_', ' ')
|
| 27 |
text = text.replace(':', ' -')
|
| 28 |
|
| 29 |
-
#NTFS forbids filenames containing characters in range 0-31 (0x00-0x1F)
|
| 30 |
ntfs = [chr(i) for i in range(0, 31)]
|
| 31 |
|
| 32 |
-
#Removing these SHOULD make most filename safe for a wide range
|
| 33 |
-
#
|
| 34 |
paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
|
| 35 |
'\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']
|
| 36 |
|
|
@@ -40,13 +40,10 @@ def safe_filename(text, max_length=200):
|
|
| 40 |
|
| 41 |
|
| 42 |
def sizeof(bytes):
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
bytes(int): size of the file in bytes
|
| 48 |
-
Return:
|
| 49 |
-
(str): containing size with formatting.
|
| 50 |
"""
|
| 51 |
alternative = [
|
| 52 |
(1024 ** 5, ' PB'),
|
|
@@ -75,14 +72,14 @@ def print_status(progress, file_size, start):
|
|
| 75 |
This function - when passed as `on_progress` to `Video.download` - prints
|
| 76 |
out the current download progress.
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
start -- time when started
|
| 82 |
"""
|
| 83 |
|
| 84 |
percentDone = int(progress) * 100. / file_size
|
| 85 |
done = int(50 * progress / int(file_size))
|
| 86 |
-
stdout.write("\r [%s%s][%3.2f%%] %s at %s/s\r " % ('=' * done, ' ' *
|
| 87 |
-
sizeof(file_size), sizeof(
|
|
|
|
| 88 |
stdout.flush()
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
import argparse
|
| 4 |
import re
|
| 5 |
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def safe_filename(text, max_length=200):
|
| 18 |
+
"""Sanitizes filenames for many operating systems.
|
|
|
|
| 19 |
|
| 20 |
+
:params text: The unsanitized pending filename.
|
|
|
|
| 21 |
"""
|
| 22 |
+
# Quickly truncates long filenames.
|
| 23 |
truncate = lambda text: text[:max_length].rsplit(' ', 0)[0]
|
| 24 |
|
| 25 |
+
# Tidy up ugly formatted filenames.
|
| 26 |
text = text.replace('_', ' ')
|
| 27 |
text = text.replace(':', ' -')
|
| 28 |
|
| 29 |
+
# NTFS forbids filenames containing characters in range 0-31 (0x00-0x1F)
|
| 30 |
ntfs = [chr(i) for i in range(0, 31)]
|
| 31 |
|
| 32 |
+
# Removing these SHOULD make most filename safe for a wide range of
|
| 33 |
+
# operating systems.
|
| 34 |
paranoid = ['\"', '\#', '\$', '\%', '\'', '\*', '\,', '\.', '\/', '\:',
|
| 35 |
'\;', '\<', '\>', '\?', '\\', '\^', '\|', '\~', '\\\\']
|
| 36 |
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def sizeof(bytes):
|
| 43 |
+
"""Takes the size of file or folder in bytes and returns size formatted in
|
| 44 |
+
KB, MB, GB, TB or PB.
|
| 45 |
|
| 46 |
+
:params bytes: size of the file in bytes
|
|
|
|
|
|
|
|
|
|
| 47 |
"""
|
| 48 |
alternative = [
|
| 49 |
(1024 ** 5, ' PB'),
|
|
|
|
| 72 |
This function - when passed as `on_progress` to `Video.download` - prints
|
| 73 |
out the current download progress.
|
| 74 |
|
| 75 |
+
:params progress: The lenght of the currently downloaded bytes.
|
| 76 |
+
:params file_size: The total size of the video.
|
| 77 |
+
:params start: time when started
|
|
|
|
| 78 |
"""
|
| 79 |
|
| 80 |
percentDone = int(progress) * 100. / file_size
|
| 81 |
done = int(50 * progress / int(file_size))
|
| 82 |
+
stdout.write("\r [%s%s][%3.2f%%] %s at %s/s\r " % ('=' * done, ' ' *
|
| 83 |
+
(50 - done), percentDone, sizeof(file_size), sizeof(
|
| 84 |
+
progress // (clock() - start))))
|
| 85 |
stdout.flush()
|