使用FB_api拉取后台数据
思路就是通过 curl 请求进行拉取,并用jq对获取到的json 数据进行解析。
#!/bin/bash
appkey=这里填写appkey信息
ads_json_name=ads_json_MK
get_curl_datas(){
echo ${1}
curl -G -d "breakdowns=country" -d "time_range={%22since%22:%22${day}%22,%22until%22:%22${day}%22}" -d "level=ad" -d "fields=ad_id,spend,ad_name,adset_id,adset_name,campaign_id,campaign_name,impressions,clicks,created_time,actions" -d "access_token=这里填写密钥" "https://graph.facebook.com/v7.0/act_${1}/insights?limit=500">json_data/${ads_json_name}
}
get_jq_datas(){
./jq-linux64 --raw-output '{data:.data[]}|.[]|["1590028813409",.ad_id,.ad_name,.country,.spend,.impressions,.clicks,.adset_id,.adset_name,.campaign_id,.campaign_name,.date_start,.date_stop,.actions[(.actions|map(.action_type)?|index("omni_app_install"))].value]|@csv' json_data/${ads_json_name} | grep _ | tr -d "\"\t" >./datas/${appkey}/ads.csv
#处理数据
cat ./datas/${appkey}/ads.csv | while read line
do
num=`echo ${line}|grep -o ,|grep -c ,`
if [ ${num} = "12" ] ;then
line=${line}",0"
echo $line
fi
echo $line >>./datas/${appkey}/csv/ads-${day}.csv
done
}
#开始运行,读取account列表
day=$1
rm -rf ./datas/${appkey}/csv/ads-${day}.csv
cat mk_account_list | while read line
do
url_next=""
until [[ $url_next == "null" ]]
do
url_next=`./jq-linux64 --raw-output '[.paging]|.[]|[.next]|.[]' json_data/${ads_json_name}`
echo $url_next
#./datas/${appkey}/csv/ads-${day}.csv
if [[ $url_next == "" ]];
then
echo "调用curl拉取---${line}"
get_curl_datas ${line}
get_jq_datas
elif [[ $url_next == "null" ]];
then
echo "完成"
> json_data/${ads_json_name}
else
echo "处理为表格并重新请求next_curl"
curl "$url_next" > json_data/${ads_json_name}
get_jq_datas
fi
done
done