nficano commited on
Commit
49627f0
·
1 Parent(s): 5d5b970

added unit tests

Browse files
pytube/cli.py CHANGED
@@ -3,6 +3,7 @@
3
  from __future__ import print_function
4
 
5
  import argparse
 
6
  import os
7
  import sys
8
 
@@ -24,16 +25,37 @@ def main():
24
  'available to download'
25
  ),
26
  )
 
 
 
 
 
27
  args = parser.parse_args()
28
  if not args.url:
29
  parser.print_help()
30
  sys.exit(1)
31
  if args.list:
32
  display_streams(args.url)
 
 
33
  elif args.itag:
34
  download(args.url, args.itag)
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def get_terminal_size():
38
  """Return the terminal size in rows and columns."""
39
  rows, columns = os.popen('stty size', 'r').read().split()
 
3
  from __future__ import print_function
4
 
5
  import argparse
6
+ import json
7
  import os
8
  import sys
9
 
 
25
  'available to download'
26
  ),
27
  )
28
+ parser.add_argument(
29
+ '--build-debug-report', action='store_true', help=(
30
+ 'Save the html and js to disk'
31
+ ),
32
+ )
33
  args = parser.parse_args()
34
  if not args.url:
35
  parser.print_help()
36
  sys.exit(1)
37
  if args.list:
38
  display_streams(args.url)
39
+ elif args.build_debug_report:
40
+ build_debug_report(args.url)
41
  elif args.itag:
42
  download(args.url, args.itag)
43
 
44
 
45
+ def build_debug_report(url):
46
+ yt = YouTube(url)
47
+ fp = os.path.join(
48
+ os.getcwd(),
49
+ 'yt-video-{yt.video_id}.json'.format(yt=yt),
50
+ )
51
+ with open(fp, 'w') as fh:
52
+ fh.write(json.dumps({
53
+ 'js': yt.js,
54
+ 'watch_html': yt.watch_html,
55
+ 'video_info': yt.vid_info,
56
+ }))
57
+
58
+
59
  def get_terminal_size():
60
  """Return the terminal size in rows and columns."""
61
  rows, columns = os.popen('stty size', 'r').read().split()
setup.cfg CHANGED
@@ -12,3 +12,7 @@ description-file = README.md
12
  [bumpversion:file:setup.py]
13
 
14
  [bumpversion:file:pytube/__init__.py]
 
 
 
 
 
12
  [bumpversion:file:setup.py]
13
 
14
  [bumpversion:file:pytube/__init__.py]
15
+
16
+
17
+ [coverage:run]
18
+ source = pytube
tests/__init__.py CHANGED
@@ -0,0 +1,2 @@
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Package init | pydocstyle ignore isn't working :/."""
tests/conftest.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Reusable dependency injected testing components."""
3
+ import json
4
+ import os
5
+
6
+ import pytest
7
+
8
+ from pytube import YouTube
9
+
10
+
11
+ @pytest.fixture
12
+ def gangnam_style():
13
+ """Youtube instance initialized with video id 9bZkp7q19f0."""
14
+ cur_dir = os.path.dirname(os.path.realpath(__file__))
15
+ fp = os.path.join(cur_dir, 'mocks', 'yt-video-9bZkp7q19f0.json')
16
+ video = None
17
+ with open(fp, 'r') as fh:
18
+ video = json.loads(fh.read())
19
+ yt = YouTube(
20
+ url='https://www.youtube.com/watch?v=9bZkp7q19f0',
21
+ defer_init=True,
22
+ )
23
+ yt.watch_html = video['watch_html']
24
+ yt.js = video['js']
25
+ yt.vid_info = video['video_info']
26
+ yt.init()
27
+ return yt
tests/mocks/yt-video-9bZkp7q19f0.json ADDED
The diff for this file is too large to render. See raw diff
 
tests/test_extract.py CHANGED
@@ -1,10 +1,5 @@
1
  # -*- coding: utf-8 -*-
2
- """
3
- tests.test_extract
4
- ~~~~~~~~~~~~~~~~~~
5
-
6
- Unit tests for the :module:`extract <extract>` module.
7
- """
8
  from pytube import extract
9
 
10
 
@@ -17,4 +12,18 @@ def test_extract_video_id():
17
  def test_extract_watch_url():
18
  video_id = '9bZkp7q19f0'
19
  watch_url = extract.watch_url(video_id)
20
- assert watch_url == 'https://www.youtube.com/watch?v=9bZkp7q19f0'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
+ """Unit tests for the :module:`extract <extract>` module."""
 
 
 
 
 
3
  from pytube import extract
4
 
5
 
 
12
  def test_extract_watch_url():
13
  video_id = '9bZkp7q19f0'
14
  watch_url = extract.watch_url(video_id)
15
+ assert watch_url == 'https://youtube.com/watch?v=9bZkp7q19f0'
16
+
17
+
18
+ def test_info_url(gangnam_style):
19
+ video_info_url = extract.video_info_url(
20
+ video_id=gangnam_style.video_id,
21
+ watch_url=gangnam_style.watch_url,
22
+ watch_html=gangnam_style.watch_html,
23
+ )
24
+ expected = (
25
+ 'https://youtube.com/get_video_info?video_id=9bZkp7q19f0&el=%24el'
26
+ '&ps=default&eurl=https%253A%2F%2Fyoutube.com%2Fwatch%253Fv%'
27
+ '253D9bZkp7q19f0&hl=en_US&t=%252C%2522t%2522%253A%25221%2522'
28
+ )
29
+ assert video_info_url == expected