mursts
commited on
Commit
·
5cccd0e
1
Parent(s):
aca9cc7
fixed python 3 compatibility
Browse files- pytube/api.py +8 -6
- pytube/models.py +17 -8
- pytube/utils.py +1 -1
pytube/api.py
CHANGED
|
@@ -3,9 +3,13 @@ from __future__ import unicode_literals
|
|
| 3 |
from .exceptions import MultipleObjectsReturned, YouTubeError
|
| 4 |
from .models import Video
|
| 5 |
from .utils import safe_filename
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
import re
|
| 11 |
|
|
@@ -262,6 +266,4 @@ class YouTube(object):
|
|
| 262 |
attr = YT_ENCODING.get(itag, None)
|
| 263 |
if not attr:
|
| 264 |
return itag, None
|
| 265 |
-
|
| 266 |
-
map(lambda k, v: data.update({k: v}), YT_ENCODING_KEYS, attr)
|
| 267 |
-
return itag, data
|
|
|
|
| 3 |
from .exceptions import MultipleObjectsReturned, YouTubeError
|
| 4 |
from .models import Video
|
| 5 |
from .utils import safe_filename
|
| 6 |
+
try:
|
| 7 |
+
from urllib import urlencode
|
| 8 |
+
from urllib2 import urlopen
|
| 9 |
+
from urlparse import urlparse, parse_qs, unquote
|
| 10 |
+
except ImportError:
|
| 11 |
+
from urllib.parse import urlencode, urlparse, parse_qs, unquote
|
| 12 |
+
from urllib.request import urlopen
|
| 13 |
|
| 14 |
import re
|
| 15 |
|
|
|
|
| 266 |
attr = YT_ENCODING.get(itag, None)
|
| 267 |
if not attr:
|
| 268 |
return itag, None
|
| 269 |
+
return itag, dict(zip(YT_ENCODING_KEYS, attr))
|
|
|
|
|
|
pytube/models.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
from __future__ import unicode_literals
|
| 2 |
from os.path import normpath, isfile
|
| 3 |
from os import remove
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
from sys import exit
|
| 6 |
|
| 7 |
|
|
@@ -48,7 +51,8 @@ class Video(object):
|
|
| 48 |
|
| 49 |
# Check for conflicting filenames
|
| 50 |
if isfile(fullpath):
|
| 51 |
-
print
|
|
|
|
| 52 |
exit(1)
|
| 53 |
|
| 54 |
response = urlopen(self.url)
|
|
@@ -58,8 +62,9 @@ class Video(object):
|
|
| 58 |
self._bytes_received = 0
|
| 59 |
try:
|
| 60 |
with open(fullpath, 'wb') as dst_file:
|
| 61 |
-
|
| 62 |
-
print
|
|
|
|
| 63 |
|
| 64 |
while True:
|
| 65 |
self._buffer = response.read(chunk_size)
|
|
@@ -75,16 +80,20 @@ class Video(object):
|
|
| 75 |
|
| 76 |
# Catch possible exceptions occurring during download
|
| 77 |
except IOError:
|
| 78 |
-
print
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
exit(2)
|
| 81 |
|
| 82 |
except BufferError:
|
| 83 |
-
print
|
|
|
|
| 84 |
exit(1)
|
| 85 |
|
| 86 |
except KeyboardInterrupt:
|
| 87 |
-
print
|
|
|
|
| 88 |
remove(fullpath)
|
| 89 |
exit(1)
|
| 90 |
|
|
|
|
| 1 |
from __future__ import unicode_literals
|
| 2 |
from os.path import normpath, isfile
|
| 3 |
from os import remove
|
| 4 |
+
try:
|
| 5 |
+
from urllib2 import urlopen
|
| 6 |
+
except ImportError:
|
| 7 |
+
from urllib.request import urlopen
|
| 8 |
from sys import exit
|
| 9 |
|
| 10 |
|
|
|
|
| 51 |
|
| 52 |
# Check for conflicting filenames
|
| 53 |
if isfile(fullpath):
|
| 54 |
+
print("\n\nError: Conflicting filename:'{}'.\n\n".format(
|
| 55 |
+
self.filename))
|
| 56 |
exit(1)
|
| 57 |
|
| 58 |
response = urlopen(self.url)
|
|
|
|
| 62 |
self._bytes_received = 0
|
| 63 |
try:
|
| 64 |
with open(fullpath, 'wb') as dst_file:
|
| 65 |
+
# Print downloading message
|
| 66 |
+
print("\nDownloading: '{0}.{1}' (Bytes: {2})\n\n".format(
|
| 67 |
+
self.filename, self.extension, file_size))
|
| 68 |
|
| 69 |
while True:
|
| 70 |
self._buffer = response.read(chunk_size)
|
|
|
|
| 80 |
|
| 81 |
# Catch possible exceptions occurring during download
|
| 82 |
except IOError:
|
| 83 |
+
print("\n\nError: Failed to open file.\n" \
|
| 84 |
+
"Check that: ('{0}'), is a valid pathname.\n\n" \
|
| 85 |
+
"Or that ('{1}.{2}') is a valid filename.\n\n".format(
|
| 86 |
+
path, self.filename, self.extension))
|
| 87 |
exit(2)
|
| 88 |
|
| 89 |
except BufferError:
|
| 90 |
+
print("\n\nError: Failed on writing buffer.\n" \
|
| 91 |
+
"Failed to write video to file.\n\n")
|
| 92 |
exit(1)
|
| 93 |
|
| 94 |
except KeyboardInterrupt:
|
| 95 |
+
print("\n\nInterrupt signal given.\nDeleting incomplete video" \
|
| 96 |
+
"('{0}.{1}').\n\n".format(self.filename, self.extension))
|
| 97 |
remove(fullpath)
|
| 98 |
exit(1)
|
| 99 |
|
pytube/utils.py
CHANGED
|
@@ -41,4 +41,4 @@ def print_status(progress, file_size):
|
|
| 41 |
percent = progress * 100. / file_size
|
| 42 |
status = r"{0:10d} [{1:3.2f}%]".format(progress, percent)
|
| 43 |
status = status + chr(8) * (len(status) + 1)
|
| 44 |
-
print
|
|
|
|
| 41 |
percent = progress * 100. / file_size
|
| 42 |
status = r"{0:10d} [{1:3.2f}%]".format(progress, percent)
|
| 43 |
status = status + chr(8) * (len(status) + 1)
|
| 44 |
+
print(status,)
|