Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge branch 'milestone_2' into 'development'
Milestone #2 See merge request Javinator9889/YouTubeMDBot!4
- Loading branch information
Showing
26 changed files
with
619 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# keys folder | ||
keys/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from ..api.youtube_api import YouTubeAPI | ||
from ..api.youtube_api import YouTubeVideoData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from isodate import parse_duration | ||
|
||
from ..constants import YOUTUBE | ||
from ..errors import EmptyBodyError | ||
|
||
|
||
class YouTubeVideoData(object): | ||
def __init__(self, data: dict, ignore_errors: bool = False): | ||
if not data.get("items"): | ||
raise EmptyBodyError("The data object has no items") | ||
self.id: str = "" | ||
self.title: str = "" | ||
self.thumbnail: str = "" | ||
self.artist: str = "" | ||
self.duration: float = 0.0 | ||
self.views: int = 0 | ||
self.likes: int = 0 | ||
self.dislikes: int = 0 | ||
if len(data.get("items")) >= 1: | ||
content = data.get("items")[0] | ||
snippet = content.get("snippet") | ||
details = content.get("contentDetails") | ||
statistics = content.get("statistics") | ||
if not snippet and not ignore_errors: | ||
raise EmptyBodyError("No information available to requested video") | ||
elif not snippet and ignore_errors: | ||
snippet_available = False | ||
else: | ||
snippet_available = True | ||
if not details and not ignore_errors: | ||
raise EmptyBodyError("No video details available") | ||
elif not details and ignore_errors: | ||
details_available = False | ||
else: | ||
details_available = True | ||
if not statistics and not ignore_errors: | ||
raise EmptyBodyError("No statistics available") | ||
elif not statistics and ignore_errors: | ||
statistics_available = False | ||
else: | ||
statistics_available = True | ||
c_id = content.get("id", "") | ||
self.id = c_id.get("videoId", "") if isinstance(c_id, dict) else c_id | ||
if snippet_available: | ||
self.title = snippet["title"] | ||
try: | ||
self.thumbnail = snippet["thumbnails"]["maxres"]["url"] | ||
except KeyError: | ||
try: | ||
self.thumbnail = snippet["thumbnails"]["high"]["url"] | ||
except KeyError: | ||
try: | ||
self.thumbnail = snippet["thumbnails"]["medium"]["url"] | ||
except KeyError: | ||
self.thumbnail = snippet["thumbnails"]["default"]["url"] | ||
self.artist = snippet["channelTitle"] | ||
if details_available: | ||
self.duration = parse_duration(details["duration"]).total_seconds() | ||
if statistics_available: | ||
self.views = int(statistics["viewCount"]) | ||
self.likes = int(statistics["likeCount"]) | ||
self.dislikes = int(statistics["dislikeCount"]) | ||
|
||
|
||
class YouTubeAPI(object): | ||
def __init__(self): | ||
from googleapiclient.discovery import build | ||
|
||
self.__youtube = build(serviceName=YOUTUBE["api"]["name"], | ||
version=YOUTUBE["api"]["version"], | ||
developerKey=YOUTUBE["key"]) | ||
|
||
def search(self, term: str): | ||
return self.__youtube.search().list( | ||
q=term, | ||
type="video", | ||
part="id,snippet", | ||
maxResults=1 | ||
).execute() | ||
|
||
@staticmethod | ||
def video_details(video_id: str) -> YouTubeVideoData: | ||
try: | ||
import ujson as json | ||
except ImportError: | ||
import json | ||
from urllib.request import urlopen | ||
|
||
api_url = YOUTUBE["endpoint"].format(video_id, YOUTUBE["key"]) | ||
data = urlopen(url=api_url) | ||
return YouTubeVideoData(data=json.loads(data.read())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from ..audio.ffmpeg import FFmpegOpener | ||
from ..audio.ffmpeg import ffmpeg_available | ||
from ..audio.fpcalc import FPCalc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
from io import BytesIO | ||
from subprocess import PIPE | ||
from subprocess import Popen | ||
|
||
|
||
def ffmpeg_available() -> bool: | ||
try: | ||
proc = Popen(["ffmpeg", "-version"], | ||
stdout=PIPE, | ||
stderr=PIPE) | ||
except OSError: | ||
return False | ||
else: | ||
proc.wait() | ||
return proc.returncode == 0 | ||
|
||
|
||
class FFmpegOpener(object): | ||
def __init__(self, data: bytes): | ||
io = BytesIO(data) | ||
self.__ffmpeg_proc = Popen(["ffmpeg", "-i", "-", "-f", "s16le", "-"], | ||
stdout=PIPE, stderr=PIPE, stdin=io) | ||
self.__out = None | ||
self.__err = None | ||
|
||
def open(self) -> int: | ||
self.__out, self.__err = self.__ffmpeg_proc.communicate() | ||
return self.__ffmpeg_proc.returncode | ||
|
||
def get_output(self) -> bytes: | ||
return self.__out | ||
|
||
def get_extra(self) -> bytes: | ||
return self.__err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# 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 re | ||
from subprocess import PIPE | ||
from subprocess import Popen | ||
|
||
from ..constants import FPCALC | ||
|
||
|
||
def is_fpcalc_available() -> bool: | ||
try: | ||
proc = Popen(["fpcalc", "-v"], stdout=PIPE, stderr=PIPE) | ||
except OSError: | ||
return False | ||
else: | ||
proc.wait() | ||
|
||
|
||
class FPCalc(object): | ||
def __init__(self, audio: bytes): | ||
fpcalc = Popen(FPCALC, stdout=PIPE, stdin=PIPE) | ||
out, _ = fpcalc.communicate(audio) | ||
res = out.decode("utf-8") | ||
|
||
duration_pattern = "[^=]\\d+\\n" | ||
fingerprint_pattern = "[^=]*$" | ||
duration = re.search(duration_pattern, res) | ||
fingerprint = re.search(fingerprint_pattern, res) | ||
|
||
self.__duration: int = int(duration.group(0)) | ||
self.__fp: str = str(fingerprint.group(0)) | ||
|
||
def duration(self) -> int: | ||
return self.__duration | ||
|
||
def fingerprint(self) -> str: | ||
return self.__fp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# YouTubeMDBot | ||
# Copyright (C) 2019 - Javinator9889 | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
class EmptyBodyError(Exception): | ||
pass |
Oops, something went wrong.