def post(self): payload = request.json 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")}' ) video_json = response.json() response = {"notes": []} if len(video_json["items"]) == 0: response["notes"].append("No video found, please check the url.") return response, 404 else: video = video_json["items"][0] 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], "archived": False, "free_to_reuse": video["contentDetails"]["licensedContent"], "authorized_to_reuse": video["contentDetails"]["licensedContent"], } validation_result = validate_video_creation_data(video_data) if validation_result is not None: return validation_result url = video_data["url"] existing_video = VideoDAO.find_video_by_url(url) if existing_video: video_data["date_published"] = datetime.strptime( video_data["date_published"], "%Y-%m-%d") existing_video.update(video_data) video = existing_video else: video = VideoDAO.create_video( video_data["title"], video_data["url"], video_data["preview_url"], datetime.strptime(video_data["date_published"], "%Y-%m-%d"), video_data["source"], video_data["channel"], video_data["duration"], video_data.get("archived"), video_data.get("free_to_reuse"), video_data.get("authorized_to_reuse"), ) response["video"] = map_to_dto(video) return response, 200
def post(self): data = request.json validation_result = validate_video_creation_data(data) if validation_result is not None: return validation_result url = data["url"] existing_video = VideoDAO.find_video_by_url(url) response = {"notes": []} # find authors from the list authors = AuthorDAO.find_authors_by_ids(data["authors"]) if authors.count() != len(data["authors"]): response["notes"].append( "Some of the authors are not valid or they do not exist." ) # find sections from the list sections = SectionDAO.find_sections_by_ids(data["category_sections"]) if sections.count() != len(data["category_sections"]): response["notes"].append( "Some of the sections are not valid or they do not exist." ) # remove association fields data.pop("authors") data.pop("category_sections") if existing_video: data["date_published"] = datetime.strptime( data["date_published"], "%Y-%m-%d" ) existing_video.update(data) VideoDAO.replace_video_authors(existing_video, authors) VideoDAO.replace_video_sections(existing_video, sections) video = existing_video else: video = VideoDAO.create_video( data["title"], data["url"], data["preview_url"], datetime.strptime(data["date_published"], "%Y-%m-%d"), data["source"], data["channel"], data["duration"], data.get("archived"), data.get("free_to_reuse"), data.get("authorized_to_reuse"), ) VideoDAO.add_video_authors(video, authors) VideoDAO.add_video_sections(video, sections) response["video"] = map_to_dto(video) return response, 200
def post(self): payload = request.json 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")}' ) video_json = response.json() response = {"notes": []} if len(video_json["items"]) == 0: response["notes"].append("No video found, please check the url.") return response, 404 else: video = video_json["items"][0] 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], "archived": False, "free_to_reuse": video["contentDetails"]["licensedContent"], "authorized_to_reuse": video["contentDetails"]["licensedContent"], "category_sections": payload["sections"][0]["id"], "authors": list(payload["authors"]), } validation_result = validate_video_creation_data(video_data) if validation_result is not None: return validation_result url = video_data["url"] existing_video = VideoDAO.find_video_by_url(url) # find authors from the list authors = AuthorDAO.find_authors_by_ids(video_data["authors"]) if authors.count() != len(video_data["authors"]): response["notes"].append( "Some of the authors are not valid or they do not exist.") # find sections from the list sections = SectionDAO.find_sections_by_ids( video_data["category_sections"]) if sections.count() != len(video_data["category_sections"]): response["notes"].append( "Some of the sections are not valid or they do not exist.") # remove association fields video_data.pop("authors") video_data.pop("category_sections") if existing_video: video_data["date_published"] = datetime.strptime( video_data["date_published"], "%Y-%m-%d") existing_video.update(video_data) VideoDAO.replace_video_authors(existing_video, authors) VideoDAO.replace_video_sections(existing_video, sections) video = existing_video else: video = VideoDAO.create_video( video_data["title"], video_data["url"], video_data["preview_url"], datetime.strptime(video_data["date_published"], "%Y-%m-%d"), video_data["source"], video_data["channel"], video_data["duration"], video_data.get("archived"), video_data.get("free_to_reuse"), video_data.get("authorized_to_reuse"), ) VideoDAO.add_video_authors(video, authors) VideoDAO.add_video_sections(video, sections) response["video"] = map_to_dto(video) return response, 200