chore: Key Validation and restructure

- Added API Key validation in Video Settings
- Moved Statistics section above description in Video
- Moved id retrieving from URL to py side
- Removed js side calls, added section toggling in js
- Restructured Code
This commit is contained in:
marination 2020-09-07 13:27:17 +05:30
parent e3495116dd
commit 9ce38de439
4 changed files with 61 additions and 64 deletions

View File

@ -3,29 +3,15 @@
frappe.ui.form.on('Video', { frappe.ui.form.on('Video', {
refresh: function (frm) { refresh: function (frm) {
if (frm.doc.provider === "YouTube") { frm.events.toggle_youtube_statistics_section(frm);
frappe.db.get_single_value("Video Settings", "enable_youtube_tracking").then(value => {
if (value) {
frm.events.get_video_stats(frm);
} else {
frm.set_df_property('youtube_tracking_section', 'hidden', true);
}
});
}
frm.add_custom_button("Watch Video", () => frappe.help.show_video(frm.doc.url, frm.doc.title)); frm.add_custom_button("Watch Video", () => frappe.help.show_video(frm.doc.url, frm.doc.title));
}, },
get_video_stats: (frm) => { toggle_youtube_statistics_section: (frm) => {
const expression = '(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?\\s]{11})'; if (frm.doc.provider === "YouTube") {
var youtube_id = frm.doc.url.match(expression)[1]; frappe.db.get_single_value("Video Settings", "enable_youtube_tracking").then( val => {
frm.toggle_display("youtube_tracking_section", val);
frappe.call({ });
method: "erpnext.utilities.doctype.video.video.get_video_stats", }
args: {
docname: frm.doc.name,
youtube_id: youtube_id
}
});
} }
}); });

View File

@ -14,15 +14,15 @@
"column_break_4", "column_break_4",
"publish_date", "publish_date",
"duration", "duration",
"section_break_7",
"description",
"image",
"youtube_tracking_section", "youtube_tracking_section",
"like_count", "like_count",
"view_count", "view_count",
"col_break", "col_break",
"dislike_count", "dislike_count",
"comment_count" "comment_count",
"section_break_7",
"description",
"image"
], ],
"fields": [ "fields": [
{ {
@ -122,7 +122,7 @@
], ],
"image_field": "image", "image_field": "image",
"links": [], "links": [],
"modified": "2020-08-02 04:26:16.345569", "modified": "2020-09-04 12:59:28.283622",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Utilities", "module": "Utilities",
"name": "Video", "name": "Video",

View File

@ -5,49 +5,48 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
import json import json
import re
from frappe.model.document import Document from frappe.model.document import Document
from frappe import _
from six import string_types from six import string_types
from pyyoutube import Api from pyyoutube import Api
class Video(Document): class Video(Document):
pass def validate(self):
self.set_youtube_statistics()
def set_youtube_statistics(self):
tracking_enabled = frappe.db.get_single_value("Video Settings", "enable_youtube_tracking")
if self.provider == "YouTube" and not tracking_enabled:
return
api_key = frappe.db.get_single_value("Video Settings", "api_key")
youtube_id = get_id_from_url(self.url)
api = Api(api_key=api_key)
try:
video = api.get_video_by_id(video_id=youtube_id)
video_stats = video.items[0].to_dict().get('statistics')
self.like_count = video_stats.get('likeCount')
self.view_count = video_stats.get('viewCount')
self.dislike_count = video_stats.get('dislikeCount')
self.comment_count = video_stats.get('commentCount')
except Exception:
title = "Failed to Update YouTube Statistics for Video: {0}".format(self.name)
frappe.log_error(title + "\n\n" + frappe.get_traceback(), title=title)
@frappe.whitelist() @frappe.whitelist()
def get_video_stats(docname, youtube_id, update=True): def get_id_from_url(url):
'''Returns/Sets video statistics
:param docname: Name of Video
:param youtube_id: Unique ID from URL
:param update: Updates db stats value if True, else returns statistics
''' '''
if isinstance(update, string_types): Returns video id from url
update = json.loads(update)
api_key = frappe.db.get_single_value("Video Settings", "api_key") :param youtube url: String URL
api = Api(api_key=api_key) '''
if not isinstance(url, string_types):
frappe.throw(_("URL can only be a string"), title=_("Invalid URL"))
try: pattern = re.compile(r'[a-z\:\//\.]+(youtube|youtu)\.(com|be)/(watch\?v=|embed/|.+\?v=)?([^"&?\s]{11})?')
video = api.get_video_by_id(video_id=youtube_id) id = pattern.match(url)
video_stats = video.items[0].to_dict().get('statistics') return id.groups()[-1]
stats = {
'like_count' : video_stats.get('likeCount'),
'view_count' : video_stats.get('viewCount'),
'dislike_count' : video_stats.get('dislikeCount'),
'comment_count' : video_stats.get('commentCount')
}
if not update:
return stats
frappe.db.sql("""
UPDATE `tabVideo`
SET
like_count = %(like_count)s,
view_count = %(view_count)s,
dislike_count = %(dislike_count)s,
comment_count = %(comment_count)s
WHERE name = {0}""".format(frappe.db.escape(docname)), stats) #nosec
frappe.db.commit()
except:
message = "Please make sure you are connected to the Internet"
frappe.log_error(message + "\n\n" + frappe.get_traceback(), "Failed to Update YouTube Statistics for Video: {0}".format(docname))

View File

@ -3,8 +3,20 @@
# For license information, please see license.txt # For license information, please see license.txt
from __future__ import unicode_literals from __future__ import unicode_literals
# import frappe import frappe
from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from apiclient.discovery import build
class VideoSettings(Document): class VideoSettings(Document):
pass def validate(self):
self.validate_youtube_api_key()
def validate_youtube_api_key(self):
if self.enable_youtube_tracking and self.api_key:
try:
build("youtube", "v3", developerKey=self.api_key)
except Exception:
title = _("Failed to Authenticate the API key.")
frappe.log_error(title + "\n\n" + frappe.get_traceback(), title=title)
frappe.throw(title + " Please check the error logs.", title=_("Invalid Credentials"))