Nick Ficano commited on
Commit
aee8e93
·
1 Parent(s): 27b0198

pep8, removed calls to deprecated methods.

Browse files
Files changed (1) hide show
  1. scripts/pytubectl +33 -31
scripts/pytubectl CHANGED
@@ -1,50 +1,50 @@
1
  #!/usr/bin/env python
 
2
  from __future__ import print_function
 
 
 
3
 
4
  from pytube import YouTube
5
- from pytube.utils import print_status
6
- from pytube.utils import FullPaths
7
  from pytube.exceptions import YouTubeError
8
  from pprint import pprint
9
 
10
- import sys
11
- import os
12
- import argparse
13
-
14
 
15
- def _main():
16
  parser = argparse.ArgumentParser(description='YouTube video downloader')
17
- parser.add_argument("url", help="The URL of the Video to be downloaded")
18
- parser.add_argument("--extension", "-e",
19
- help="The requested format of the video", dest="ext")
20
- parser.add_argument("--resolution", "-r",
21
- help="The requested resolution", dest="res")
22
- parser.add_argument("--path", "-p", action=FullPaths,
23
- default=os.getcwd(),
24
- help="The path to save the video to.", dest="path")
25
- parser.add_argument("--filename", "-f",
26
- dest="filename",
27
- help=("The filename, without extension, "
28
- "to save the video in."))
29
 
30
  args = parser.parse_args()
31
 
32
- yt = YouTube()
33
  try:
34
- yt.url = args.url
35
- res_formts = yt.videos
36
- res_formts = ["%s %s" % (str(res_formts[index].extension), str(res_formts[index].resolution)) for index, video in enumerate(res_formts)]
 
 
 
37
  except YouTubeError:
38
  print("Incorrect video URL.")
39
  sys.exit(1)
40
 
41
  if args.filename:
42
- yt.filename = args.filename
43
 
44
  if args.ext or args.res:
45
  if not all([args.ext, args.res]):
46
- print("\nMake sure you give either of the below specified format/resolution combination.\n")
47
- pprint(res_formts)
 
48
  sys.exit(1)
49
 
50
  if args.ext and args.res:
@@ -52,8 +52,9 @@ def _main():
52
  vid = yt.get(args.ext, args.res)
53
  # Check if there's a video returned
54
  if not vid:
55
- print("\nThere's no video with the specified format/resolution combination.\n")
56
- pprint(res_formts)
 
57
  sys.exit(1)
58
 
59
  elif args.ext:
@@ -70,13 +71,14 @@ def _main():
70
  videos = yt.filter(res=args.res)
71
  # Check if we have a video
72
  if not videos:
73
- print("There are no videos in the specified in the specified resolution.")
 
74
  sys.exit(1)
75
  # Select the highest resolution one
76
  vid = max(videos)
77
  else:
78
  # If nothing is specified get the highest resolution one
79
- vid = max(yt.videos)
80
 
81
  try:
82
  vid.download(path=args.path, on_progress=print_status)
@@ -85,4 +87,4 @@ def _main():
85
  sys.exit(1)
86
 
87
  if __name__ == '__main__':
88
- _main()
 
1
  #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
  from __future__ import print_function
4
+ import sys
5
+ import os
6
+ import argparse
7
 
8
  from pytube import YouTube
9
+ from pytube.utils import print_status, FullPaths
 
10
  from pytube.exceptions import YouTubeError
11
  from pprint import pprint
12
 
 
 
 
 
13
 
14
+ def main():
15
  parser = argparse.ArgumentParser(description='YouTube video downloader')
16
+ parser.add_argument("url", help=(
17
+ "The URL of the Video to be downloaded"))
18
+ parser.add_argument("--extension", "-e", dest="ext", help=(
19
+ "The requested format of the video"))
20
+ parser.add_argument("--resolution", "-r", dest="res", help=(
21
+ "The requested resolution"))
22
+ parser.add_argument("--path", "-p", action=FullPaths, default=os.getcwd(),
23
+ dest="path", help=("The path to save the video to."))
24
+ parser.add_argument("--filename", "-f", dest="filename", help=(
25
+ "The filename, without extension, to save the video in."))
 
 
26
 
27
  args = parser.parse_args()
28
 
 
29
  try:
30
+ yt = YouTube(args.url)
31
+ videos = []
32
+ for i, video in enumerate(yt.get_videos()):
33
+ ext = video.extension
34
+ res = video.resolution
35
+ videos.append("{} {}".format(ext, res))
36
  except YouTubeError:
37
  print("Incorrect video URL.")
38
  sys.exit(1)
39
 
40
  if args.filename:
41
+ yt.set_filename(args.filename)
42
 
43
  if args.ext or args.res:
44
  if not all([args.ext, args.res]):
45
+ print("Make sure you give either of the below specified "
46
+ "format/resolution combination.")
47
+ pprint(videos)
48
  sys.exit(1)
49
 
50
  if args.ext and args.res:
 
52
  vid = yt.get(args.ext, args.res)
53
  # Check if there's a video returned
54
  if not vid:
55
+ print("There's no video with the specified format/resolution "
56
+ "combination.")
57
+ pprint(videos)
58
  sys.exit(1)
59
 
60
  elif args.ext:
 
71
  videos = yt.filter(res=args.res)
72
  # Check if we have a video
73
  if not videos:
74
+ print("There are no videos in the specified in the specified "
75
+ "resolution.")
76
  sys.exit(1)
77
  # Select the highest resolution one
78
  vid = max(videos)
79
  else:
80
  # If nothing is specified get the highest resolution one
81
+ vid = max(yt.get_videos())
82
 
83
  try:
84
  vid.download(path=args.path, on_progress=print_status)
 
87
  sys.exit(1)
88
 
89
  if __name__ == '__main__':
90
+ main()