Skip to content

Commit

Permalink
add function to download from youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Dec 28, 2017
1 parent dcf4437 commit c884e30
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 9 deletions.
3 changes: 3 additions & 0 deletions _doc/sphinxdoc/source/conf.py
Expand Up @@ -74,4 +74,7 @@

epkg_dictionary['deep learning'] = 'https://en.wikipedia.org/wiki/Deep_learning'
epkg_dictionary['fcn'] = 'https://github.com/wkentaro/fcn'
epkg_dictionary['pytube'] = 'https://github.com/nficano/pytube'
epkg_dictionary['SciTe'] = 'http://www.scintilla.org/SciTE.html'
epkg_dictionary['scratch'] = "https://scratch.mit.edu/"
epkg_dictionary['youtube'] = "https://www.youtube.com/"
4 changes: 2 additions & 2 deletions _doc/sphinxdoc/source/phdoc_static/my-styles.css
@@ -1,7 +1,7 @@
.highlight-ipython3 {
background-color: #f8f8c8;
background-color: #dddddd;
}

div.highlight-ipython3 pre {
background-color: #f8f8c8;
background-color: #dddddd;
}
57 changes: 57 additions & 0 deletions _unittests/ut_faq/test_pass.py
@@ -0,0 +1,57 @@
"""
@brief test log(time=1s)
"""


import sys
import os
import unittest


try:
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper


try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src


from pyquickhelper.loghelper import fLOG
from src.code_beatrix.faq.faq_python import instruction_pass


class TestPass(unittest.TestCase):

def test_pass(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
instruction_pass()


if __name__ == "__main__":
unittest.main()
62 changes: 62 additions & 0 deletions _unittests/ut_faq/test_video.py
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
"""
@brief test log(time=1s)
"""


import sys
import os
import unittest


try:
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper


try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src


from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.faq.faq_video import download_youtube_video


class TestVideo(ExtTestCase):

def test_video_download(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_video_download")
download_youtube_video('vHcfbOqYztU', output_path=temp)
exp = os.path.join(temp, "vidéo tres courte.mp4")
self.assertExists(exp)


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -33,6 +33,7 @@ install:
- "%PYTHON%\\Scripts\\pip install pyrsslocal"
- "%PYTHON%\\Scripts\\pip install ensae_projects"
- "%PYTHON%\\Scripts\\pip install pygame"
- "%PYTHON%\\Scripts\\pip install pytube"
- "%PYTHON%\\Scripts\\pymy_install3 opencv_python cupy"
- "%PYTHON%\\Scripts\\pymy_install3 yaml pyproj scikit-image opencv-python"
- "%PYTHON%\\Scripts\\pip install tutormagic"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -20,6 +20,7 @@ pymmails
pymyinstall
pyquickhelper>=1.5.2259
pyshp
pytube
qgrid
semantic_version
shapely
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -232,4 +232,7 @@ def write_version():
package_dir=package_dir,
package_data=package_data,
install_requires=["pyquickhelper>=1.5.2259"],
extras_require={
'faq': ['pytube'],
}
)
12 changes: 5 additions & 7 deletions src/code_beatrix/faq/faq_python.py
@@ -1,17 +1,15 @@
# -*- coding: utf-8 -*-
"""
@file
@brief Quelques questions d'ordre général autour du langage Python.
@brief Quelques questions d'ordre général autour du langage :epkg:`Python`.
"""


def instruction_pass():
"""
Cette fonction ne fait rien.
..reffaq::
.. faqref::
:title: Quel est l'entier le plus grand ?
La version 3 du langage Python a supprimé la constante ``sys.maxint``
Expand All @@ -31,16 +29,16 @@ def instruction_pass():
Au delà, il faut utiliser la librairie `gmpy2 <http://gmpy2.readthedocs.org/en/latest/>`_.
Il est également recommandé d'utiliser cette librairie pour les grands nombres entiers
(entre 20 et 40 chiffres). La librairie est plus rapide que l'implémentation
du langage Python (voir `Overview of gmpy2 <https://gmpy2.readthedocs.org/en/latest/overview.html>`_).
du langage :epkg:`Pythonè (voir `Overview of gmpy2 <https://gmpy2.readthedocs.org/en/latest/overview.html>`_).
.. reffaq::
.. faqref::
:title: Tabulations ou espace ?
Il est préférable de ne pas utiliser les tabulations et de les remplacer par des espaces.
Lorsqu'on passe d'un Editeur à un autre, les espaces ne bougent pas. Les tabulations sont plus ou moins grandes visuellement.
L'essentiel est de ne pas mélanger.
Dans `SciTE <http://www.scintilla.org/SciTE.html>`_, il faut aller dans le menu Options / Change Indentation Settings...
Dans :epkg:`SciTe`, il faut aller dans le menu Options / Change Indentation Settings...
Tous les éditeurs ont une option similaire.
"""
pass
41 changes: 41 additions & 0 deletions src/code_beatrix/faq/faq_video.py
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
@file
@brief Quelques questions d'ordre général autour du langage Python.
"""
from pytube import YouTube


def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/mp4", **kwargs):
"""
Downloads a video from :epkg:`youtube`.
@param tag tag of the :epkg:`youtube` video to download
@param output_path output path
@param mime_type see :epkg:`youtube`
@param res see :epkg:`youtube`
@param kwargs see :epkg:`youtube`
@return filename
.. faqref::
:title: Télécharger une vidéo sur YouTube
Le module :epkg:`pytube` permet de télécharger une vidéo
:epkg:`youtube`. Chaque vidéo est disponible selon plusieurs
format dont on récupère la liste avant de choisir
qui correspond à celui voulu.
::
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=tRFHXMQP-QU')
st = yt.streams
fil = st.filter(mime_type="video/mp4", res="720p")
fil.first().download()
"""
yt = YouTube('https://www.youtube.com/watch?v={0}'.format(tag))
st = yt.streams.filter(mime_type=mime_type, res=res, **kwargs)
fi = st.first()
fi.download(output_path=output_path)
return fi.default_filename

0 comments on commit c884e30

Please sign in to comment.