diff --git a/app/api/controllers/feed.py b/app/api/controllers/feed.py index 5e1440d..821a8e6 100644 --- a/app/api/controllers/feed.py +++ b/app/api/controllers/feed.py @@ -1,7 +1,7 @@ from flask_restplus import Namespace, Resource from flask import request -from app.utils.extract_video_id import extract_video_id +from app.utils.video_utils import extract_video_id from app.utils.messages import SECTION_ID_NOT_PROVIDED, RESOURCE_NOT_FOUND from app.utils.youtube_dl import youtube_dl_extract_info, youtube_dl_extract_format from ..dao.section_dao import SectionDAO diff --git a/app/api/controllers/video.py b/app/api/controllers/video.py index a0f5784..44e4a33 100644 --- a/app/api/controllers/video.py +++ b/app/api/controllers/video.py @@ -13,7 +13,7 @@ validate_video_creation_data, validate_video_sections_data, ) -from app.utils.extract_video_id import extract_video_id +from app.utils.video_utils import extract_video_id, yt_duration_to_seconds from app.utils.youtube_dl import * from app.utils.messages import RESOURCE_NOT_FOUND from flask import Response, jsonify, request @@ -113,16 +113,18 @@ def post(self): video_url = payload["url"] video_id = extract_video_id(video_url) response = requests.get( - f'https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cid%2Clocalizations%2Cstatistics%2CtopicDetails&id={video_id}&key={os.environ.get("API_KEY")}' + f'https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cid%2Cstatus&id={video_id}&key={os.environ.get("API_KEY")}' ) video_json = response.json() - response = {"notes": []} + result = {"notes": []} if len(video_json["items"]) == 0: - response["notes"].append("No video found, please check the url.") - return response, 404 + result["notes"].append("No video found, please check the url.") + return result, 404 else: video = video_json["items"][0] + raw_video_duration = video["contentDetails"]["duration"].split("T")[1] + video_data = { "title": video["snippet"]["title"], "url": video_url, @@ -130,10 +132,14 @@ def post(self): "date_published": video["snippet"]["publishedAt"].split("T")[0], "source": "YouTube", "channel": video["snippet"]["channelTitle"], - "duration": video["contentDetails"]["duration"].split("T")[1], + "duration": yt_duration_to_seconds(video["contentDetails"]["duration"]), "archived": False, - "free_to_reuse": video["contentDetails"]["licensedContent"], - "authorized_to_reuse": video["contentDetails"]["licensedContent"], + "free_to_reuse": ( + True + if video["status"]["license"] == "creativeCommon" + else not video["contentDetails"]["licensedContent"] + ), + "authorized_to_reuse": False, } validation_result = validate_video_creation_data(video_data) @@ -163,8 +169,8 @@ def post(self): video_data.get("authorized_to_reuse"), ) - response["video"] = map_to_dto(video) - return response, 200 + result["video"] = map_to_dto(video) + return {"123": response.json(), "result": result}, 200 @video_ns.route("//sections") @@ -211,7 +217,7 @@ def get(self, videoId, format): return {"stream": stream_info}, 200 -@video_ns.route('/') +@video_ns.route("/") class DeleteVideo(Resource): @token_required @video_ns.doc( diff --git a/app/utils/extract_video_id.py b/app/utils/extract_video_id.py deleted file mode 100644 index 13ccf85..0000000 --- a/app/utils/extract_video_id.py +++ /dev/null @@ -1,20 +0,0 @@ -from urllib.parse import urlparse, parse_qs - - -def extract_video_id(url): - # Examples: - # - http://youtu.be/SA2iWivDJiE - # - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu - # - http://www.youtube.com/embed/SA2iWivDJiE - # - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US - query = urlparse(url) - if query.hostname == "youtu.be": - return query.path[1:] - if query.hostname in {"www.youtube.com", "youtube.com"}: - if query.path == "/watch": - return parse_qs(query.query)["v"][0] - if query.path[:7] == "/embed/": - return query.path.split("/")[2] - if query.path[:3] == "/v/": - return query.path.split("/")[2] - return None diff --git a/app/utils/video_utils.py b/app/utils/video_utils.py new file mode 100644 index 0000000..3beb624 --- /dev/null +++ b/app/utils/video_utils.py @@ -0,0 +1,56 @@ +from urllib.parse import urlparse, parse_qs + + +def extract_video_id(url): + # eg: + # - http://youtu.be/SA2iWivDJiE + # - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu + # - http://www.youtube.com/embed/SA2iWivDJiE + # - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US + query = urlparse(url) + if query.hostname == "youtu.be": + return query.path[1:] + if query.hostname in {"www.youtube.com", "youtube.com"}: + if query.path == "/watch": + return parse_qs(query.query)["v"][0] + if query.path[:7] == "/embed/": + return query.path.split("/")[2] + if query.path[:3] == "/v/": + return query.path.split("/")[2] + return None + + +def yt_duration_to_seconds(duration): + # eg: P1W2DT6H21M32S + week = 0 + day = 0 + hour = 0 + min = 0 + sec = 0 + + duration = duration.lower() + + value = "" + for c in duration: + if c.isdigit(): + value += c + continue + + elif c == "p": + pass + elif c == "t": + pass + elif c == "w": + week = int(value) * 604800 + elif c == "d": + day = int(value) * 86400 + elif c == "h": + hour = int(value) * 3600 + elif c == "m": + min = int(value) * 60 + elif c == "s": + sec = int(value) + + value = "" + + return week + day + hour + min + sec