123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\AdsDailyReport;
- use App\Models\UserAuthorization;
- use App\Services\HttpService;
- use App\Services\MpDailyReportService;
- use Illuminate\Console\Command;
- class MpHourlyReport extends Command
- {
- protected $signature = 'MpHourlyReport {type?} {date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
- protected $description = '微信广告主小时报表(临时处理数据脚本)';
- protected $type;
- protected $dateTime;
- protected $month;
- protected $page;
- protected $time;
- protected $date = false;
- protected $pageSize = 200;
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->time = date('m-d H:i:s');
- $this->info($this->time . ' 开始整理');
- $this->type = $this->argument('type') ? $this->argument('type') : 2;
- $this->dateTime = $this->argument('date') ? $this->argument('date') : null;
- $this->getAccount();
- }
- public function getAccount()
- {
- $account = [
- // [
- // 'name' => '月牙读物',
- // 'app_id' => 'wx17217eb8c222ed4b',
- // 'account_id' => '18406620',
- // 'access_token' => '37ca72ff456fb7d555db4d44f281a409',
- // 'ids' => [
- // '3599099954','3633669596','3747907286','3620534295','3739536495','3747907283'
- // ]
- // ],
- // [
- // 'name' => '九州文轩',
- // 'app_id' => 'wx6135c83ac58ef5f9',
- // 'account_id' => '16816472',
- // 'access_token' => '09cb52b24115ec44c0459521a0a812ce',
- // 'ids' => [
- // '3732293407','3254491971','3243318720','3254360626','3264345785','3231473736','3613241533','3720738263','3726284475','3264344595'
- // ]
- // ],
- // [
- // 'name' => '豪门书刊',
- // 'app_id' => 'wx37bf23e4f99d9ede',
- // 'account_id' => '18410853',
- // 'access_token' => '9531f021143572d483df64445af3299b',
- // 'ids' => [
- // '3353524665','3343259915','3351858521','3351858536','3353524358','3355310072','3351858533','3343259924'
- // ]
- // ],
- // [
- // 'name' => '半冬文摘',
- // 'app_id' => 'wxbf38812d63d4aef1',
- // 'account_id' => '17301675',
- // 'access_token' => 'bf7c85c58e48ba9d3bc0d8616a4bcb34',
- // 'ids' => [
- // '3476216514','3387572895','3552914508','3515116675','3384388072','3525209641','3655140015','3384388072','3468959432','3457790610','3643460247','3554857745','3397052151','3538775776','3541812171','3581200476','3453108235','3421900432','3518075878','3363513751','3526135265','3616784902','3628807113','3526760103'
- // ]
- // ],
- // [
- // 'name' => '痴海文轩',
- // 'app_id' => 'wxaf1a7b478cc0d86e',
- // 'account_id' => '19450435',
- // 'access_token' => 'b5a305a544b6b740a9125c45c0b35a1a',
- // 'ids' => [
- // '3385518061','3385518067','3440632877'
- // ]
- // ],
- // [
- // 'name' => '亦双文苑',
- // 'app_id' => 'wx991b98c6c2ca6eed',
- // 'account_id' => '19450449',
- // 'access_token' => 'c03c3917b3ef68d82c0200f1a3221a65',
- // 'ids' => [
- // '3374243815','3376055171','3512748537','3385516712','3388583283','3515055296','3419716083','3433753501'
- // ]
- // ],
- // [
- // 'name' => '九州文舍',
- // 'app_id' => 'wxe88ea2b22e46da60',
- // 'account_id' => '16816510',
- // 'access_token' => '8af51f63c6a758a944db0a98fbabb16b',
- // 'ids' => [
- // '3525115575','2384727801','3525115578','2387814162','3525623594'
- // ]
- // ],
- // [
- // 'name' => '沐璎阅读',
- // 'app_id' => 'wx83cf8b4382125cc2',
- // 'account_id' => '18032879',
- // 'access_token' => '9ecb7badca363020d70f557acb6eb1a5',
- // 'ids' => [
- // '3510554235','3583095874','3523650197'
- // ]
- // ],
- [
- 'name' => '梦璃文苑',
- 'app_id' => 'wxbd06cf988b8994ad',
- 'account_id' => '17072282',
- 'access_token' => 'c19e178ee2be5c551902895b4e3ebe7b',
- 'ids' => [
- '2369112234','2369112228','2373148837','2373148840'
- ]
- ],
- // [
- // 'name' => '九霄文舍',
- // 'app_id' => 'wxdc8bd7f47fb5d5c1',
- // 'account_id' => '15203275',
- // 'access_token' => '4cb68ebed1dd4d7f0c38af0780d18568',
- // 'ids' => [
- // '3259707658','3647627623','240381751','3647627614'
- // ]
- // ],
- // [
- // 'name' => '纵横书斋',
- // 'app_id' => 'wx7ba2d755310209df',
- // 'account_id' => '14096389',
- // 'access_token' => '880bd0d440aecf350a14a3af31d0df43',
- // 'ids' => [
- // '2259396474','2265758215','2265758218','2250950080','2202026693','3470272295','2183914294','2202154526','3418967451','2227074524','2204871423','2201885963','3445014700','2298605122','2275652267','2213786437','2184292861','2248217749','2249165509','3469152950','2264291156','2259298391','3455542502','2294419727','3417560501','2261776400','2231560270','3459105055','2259396158','2298595665','2298568400'
- // ]
- // ],
- [
- 'name' => '扳手阅读',
- 'app_id' => 'wx98d307e15b812a9e',
- 'account_id' => '18410879',
- 'access_token' => 'addc003a21c56295ae48fc471a7adcfa',
- 'ids' => [
- '2483730944','2488370515','2487115463','2489173339'
- ]
- ],
- // [
- // 'name' => '世离阅读',
- // 'app_id' => 'wx314f6929c50c2cf1',
- // 'account_id' => '17167180',
- // 'access_token' => '038f1df3652056696ba77a119788bab4',
- // 'ids' => [
- // '3264346395'
- // ]
- // ],
- // [
- // 'name' => '无岳文摘',
- // 'app_id' => 'wxeaf7f5eb94ea0a01',
- // 'account_id' => '17959720',
- // 'access_token' => '68a75a5ddee5a40b79fb0a7012169094',
- // 'ids' => [
- // '3474116597','3439636293','3468994242','3491258592','3439636296','3491258588'
- // ]
- // ],
- // [
- // 'name' => '碧芷文苑',
- // 'app_id' => 'wx3b0e971cba3dad42',
- // 'account_id' => '19465480',
- // 'access_token' => '60864acf0544c906471e935e402686c2',
- // 'ids' => [
- // '3468435965','3417077749'
- // ]
- // ],
- // [
- // 'name' => '文昂文苑',
- // 'app_id' => 'wxd68aa9126e5da8d5',
- // 'account_id' => '19501200',
- // 'access_token' => '82d04ab670f68383cca53fe6fd65bcce',
- // 'ids' => [
- // '3722252058','3482476896','3738457288','3738453679','3752160764'
- // ]
- // ],
- // [
- // 'name' => '鼎轩文苑',
- // 'app_id' => 'wx437e7709c45b7dce',
- // 'account_id' => '19465492',
- // 'access_token' => 'ff02641d1ae1ca0409f08f06417f6aa9',
- // 'ids' => [
- // '3492350273'
- // ]
- // ],
- // [
- // 'name' => '紫英阅读',
- // 'app_id' => 'wx9b530e5cfba90252',
- // 'account_id' => '17072260',
- // 'access_token' => 'b74646ccf145cdb506e552e4bfbb08ad',
- // 'ids' => [
- // '3498526498','3492629088','3358470314','3358475484'
- // ]
- // ],
- // [
- // 'name' => '伽罗书卷',
- // 'app_id' => 'wx1f523ec0118e76e5',
- // 'account_id' => '16914719',
- // 'access_token' => '4cd80c3b2d248ef33dd08da9a8977074',
- // 'ids' => [
- // '2304579597','2309012962','2312108228','2304579374','2309496998','2312106960','2318243752','2304572752','2317489386','2336899094','2304575254','2318951575','2325029007','2320702587','2320966607','2306474839'
- // ]
- // ],
- // [
- // 'name' => '月半书刊',
- // 'app_id' => 'wx0d4b577ddc8dc3ed',
- // 'account_id' => '20058624',
- // 'access_token' => '1385e106817139923bc3ecdb181449c6',
- // 'ids' => [
- // '3680323931','3741629652'
- // ]
- // ],
- // [
- // 'name' => '粉黛文轩',
- // 'app_id' => 'wxfc7fa74bd319595d',
- // 'account_id' => '20002832',
- // 'access_token' => 'ada5534368c4f456168e9fedea7d13d0',
- // 'ids' => [
- // '3697633451','3631397307','3719975908','3684157971'
- // ]
- // ],
- // [
- // 'name' => '璃璎文舍',
- // 'app_id' => 'wx82fe5882ff942195',
- // 'account_id' => '18030306',
- // 'access_token' => 'd7486cd4f7959ff5048773b00d5297c4',
- // 'ids' => [
- // '3663730387','3721263637','3702332449','3642406082','3678125416'
- // ]
- // ],
- // [
- // 'name' => '灵狐文轩',
- // 'app_id' => 'wx0f9c4796a5fabc25',
- // 'account_id' => '17086893',
- // 'access_token' => 'ab8083c49436edf34ddc54e1e06c5ffb',
- // 'ids' => [
- // '3172400114','3124988906','3179248761','3127512150','3183978213','3124988900','3127512025','3134001511','3126166124','3172400117','3096077473','3124988903','3127512022','3126166127','3136276730','3136276519'
- // ]
- // ],
- [
- 'name' => '紫真文苑',
- 'app_id' => 'wx90353a310e7d816a',
- 'account_id' => '19501244',
- 'access_token' => '67ef5268acff4f3898ebd8ee1b937881',
- 'ids' => [
- '3543812549'
- ]
- ],
- // [
- // 'name' => '蓬絮文苑',
- // 'app_id' => 'wxc88eb13a301b5247',
- // 'account_id' => '17086911',
- // 'access_token' => '5e0c4602bfa0744984c050848628e967',
- // 'ids' => [
- // '3127509443','3109017903','3114222229','3109017912','3109017909','3091890757','3126034235','3109017915','3126029061','3114222226','3114222183','3108369724','3127510724','3126319377','3114222235','3127509440','3127510711','3126034244'
- // ]
- // ],
- // [
- // 'name' => '芒香文舍',
- // 'app_id' => 'wxbdda342641c5ac38',
- // 'account_id' => '18032966',
- // 'access_token' => 'b35d27244023a073359f7854255df126',
- // 'ids' => [
- // '3721733803','3667107780','3721601748','3721729385'
- // ]
- // ],
- // [
- // 'name' => '烟柳文轩',
- // 'app_id' => 'wxe6082ece55e16f47',
- // 'account_id' => '16043345',
- // 'access_token' => '0ec48054b0653f445febca9b421fc88b',
- // 'ids' => [
- // '3107500600','3108103115','3108103112'
- // ]
- // ],
- // [
- // 'name' => '义萝阅读',
- // 'app_id' => 'wx90c87d0cfd1d2ad5',
- // 'account_id' => '17959727',
- // 'access_token' => '05e76b22f8e5a0f94427bf99ef88942b',
- // 'ids' => [
- // '2403908204','3133483286','3180769828','3107728488','3113537264','2409786071'
- // ]
- // ],
- // [
- // 'name' => '青山文轩',
- // 'app_id' => 'wxb342de5d79b9f031',
- // 'account_id' => '16914671',
- // 'access_token' => '51b9eab0911a7d8c4910cdbaf34df91e',
- // 'ids' => [
- // '3210930161','3386977247','3252317943','3365528810','3360740635','3215839621','3274618255','3386761982','3279890303','3384645330','3210930161'
- // ]
- // ],
- // [
- // 'name' => '源木阅读',
- // 'app_id' => 'wx5d0036ee184f4e74',
- // 'account_id' => '15203640',
- // 'access_token' => 'b63d8495025f947d5a2ac726dc123bb5',
- // 'ids' => [
- // '3170019264','3124426595','3162107743','3138759357','3113806578'
- // ]
- // ],
- // [
- // 'name' => '孤月文斋',
- // 'app_id' => 'wx0481998ea3ebe675',
- // 'account_id' => '16892551',
- // 'access_token' => 'e2d3cbc2406635d33554d1a07d229210',
- // 'ids' => [
- // '2374321652','3588140736','3325982145','2369880993','2370469632','3173952288','3348464088','3327025355','3135941786','3324328471','2367774637','3342327605','3327025269'
- // ]
- // ],
- // [
- // 'name' => '酒巷阅读',
- // 'app_id' => 'wx964b6b5552868ca1',
- // 'account_id' => '17849964',
- // 'access_token' => '5bac05346161b9b0f9771140a743fd96',
- // 'ids' => [
- // '2348149430','2336862637','2347545925','2333830813','2349554736','2351386816'
- // ]
- // ],
- // [
- // 'name' => '晓秋书坊',
- // 'app_id' => 'wxefaf9011b7eead82',
- // 'account_id' => '17183738',
- // 'access_token' => '5663e85e092b4adfe89341de220991a3',
- // 'ids' => [
- // '3093201722','2387173542','2374370193','2387107530','2387107533','2337565111'
- // ]
- // ],
- [
- 'name' => '菱纱阅读',
- 'app_id' => 'wx1fa2597d5ce50c0e',
- 'account_id' => '17167175',
- 'access_token' => 'e98d10b5359f1028b1f043510ae115bb',
- 'ids' => [
- '3321841675'
- ]
- ],
- // [
- // 'name' => '红粉书馆',
- // 'app_id' => 'wxcf3b742a4e5e0624',
- // 'account_id' => '8014477',
- // 'access_token' => '1907cf83ec086773ded941191e6d7fbd',
- // 'ids' => [
- // '2290920004','2306024302','2157708192','2157648429','2173111511','2191933550','2156439529','2314913973','2307179545','2311851651','2159222757','2307274304','2173197309','2311765880','2173256621','2230522323','2310932112','2194401227','2195225864','2196397811','2306024092'
- // ]
- // ],
- // [
- // 'name' => '秋叶书斋',
- // 'app_id' => 'wx8f53476058a66c3c',
- // 'account_id' => '14074160',
- // 'access_token' => '85c3ee8620983b32d8ac3b646d3b4461',
- // 'ids' => [
- // '2294326208','2294325543','2300720067','2257503028','2150088954','2251928619','2290984767','2257731226','2300720070','2257501051','2155263884','2157739559','2290984764','2251928613','2153687850','2290984775'
- // ]
- // ],
- [
- 'name' => '天河悦文',
- 'app_id' => 'wx2f3a6ade774f90ab',
- 'account_id' => '15203319',
- 'access_token' => '23723d388094954d93f8055035b7fce9',
- 'ids' => [
- '2294062152','2298611516','2288408611','2298611523','2294062155','2169264110','2298611519','2294062149'
- ]
- ],
- // [
- // 'name' => '半竹文舍',
- // 'app_id' => 'wx90f96793b8b1a170',
- // 'account_id' => '17183722',
- // 'access_token' => '1ec836943aaa05cc75332994f378cef4',
- // 'ids' => [
- // '3358197848','3390302427','3214804753','3215466414','3269795820','3213969248','3358197848','3213969254','3390302427','3349684592'
- // ]
- // ],
- // [
- // 'name' => '毛豆文摘',
- // 'app_id' => 'wx3a3d4b5f94bb2249',
- // 'account_id' => '18032948',
- // 'access_token' => 'd0e5f50e23a7370a1438ccb16e2b8ac7',
- // 'ids' => [
- // '3358060075','3364695239','3354428540','3354428537','3218661633','3266986525','3358060075','3364695239','3261989246','3354428540','3354428537','3234987464','3234987467','3215027524','3219749267','3349657288','3215027527'
- // ]
- // ],
- // [
- // 'name' => '半秋读书',
- // 'app_id' => 'wxbc4019a68eecaa2a',
- // 'account_id' => '18030339',
- // 'access_token' => 'd5afd88b54937dfca394084677738c83',
- // 'ids' => [
- // '3244618571','3240909730','3252601507'
- // ]
- // ],
- // [
- // 'name' => '天河书斋',
- // 'app_id' => 'wx8277b63789861d3f',
- // 'account_id' => '14940322',
- // 'access_token' => '493d35ea988b7c83f363b4fd1021292a',
- // 'ids' => [
- // '3113998867','3102152912','3113041532'
- // ]
- // ],
- ];
- set_time_limit(0);
- ini_set('memory_limit', '1024M');
- foreach($account as $value) {
- $accessToken = $value['access_token'];
- $accountId = $value['account_id'];
- $ids = $value['ids'];
- $date = date('Y-m-d', strtotime('-30 day'));
- $pageSige = count($value['ids']) * 24;
- while($date < date('Y-m-d')) {
- $insert = [];
- $result = $this->hourly_reports_get($accessToken, $accountId, $ids, $date, $pageSige);
- $date = date('Y-m-d', strtotime($date.' +1 day'));
- echo '公众号:'.$value['name'].'在'.$date.'日的数据:'.$result."\r\n";
- $result = json_decode($result, 1);
- $list = $result['data']['list'];
- foreach($list as $item) {
- $insert['account_name'] = $value['name'];
- $insert['app_id'] = $value['app_id'];
- $insert['account_id'] = $value['account_id'];
- $insert['ad_id'] = $item['ad_id'];
- $insert['date'] = $date;
- $insert['hour'] = $item['hour'];
- $insert['view_count'] = $item['view_count'];
- $insert['type'] = 3;
- $this->insertData($insert);
- }
- }
- sleep(1);
- }
- }
- public function insertData($data)
- {
- \DB::table('data_scrape')->insert($data);
- }
- public function hourly_reports_get($access_token, $accountId, $Ids, $date, $pageSige)
- {
- $interface = 'hourly_reports/get';
- $url = 'https://api.e.qq.com/v1.3/' . $interface;
- $common_parameters = array (
- 'access_token' => $access_token,
- 'timestamp' => time(),
- 'nonce' => md5(uniqid('', true))
- );
- $parameters = array (
- 'account_id' => $accountId,
- 'level' => 'REPORT_LEVEL_AD_WECHAT',
- 'date_range' => array (
- 'start_date' => $date,
- 'end_date' => $date,
- ),
- 'filtering' => array (
- 0 => array (
- 'field' => 'ad_id',
- 'operator' => 'IN',
- 'values' => $Ids,
- ),
- ),
- 'page' => 1,
- 'page_size' => $pageSige,
- 'fields' => array (
- 'account_id',
- 'ad_id',
- 'hour',
- 'view_count'
- ),
- );
- $parameters = array_merge($common_parameters, $parameters);
- foreach ($parameters as $key => $value) {
- if (!is_string($value)) {
- $parameters[$key] = json_encode($value);
- }
- }
- $request_url = $url . '?' . http_build_query($parameters);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $request_url);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
- $response = curl_exec($curl);
- if (curl_error($curl)) {
- $error_msg = curl_error($curl);
- $error_no = curl_errno($curl);
- curl_close($curl);
- throw new \Exception($error_msg, $error_no);
- }
- curl_close($curl);
- return $response;
- }
- }
|