fix: youtube stats background sync failures

This commit is contained in:
Ankush Menat 2022-06-28 10:50:44 +05:30
parent 925b9d985e
commit bedb11ee67

View File

@ -9,6 +9,7 @@ import frappe
import pytz
from frappe import _
from frappe.model.document import Document
from frappe.utils import cint
from pyyoutube import Api
@ -46,7 +47,7 @@ def is_tracking_enabled():
def get_frequency(value):
# Return numeric value from frequency field, return 1 as fallback default value: 1 hour
if value != "Daily":
return frappe.utils.cint(value[:2].strip())
return cint(value[:2].strip())
elif value:
return 24
return 1
@ -120,24 +121,12 @@ def batch_update_youtube_data():
video_stats = entry.to_dict().get("statistics")
video_id = entry.to_dict().get("id")
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"),
"video_id": video_id,
"like_count": cint(video_stats.get("likeCount")),
"view_count": cint(video_stats.get("viewCount")),
"dislike_count": cint(video_stats.get("dislikeCount")),
"comment_count": cint(video_stats.get("commentCount")),
}
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 youtube_video_id = %(video_id)s""",
stats,
)
frappe.db.set_value("Video", video_id, stats)
video_list = frappe.get_all("Video", fields=["youtube_video_id"])
if len(video_list) > 50: