Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
pylint improvement - completed API and ffmpeg audio classes & methods
  • Loading branch information
Javinator9889 committed Oct 14, 2019
1 parent e55eb1a commit 6e6ade3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
22 changes: 21 additions & 1 deletion YouTubeMDBot/audio/fpcalc.py
Expand Up @@ -21,6 +21,10 @@


def is_fpcalc_available() -> bool:
"""
Checks if ffmpeg is installed in the system.
:return: True if available, else False.
"""
try:
proc = Popen(["fpcalc", "-v"], stdout=PIPE, stderr=PIPE)
except OSError:
Expand All @@ -29,8 +33,16 @@ def is_fpcalc_available() -> bool:
proc.wait()


class FPCalc(object):
class FPCalc:
"""
Calculates audio fingerprint by passing the audio bytes.
It operates with pipes so no file is created.
"""
def __init__(self, audio: bytes):
"""
Creates the FPCalc object.
:param audio: the audio bytes.
"""
fpcalc = Popen(FPCALC, stdout=PIPE, stdin=PIPE)
out, _ = fpcalc.communicate(audio)
res = out.decode("utf-8")
Expand All @@ -44,7 +56,15 @@ def __init__(self, audio: bytes):
self.__fp: str = str(fingerprint.group(0))

def duration(self) -> int:
"""
Obtains the audio duration in seconds.
:return: duration in seconds.
"""
return self.__duration

def fingerprint(self) -> str:
"""
Obtains the audio fingerprint.
:return: fingerprint in seconds.
"""
return self.__fp
1 change: 1 addition & 0 deletions YouTubeMDBot/constants/__init__.py
Expand Up @@ -17,5 +17,6 @@
from ..constants.app_constants import FPCALC
from ..constants.app_constants import YDL_CLI_OPTIONS
from ..constants.app_constants import YOUTUBE
from ..constants.app_constants import PROGRAM_ARGS
from ..constants.app_constants import FFMPEG_OPENER
from ..constants.app_constants import FFMPEG_CONVERTER
2 changes: 2 additions & 0 deletions YouTubeMDBot/constants/app_constants.py
Expand Up @@ -14,7 +14,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys

PROGRAM_ARGS = sys.argv
# YouTube DL options
YDL_CLI_OPTIONS = ["youtube-dl", "--format", "bestaudio[ext=m4a]", "--quiet", "--output",
"-"]
Expand Down
2 changes: 1 addition & 1 deletion YouTubeMDBot/decorators/decorators.py
Expand Up @@ -15,7 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from functools import wraps

from .. import PROGRAM_ARGS
from ..constants import PROGRAM_ARGS


# logging = LoggingHandler()
Expand Down
17 changes: 16 additions & 1 deletion YouTubeMDBot/downloader/youtube_downloader.py
Expand Up @@ -19,13 +19,24 @@
from ..constants.app_constants import YDL_CLI_OPTIONS


class YouTubeDownloader(object):
class YouTubeDownloader:
"""
Download a YouTube video directly into memory.
"""
def __init__(self, url: str):
"""
Creates the YouTubeDownloader object. Call "download" for obtaining the video.
:param url: the video URL.
"""
self.__url: str = url
self.__options: list = YDL_CLI_OPTIONS.copy()
self.__options.append(self.__url)

def download(self) -> Tuple[BytesIO, bytes]:
"""
Downloads the YouTube video directly into memory by using pipes.
:return: a tuple with "BytesIO" and "bytes".
"""
import subprocess

proc = subprocess.Popen(self.__options,
Expand All @@ -40,4 +51,8 @@ def download(self) -> Tuple[BytesIO, bytes]:
str(stderr.decode("utf-8")))

def get_url(self) -> str:
"""
Obtains the video URL.
:return: str with the URL.
"""
return self.__url

0 comments on commit 6e6ade3

Please sign in to comment.