mursts commited on
Commit
5cccd0e
·
1 Parent(s): aca9cc7

fixed python 3 compatibility

Browse files
Files changed (3) hide show
  1. pytube/api.py +8 -6
  2. pytube/models.py +17 -8
  3. 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
- from urllib import urlencode
7
- from urllib2 import urlopen
8
- from urlparse import urlparse, parse_qs, unquote
 
 
 
 
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
- data = {}
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
- from urllib2 import urlopen
 
 
 
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 "\n\nError: Conflicting filename: '{}'.\n\n".format(self.filename)
 
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
- # Print downloading message
62
- print "\nDownloading: '{0}.{1}' (Bytes: {2})\n\n".format(self.filename, self.extension, file_size)
 
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 """\n\nError: Failed to open file.\nCheck that: ('{0}'), is a valid pathname.
79
- \nOr that ('{1}.{2}') is a valid filename.\n\n""".format(path, self.filename, self.extension)
 
 
80
  exit(2)
81
 
82
  except BufferError:
83
- print "\n\nError: Failed on writing buffer.\nFailed to write video to file.\n\n"
 
84
  exit(1)
85
 
86
  except KeyboardInterrupt:
87
- print "\n\nInterrupt signal given.\nDeleting incomplete video ('{0}.{1}').\n\n".format(self.filename, self.extension)
 
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 status,
 
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,)