Richard Borcsik commited on
Commit
7d6d1a7
·
1 Parent(s): d892f3a

Added error handling.

Browse files
Files changed (1) hide show
  1. pytube/cli.py +27 -4
pytube/cli.py CHANGED
@@ -1,7 +1,9 @@
 
1
  import argparse
2
 
3
  from .api import YouTube
4
  from .utils import print_status
 
5
 
6
 
7
  def _main():
@@ -21,7 +23,11 @@ def _main():
21
  args = parser.parse_args()
22
 
23
  yt = YouTube()
24
- yt.url = args.url
 
 
 
 
25
 
26
  if args.filename:
27
  yt.filename = args.filename
@@ -29,18 +35,35 @@ def _main():
29
  if args.ext and args.res:
30
  # There's only ope video that matches both so get it
31
  vid = yt.get(args.ext, args.res)
 
 
 
 
 
32
  elif args.ext:
33
  # There are several videos with the same extension
34
  videos = yt.filter(extension=args.ext)
 
 
 
 
35
  # Select the highest resolution one
36
  vid = max(videos)
37
  elif args.res:
38
- # There are several videos with the same extension
39
- videos = yt.filter(resolution=args.res)
 
 
 
 
40
  # Select the highest resolution one
41
  vid = max(videos)
42
  else:
43
  # If nothing is specified get the highest resolution one
44
  vid = max(yt.videos)
45
 
46
- vid.download(path=args.path, on_progress=print_status)
 
 
 
 
 
1
+ import sys
2
  import argparse
3
 
4
  from .api import YouTube
5
  from .utils import print_status
6
+ from .exceptions import YouTubeError
7
 
8
 
9
  def _main():
 
23
  args = parser.parse_args()
24
 
25
  yt = YouTube()
26
+ try:
27
+ yt.url = args.url
28
+ except YouTubeError:
29
+ print "Incorrect video URL."
30
+ sys.exit(1)
31
 
32
  if args.filename:
33
  yt.filename = args.filename
 
35
  if args.ext and args.res:
36
  # There's only ope video that matches both so get it
37
  vid = yt.get(args.ext, args.res)
38
+ # Check if there's a video returned
39
+ if not vid:
40
+ print "There's no video with the specified format/resolution combination."
41
+ sys.exit(1)
42
+
43
  elif args.ext:
44
  # There are several videos with the same extension
45
  videos = yt.filter(extension=args.ext)
46
+ # Check if we have a video
47
+ if not videos:
48
+ print "There are no videos in the specified format."
49
+ sys.exit(1)
50
  # Select the highest resolution one
51
  vid = max(videos)
52
  elif args.res:
53
+ # There might be several videos in the same resolution
54
+ videos = yt.filter(res=args.res)
55
+ # Check if we have a video
56
+ if not videos:
57
+ print "There are no videos in the specified in the specified resolution."
58
+ sys.exit(1)
59
  # Select the highest resolution one
60
  vid = max(videos)
61
  else:
62
  # If nothing is specified get the highest resolution one
63
  vid = max(yt.videos)
64
 
65
+ try:
66
+ vid.download(path=args.path, on_progress=print_status)
67
+ except KeyboardInterrupt:
68
+ print "Download interrupted."
69
+ sys.exit(1)