Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix wrong response formats for the add youtube video endpoint #119

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api/controllers/feed.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 17 additions & 11 deletions app/api/controllers/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,27 +113,33 @@ 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,
"preview_url": video["snippet"]["thumbnails"]["standard"]["url"],
"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)
Expand Down Expand Up @@ -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("/<int:id>/sections")
Expand Down Expand Up @@ -211,7 +217,7 @@ def get(self, videoId, format):
return {"stream": stream_info}, 200


@video_ns.route('/<int:id>')
@video_ns.route("/<int:id>")
class DeleteVideo(Resource):
@token_required
@video_ns.doc(
Expand Down
20 changes: 0 additions & 20 deletions app/utils/extract_video_id.py

This file was deleted.

56 changes: 56 additions & 0 deletions app/utils/video_utils.py
Original file line number Diff line number Diff line change
@@ -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&amp;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