update bing from link to search

This commit is contained in:
呱呱呱 2025-06-18 14:56:13 +08:00
parent f767d95730
commit 2ff38ad402
1 changed files with 35 additions and 1 deletions

View File

@ -56,8 +56,18 @@ module Baidu
return [] unless access_token.present? && start_date.present?
source_from_data = api("source/all/a", start_date, start_date, "pv_count,visitor_count,ip_count")
source_from = []
#当日的bing来源数量
bing_number = bing_link_number(start_date)
source_from_data['items'][1].each_with_index do |source, index|
source_from.push(((source[0].to_f / source_from_data['sum'][0][0].to_f) * 100).round(2))
source_number = case index
when 1 # link 数据
source[0].to_f - bing_number
when 2 # search数据
source[0].to_f + bing_number
else
source[0].to_f
end
source_from.push((( source_number / source_from_data['sum'][0][0].to_f) * 100).round(2))
end
daily_statistic = DailyPlatformStatistic.find_or_initialize_by(date: start_date)
daily_statistic.source_through = source_from[0]
@ -67,6 +77,30 @@ module Baidu
daily_statistic.save
end
def bing_link_number(start_date)
return [] unless access_token.present? && start_date.present?
source_from_data = api("source/link/a", start_date, start_date, "pv_count")
# 提取链接项和对应的数据项
link_items = source_from_data.dig("result", "items", 0) || []
value_items = source_from_data.dig("result", "items", 1) || []
# 目标链接名称
target_name = "http://cn.bing.com"
target_number = 0
# 遍历查找匹配的 name
link_items.each_with_index do |link_array, index|
link = link_array.first
if link["name"] == target_name
stats = value_items[index]
target_number = stats[0]
break
end
end
target_number.to_f
end
def diff_days(start_date, end_date)
(end_date.beginning_of_day.to_i - start_date.beginning_of_day.to_i) / (24 * 3600)
end