chore: Added Interactions Report and behaviour fixes
- Youtube Interactions Report with Chart and Summary - Statistics change in doc on refresh and get updated in db as well
This commit is contained in:
parent
ddc2be36ab
commit
ccf4ab9f85
@ -21,17 +21,10 @@ frappe.ui.form.on('Video', {
|
|||||||
var youtube_id = frm.doc.url.match(expression)[1];
|
var youtube_id = frm.doc.url.match(expression)[1];
|
||||||
|
|
||||||
frappe.call({
|
frappe.call({
|
||||||
method: "erpnext.utilities.doctype.video.video.update_video_stats",
|
method: "erpnext.utilities.doctype.video.video.get_video_stats",
|
||||||
args: {
|
args: {
|
||||||
|
docname: frm.doc.name,
|
||||||
youtube_id: youtube_id
|
youtube_id: youtube_id
|
||||||
},
|
|
||||||
callback: (r) => {
|
|
||||||
var result = r.message;
|
|
||||||
var fields = ['like_count', 'view_count', 'dislike_count', 'comment_count'];
|
|
||||||
fields.forEach((field) => {
|
|
||||||
frm.doc[field] = result[field];
|
|
||||||
})
|
|
||||||
frm.refresh_fields();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5,24 +5,43 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from six import string_types
|
||||||
from pyyoutube import Api
|
from pyyoutube import Api
|
||||||
|
|
||||||
class Video(Document):
|
class Video(Document):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def update_video_stats(youtube_id):
|
def get_video_stats(docname, youtube_id, update=True):
|
||||||
'''
|
'''Returns/Sets video statistics
|
||||||
|
:param docname: Name of Video
|
||||||
:param youtube_id: Unique ID from URL
|
:param youtube_id: Unique ID from URL
|
||||||
|
:param update: Updates db stats value if True, else returns statistics
|
||||||
'''
|
'''
|
||||||
|
if isinstance(update, string_types):
|
||||||
|
update = json.loads(update)
|
||||||
|
|
||||||
api_key = frappe.db.get_single_value("Video Settings", "api_key")
|
api_key = frappe.db.get_single_value("Video Settings", "api_key")
|
||||||
api = Api(api_key=api_key)
|
api = Api(api_key=api_key)
|
||||||
|
|
||||||
video = api.get_video_by_id(video_id=youtube_id)
|
video = api.get_video_by_id(video_id=youtube_id)
|
||||||
video_stats = video.items[0].to_dict().get('statistics')
|
video_stats = video.items[0].to_dict().get('statistics')
|
||||||
return {
|
stats = {
|
||||||
'like_count' : video_stats.get('likeCount'),
|
'like_count' : video_stats.get('likeCount'),
|
||||||
'view_count' : video_stats.get('viewCount'),
|
'view_count' : video_stats.get('viewCount'),
|
||||||
'dislike_count' : video_stats.get('dislikeCount'),
|
'dislike_count' : video_stats.get('dislikeCount'),
|
||||||
'comment_count' : video_stats.get('commentCount')
|
'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)
|
||||||
|
frappe.db.commit()
|
@ -4,6 +4,17 @@
|
|||||||
|
|
||||||
frappe.query_reports["YouTube Interactions"] = {
|
frappe.query_reports["YouTube Interactions"] = {
|
||||||
"filters": [
|
"filters": [
|
||||||
|
{
|
||||||
|
fieldname: "from_date",
|
||||||
|
label: __("From Date"),
|
||||||
|
fieldtype: "Date",
|
||||||
|
default: frappe.datetime.add_months(frappe.datetime.now_date(), -12),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldname:"to_date",
|
||||||
|
label: __("To Date"),
|
||||||
|
fieldtype: "Date",
|
||||||
|
default: frappe.datetime.now_date(),
|
||||||
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
@ -4,11 +4,16 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
|
from frappe.utils import flt
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
|
if not frappe.db.get_single_value("Video Settings", "enable_youtube_tracking") or not filters:
|
||||||
|
return [], []
|
||||||
|
|
||||||
columns = get_columns()
|
columns = get_columns()
|
||||||
data = get_data()
|
data = get_data(filters)
|
||||||
return columns, data
|
chart_data, summary = get_chart_summary_data(data)
|
||||||
|
return columns, data, None, chart_data, summary
|
||||||
|
|
||||||
def get_columns():
|
def get_columns():
|
||||||
return [
|
return [
|
||||||
@ -22,25 +27,25 @@ def get_columns():
|
|||||||
"label": _("Title"),
|
"label": _("Title"),
|
||||||
"fieldname": "title",
|
"fieldname": "title",
|
||||||
"fieldtype": "Data",
|
"fieldtype": "Data",
|
||||||
"width": 100
|
"width": 200
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Provider"),
|
"label": _("Duration"),
|
||||||
"fieldname": "provider",
|
"fieldname": "duration",
|
||||||
"fieldtype": "Data",
|
"fieldtype": "Duration",
|
||||||
"width": 100
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Views"),
|
"label": _("Views"),
|
||||||
"fieldname": "view_count",
|
"fieldname": "view_count",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 100
|
"width": 200
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Likes"),
|
"label": _("Likes"),
|
||||||
"fieldname": "like_count",
|
"fieldname": "like_count",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 100
|
"width": 200
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Dislikes"),
|
"label": _("Dislikes"),
|
||||||
@ -49,24 +54,60 @@ def get_columns():
|
|||||||
"width": 100
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": _("Views"),
|
"label": _("Comments"),
|
||||||
"fieldname": "view_count",
|
"fieldname": "comment_count",
|
||||||
"fieldtype": "Float",
|
"fieldtype": "Float",
|
||||||
"width": 100
|
"width": 100
|
||||||
},
|
|
||||||
{
|
|
||||||
"label": _("Like:Dislike Ratio"),
|
|
||||||
"fieldname": "ratio",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"width": 100
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_data():
|
def get_data(filters):
|
||||||
return frappe.db.sql("""
|
return frappe.db.sql("""
|
||||||
SELECT
|
SELECT
|
||||||
publish_date, title, provider,
|
publish_date, title, provider, duration,
|
||||||
view_count, like_count, dislike_count, comment_count
|
view_count, like_count, dislike_count, comment_count
|
||||||
FROM `tabVideo`
|
FROM `tabVideo`
|
||||||
WHERE view_count is not null
|
WHERE view_count is not null
|
||||||
ORDER BY view_count desc""")
|
and publish_date between %(from_date)s and %(to_date)s
|
||||||
|
ORDER BY view_count desc""", filters, as_dict=1)
|
||||||
|
|
||||||
|
def get_chart_summary_data(data):
|
||||||
|
labels, likes, views = [], [], []
|
||||||
|
total_views = 0
|
||||||
|
|
||||||
|
for row in data:
|
||||||
|
labels.append(row.get('title'))
|
||||||
|
likes.append(row.get('like_count'))
|
||||||
|
views.append(row.get('view_count'))
|
||||||
|
total_views += flt(row.get('view_count'))
|
||||||
|
|
||||||
|
|
||||||
|
chart_data = {
|
||||||
|
"data" : {
|
||||||
|
"labels" : labels,
|
||||||
|
"datasets" : [
|
||||||
|
{
|
||||||
|
"name" : "Likes",
|
||||||
|
"values" : likes
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" : "Views",
|
||||||
|
"values" : views
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"type": "bar",
|
||||||
|
"barOptions": {
|
||||||
|
"stacked": 1
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
summary = [
|
||||||
|
{
|
||||||
|
"value": total_views,
|
||||||
|
"indicator": "Blue",
|
||||||
|
"label": "Total Views",
|
||||||
|
"datatype": "Float",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
return chart_data, summary
|
Loading…
x
Reference in New Issue
Block a user