小说推广数据系统

MpHourlyReport.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\AdsDailyReport;
  5. use App\Models\UserAuthorization;
  6. use App\Services\HttpService;
  7. use App\Services\MpDailyReportService;
  8. use Illuminate\Console\Command;
  9. class MpHourlyReport extends Command
  10. {
  11. protected $signature = 'MpHourlyReport {type?} {date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
  12. protected $description = '微信广告主小时报表(临时处理数据脚本)';
  13. protected $type;
  14. protected $dateTime;
  15. protected $month;
  16. protected $page;
  17. protected $time;
  18. protected $date = false;
  19. protected $pageSize = 200;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. }
  24. public function handle()
  25. {
  26. \DB::connection()->disableQueryLog();
  27. $this->time = date('m-d H:i:s');
  28. $this->info($this->time . ' 开始整理');
  29. $this->type = $this->argument('type') ? $this->argument('type') : 2;
  30. $this->dateTime = $this->argument('date') ? $this->argument('date') : null;
  31. $this->getAccount();
  32. }
  33. public function getAccount()
  34. {
  35. $account = [
  36. // [
  37. // 'name' => '月牙读物',
  38. // 'app_id' => 'wx17217eb8c222ed4b',
  39. // 'account_id' => '18406620',
  40. // 'access_token' => '37ca72ff456fb7d555db4d44f281a409',
  41. // 'ids' => [
  42. // '3599099954','3633669596','3747907286','3620534295','3739536495','3747907283'
  43. // ]
  44. // ],
  45. // [
  46. // 'name' => '九州文轩',
  47. // 'app_id' => 'wx6135c83ac58ef5f9',
  48. // 'account_id' => '16816472',
  49. // 'access_token' => '09cb52b24115ec44c0459521a0a812ce',
  50. // 'ids' => [
  51. // '3732293407','3254491971','3243318720','3254360626','3264345785','3231473736','3613241533','3720738263','3726284475','3264344595'
  52. // ]
  53. // ],
  54. // [
  55. // 'name' => '豪门书刊',
  56. // 'app_id' => 'wx37bf23e4f99d9ede',
  57. // 'account_id' => '18410853',
  58. // 'access_token' => '9531f021143572d483df64445af3299b',
  59. // 'ids' => [
  60. // '3353524665','3343259915','3351858521','3351858536','3353524358','3355310072','3351858533','3343259924'
  61. // ]
  62. // ],
  63. // [
  64. // 'name' => '半冬文摘',
  65. // 'app_id' => 'wxbf38812d63d4aef1',
  66. // 'account_id' => '17301675',
  67. // 'access_token' => 'bf7c85c58e48ba9d3bc0d8616a4bcb34',
  68. // 'ids' => [
  69. // '3476216514','3387572895','3552914508','3515116675','3384388072','3525209641','3655140015','3384388072','3468959432','3457790610','3643460247','3554857745','3397052151','3538775776','3541812171','3581200476','3453108235','3421900432','3518075878','3363513751','3526135265','3616784902','3628807113','3526760103'
  70. // ]
  71. // ],
  72. // [
  73. // 'name' => '痴海文轩',
  74. // 'app_id' => 'wxaf1a7b478cc0d86e',
  75. // 'account_id' => '19450435',
  76. // 'access_token' => 'b5a305a544b6b740a9125c45c0b35a1a',
  77. // 'ids' => [
  78. // '3385518061','3385518067','3440632877'
  79. // ]
  80. // ],
  81. // [
  82. // 'name' => '亦双文苑',
  83. // 'app_id' => 'wx991b98c6c2ca6eed',
  84. // 'account_id' => '19450449',
  85. // 'access_token' => 'c03c3917b3ef68d82c0200f1a3221a65',
  86. // 'ids' => [
  87. // '3374243815','3376055171','3512748537','3385516712','3388583283','3515055296','3419716083','3433753501'
  88. // ]
  89. // ],
  90. // [
  91. // 'name' => '九州文舍',
  92. // 'app_id' => 'wxe88ea2b22e46da60',
  93. // 'account_id' => '16816510',
  94. // 'access_token' => '8af51f63c6a758a944db0a98fbabb16b',
  95. // 'ids' => [
  96. // '3525115575','2384727801','3525115578','2387814162','3525623594'
  97. // ]
  98. // ],
  99. // [
  100. // 'name' => '沐璎阅读',
  101. // 'app_id' => 'wx83cf8b4382125cc2',
  102. // 'account_id' => '18032879',
  103. // 'access_token' => '9ecb7badca363020d70f557acb6eb1a5',
  104. // 'ids' => [
  105. // '3510554235','3583095874','3523650197'
  106. // ]
  107. // ],
  108. [
  109. 'name' => '梦璃文苑',
  110. 'app_id' => 'wxbd06cf988b8994ad',
  111. 'account_id' => '17072282',
  112. 'access_token' => 'c19e178ee2be5c551902895b4e3ebe7b',
  113. 'ids' => [
  114. '2369112234','2369112228','2373148837','2373148840'
  115. ]
  116. ],
  117. // [
  118. // 'name' => '九霄文舍',
  119. // 'app_id' => 'wxdc8bd7f47fb5d5c1',
  120. // 'account_id' => '15203275',
  121. // 'access_token' => '4cb68ebed1dd4d7f0c38af0780d18568',
  122. // 'ids' => [
  123. // '3259707658','3647627623','240381751','3647627614'
  124. // ]
  125. // ],
  126. // [
  127. // 'name' => '纵横书斋',
  128. // 'app_id' => 'wx7ba2d755310209df',
  129. // 'account_id' => '14096389',
  130. // 'access_token' => '880bd0d440aecf350a14a3af31d0df43',
  131. // 'ids' => [
  132. // '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'
  133. // ]
  134. // ],
  135. [
  136. 'name' => '扳手阅读',
  137. 'app_id' => 'wx98d307e15b812a9e',
  138. 'account_id' => '18410879',
  139. 'access_token' => 'addc003a21c56295ae48fc471a7adcfa',
  140. 'ids' => [
  141. '2483730944','2488370515','2487115463','2489173339'
  142. ]
  143. ],
  144. // [
  145. // 'name' => '世离阅读',
  146. // 'app_id' => 'wx314f6929c50c2cf1',
  147. // 'account_id' => '17167180',
  148. // 'access_token' => '038f1df3652056696ba77a119788bab4',
  149. // 'ids' => [
  150. // '3264346395'
  151. // ]
  152. // ],
  153. // [
  154. // 'name' => '无岳文摘',
  155. // 'app_id' => 'wxeaf7f5eb94ea0a01',
  156. // 'account_id' => '17959720',
  157. // 'access_token' => '68a75a5ddee5a40b79fb0a7012169094',
  158. // 'ids' => [
  159. // '3474116597','3439636293','3468994242','3491258592','3439636296','3491258588'
  160. // ]
  161. // ],
  162. // [
  163. // 'name' => '碧芷文苑',
  164. // 'app_id' => 'wx3b0e971cba3dad42',
  165. // 'account_id' => '19465480',
  166. // 'access_token' => '60864acf0544c906471e935e402686c2',
  167. // 'ids' => [
  168. // '3468435965','3417077749'
  169. // ]
  170. // ],
  171. // [
  172. // 'name' => '文昂文苑',
  173. // 'app_id' => 'wxd68aa9126e5da8d5',
  174. // 'account_id' => '19501200',
  175. // 'access_token' => '82d04ab670f68383cca53fe6fd65bcce',
  176. // 'ids' => [
  177. // '3722252058','3482476896','3738457288','3738453679','3752160764'
  178. // ]
  179. // ],
  180. // [
  181. // 'name' => '鼎轩文苑',
  182. // 'app_id' => 'wx437e7709c45b7dce',
  183. // 'account_id' => '19465492',
  184. // 'access_token' => 'ff02641d1ae1ca0409f08f06417f6aa9',
  185. // 'ids' => [
  186. // '3492350273'
  187. // ]
  188. // ],
  189. // [
  190. // 'name' => '紫英阅读',
  191. // 'app_id' => 'wx9b530e5cfba90252',
  192. // 'account_id' => '17072260',
  193. // 'access_token' => 'b74646ccf145cdb506e552e4bfbb08ad',
  194. // 'ids' => [
  195. // '3498526498','3492629088','3358470314','3358475484'
  196. // ]
  197. // ],
  198. // [
  199. // 'name' => '伽罗书卷',
  200. // 'app_id' => 'wx1f523ec0118e76e5',
  201. // 'account_id' => '16914719',
  202. // 'access_token' => '4cd80c3b2d248ef33dd08da9a8977074',
  203. // 'ids' => [
  204. // '2304579597','2309012962','2312108228','2304579374','2309496998','2312106960','2318243752','2304572752','2317489386','2336899094','2304575254','2318951575','2325029007','2320702587','2320966607','2306474839'
  205. // ]
  206. // ],
  207. // [
  208. // 'name' => '月半书刊',
  209. // 'app_id' => 'wx0d4b577ddc8dc3ed',
  210. // 'account_id' => '20058624',
  211. // 'access_token' => '1385e106817139923bc3ecdb181449c6',
  212. // 'ids' => [
  213. // '3680323931','3741629652'
  214. // ]
  215. // ],
  216. // [
  217. // 'name' => '粉黛文轩',
  218. // 'app_id' => 'wxfc7fa74bd319595d',
  219. // 'account_id' => '20002832',
  220. // 'access_token' => 'ada5534368c4f456168e9fedea7d13d0',
  221. // 'ids' => [
  222. // '3697633451','3631397307','3719975908','3684157971'
  223. // ]
  224. // ],
  225. // [
  226. // 'name' => '璃璎文舍',
  227. // 'app_id' => 'wx82fe5882ff942195',
  228. // 'account_id' => '18030306',
  229. // 'access_token' => 'd7486cd4f7959ff5048773b00d5297c4',
  230. // 'ids' => [
  231. // '3663730387','3721263637','3702332449','3642406082','3678125416'
  232. // ]
  233. // ],
  234. // [
  235. // 'name' => '灵狐文轩',
  236. // 'app_id' => 'wx0f9c4796a5fabc25',
  237. // 'account_id' => '17086893',
  238. // 'access_token' => 'ab8083c49436edf34ddc54e1e06c5ffb',
  239. // 'ids' => [
  240. // '3172400114','3124988906','3179248761','3127512150','3183978213','3124988900','3127512025','3134001511','3126166124','3172400117','3096077473','3124988903','3127512022','3126166127','3136276730','3136276519'
  241. // ]
  242. // ],
  243. [
  244. 'name' => '紫真文苑',
  245. 'app_id' => 'wx90353a310e7d816a',
  246. 'account_id' => '19501244',
  247. 'access_token' => '67ef5268acff4f3898ebd8ee1b937881',
  248. 'ids' => [
  249. '3543812549'
  250. ]
  251. ],
  252. // [
  253. // 'name' => '蓬絮文苑',
  254. // 'app_id' => 'wxc88eb13a301b5247',
  255. // 'account_id' => '17086911',
  256. // 'access_token' => '5e0c4602bfa0744984c050848628e967',
  257. // 'ids' => [
  258. // '3127509443','3109017903','3114222229','3109017912','3109017909','3091890757','3126034235','3109017915','3126029061','3114222226','3114222183','3108369724','3127510724','3126319377','3114222235','3127509440','3127510711','3126034244'
  259. // ]
  260. // ],
  261. // [
  262. // 'name' => '芒香文舍',
  263. // 'app_id' => 'wxbdda342641c5ac38',
  264. // 'account_id' => '18032966',
  265. // 'access_token' => 'b35d27244023a073359f7854255df126',
  266. // 'ids' => [
  267. // '3721733803','3667107780','3721601748','3721729385'
  268. // ]
  269. // ],
  270. // [
  271. // 'name' => '烟柳文轩',
  272. // 'app_id' => 'wxe6082ece55e16f47',
  273. // 'account_id' => '16043345',
  274. // 'access_token' => '0ec48054b0653f445febca9b421fc88b',
  275. // 'ids' => [
  276. // '3107500600','3108103115','3108103112'
  277. // ]
  278. // ],
  279. // [
  280. // 'name' => '义萝阅读',
  281. // 'app_id' => 'wx90c87d0cfd1d2ad5',
  282. // 'account_id' => '17959727',
  283. // 'access_token' => '05e76b22f8e5a0f94427bf99ef88942b',
  284. // 'ids' => [
  285. // '2403908204','3133483286','3180769828','3107728488','3113537264','2409786071'
  286. // ]
  287. // ],
  288. // [
  289. // 'name' => '青山文轩',
  290. // 'app_id' => 'wxb342de5d79b9f031',
  291. // 'account_id' => '16914671',
  292. // 'access_token' => '51b9eab0911a7d8c4910cdbaf34df91e',
  293. // 'ids' => [
  294. // '3210930161','3386977247','3252317943','3365528810','3360740635','3215839621','3274618255','3386761982','3279890303','3384645330','3210930161'
  295. // ]
  296. // ],
  297. // [
  298. // 'name' => '源木阅读',
  299. // 'app_id' => 'wx5d0036ee184f4e74',
  300. // 'account_id' => '15203640',
  301. // 'access_token' => 'b63d8495025f947d5a2ac726dc123bb5',
  302. // 'ids' => [
  303. // '3170019264','3124426595','3162107743','3138759357','3113806578'
  304. // ]
  305. // ],
  306. // [
  307. // 'name' => '孤月文斋',
  308. // 'app_id' => 'wx0481998ea3ebe675',
  309. // 'account_id' => '16892551',
  310. // 'access_token' => 'e2d3cbc2406635d33554d1a07d229210',
  311. // 'ids' => [
  312. // '2374321652','3588140736','3325982145','2369880993','2370469632','3173952288','3348464088','3327025355','3135941786','3324328471','2367774637','3342327605','3327025269'
  313. // ]
  314. // ],
  315. // [
  316. // 'name' => '酒巷阅读',
  317. // 'app_id' => 'wx964b6b5552868ca1',
  318. // 'account_id' => '17849964',
  319. // 'access_token' => '5bac05346161b9b0f9771140a743fd96',
  320. // 'ids' => [
  321. // '2348149430','2336862637','2347545925','2333830813','2349554736','2351386816'
  322. // ]
  323. // ],
  324. // [
  325. // 'name' => '晓秋书坊',
  326. // 'app_id' => 'wxefaf9011b7eead82',
  327. // 'account_id' => '17183738',
  328. // 'access_token' => '5663e85e092b4adfe89341de220991a3',
  329. // 'ids' => [
  330. // '3093201722','2387173542','2374370193','2387107530','2387107533','2337565111'
  331. // ]
  332. // ],
  333. [
  334. 'name' => '菱纱阅读',
  335. 'app_id' => 'wx1fa2597d5ce50c0e',
  336. 'account_id' => '17167175',
  337. 'access_token' => 'e98d10b5359f1028b1f043510ae115bb',
  338. 'ids' => [
  339. '3321841675'
  340. ]
  341. ],
  342. // [
  343. // 'name' => '红粉书馆',
  344. // 'app_id' => 'wxcf3b742a4e5e0624',
  345. // 'account_id' => '8014477',
  346. // 'access_token' => '1907cf83ec086773ded941191e6d7fbd',
  347. // 'ids' => [
  348. // '2290920004','2306024302','2157708192','2157648429','2173111511','2191933550','2156439529','2314913973','2307179545','2311851651','2159222757','2307274304','2173197309','2311765880','2173256621','2230522323','2310932112','2194401227','2195225864','2196397811','2306024092'
  349. // ]
  350. // ],
  351. // [
  352. // 'name' => '秋叶书斋',
  353. // 'app_id' => 'wx8f53476058a66c3c',
  354. // 'account_id' => '14074160',
  355. // 'access_token' => '85c3ee8620983b32d8ac3b646d3b4461',
  356. // 'ids' => [
  357. // '2294326208','2294325543','2300720067','2257503028','2150088954','2251928619','2290984767','2257731226','2300720070','2257501051','2155263884','2157739559','2290984764','2251928613','2153687850','2290984775'
  358. // ]
  359. // ],
  360. [
  361. 'name' => '天河悦文',
  362. 'app_id' => 'wx2f3a6ade774f90ab',
  363. 'account_id' => '15203319',
  364. 'access_token' => '23723d388094954d93f8055035b7fce9',
  365. 'ids' => [
  366. '2294062152','2298611516','2288408611','2298611523','2294062155','2169264110','2298611519','2294062149'
  367. ]
  368. ],
  369. // [
  370. // 'name' => '半竹文舍',
  371. // 'app_id' => 'wx90f96793b8b1a170',
  372. // 'account_id' => '17183722',
  373. // 'access_token' => '1ec836943aaa05cc75332994f378cef4',
  374. // 'ids' => [
  375. // '3358197848','3390302427','3214804753','3215466414','3269795820','3213969248','3358197848','3213969254','3390302427','3349684592'
  376. // ]
  377. // ],
  378. // [
  379. // 'name' => '毛豆文摘',
  380. // 'app_id' => 'wx3a3d4b5f94bb2249',
  381. // 'account_id' => '18032948',
  382. // 'access_token' => 'd0e5f50e23a7370a1438ccb16e2b8ac7',
  383. // 'ids' => [
  384. // '3358060075','3364695239','3354428540','3354428537','3218661633','3266986525','3358060075','3364695239','3261989246','3354428540','3354428537','3234987464','3234987467','3215027524','3219749267','3349657288','3215027527'
  385. // ]
  386. // ],
  387. // [
  388. // 'name' => '半秋读书',
  389. // 'app_id' => 'wxbc4019a68eecaa2a',
  390. // 'account_id' => '18030339',
  391. // 'access_token' => 'd5afd88b54937dfca394084677738c83',
  392. // 'ids' => [
  393. // '3244618571','3240909730','3252601507'
  394. // ]
  395. // ],
  396. // [
  397. // 'name' => '天河书斋',
  398. // 'app_id' => 'wx8277b63789861d3f',
  399. // 'account_id' => '14940322',
  400. // 'access_token' => '493d35ea988b7c83f363b4fd1021292a',
  401. // 'ids' => [
  402. // '3113998867','3102152912','3113041532'
  403. // ]
  404. // ],
  405. ];
  406. set_time_limit(0);
  407. ini_set('memory_limit', '1024M');
  408. foreach($account as $value) {
  409. $accessToken = $value['access_token'];
  410. $accountId = $value['account_id'];
  411. $ids = $value['ids'];
  412. $date = date('Y-m-d', strtotime('-30 day'));
  413. $pageSige = count($value['ids']) * 24;
  414. while($date < date('Y-m-d')) {
  415. $insert = [];
  416. $result = $this->hourly_reports_get($accessToken, $accountId, $ids, $date, $pageSige);
  417. $date = date('Y-m-d', strtotime($date.' +1 day'));
  418. echo '公众号:'.$value['name'].'在'.$date.'日的数据:'.$result."\r\n";
  419. $result = json_decode($result, 1);
  420. $list = $result['data']['list'];
  421. foreach($list as $item) {
  422. $insert['account_name'] = $value['name'];
  423. $insert['app_id'] = $value['app_id'];
  424. $insert['account_id'] = $value['account_id'];
  425. $insert['ad_id'] = $item['ad_id'];
  426. $insert['date'] = $date;
  427. $insert['hour'] = $item['hour'];
  428. $insert['view_count'] = $item['view_count'];
  429. $insert['type'] = 3;
  430. $this->insertData($insert);
  431. }
  432. }
  433. sleep(1);
  434. }
  435. }
  436. public function insertData($data)
  437. {
  438. \DB::table('data_scrape')->insert($data);
  439. }
  440. public function hourly_reports_get($access_token, $accountId, $Ids, $date, $pageSige)
  441. {
  442. $interface = 'hourly_reports/get';
  443. $url = 'https://api.e.qq.com/v1.3/' . $interface;
  444. $common_parameters = array (
  445. 'access_token' => $access_token,
  446. 'timestamp' => time(),
  447. 'nonce' => md5(uniqid('', true))
  448. );
  449. $parameters = array (
  450. 'account_id' => $accountId,
  451. 'level' => 'REPORT_LEVEL_AD_WECHAT',
  452. 'date_range' => array (
  453. 'start_date' => $date,
  454. 'end_date' => $date,
  455. ),
  456. 'filtering' => array (
  457. 0 => array (
  458. 'field' => 'ad_id',
  459. 'operator' => 'IN',
  460. 'values' => $Ids,
  461. ),
  462. ),
  463. 'page' => 1,
  464. 'page_size' => $pageSige,
  465. 'fields' => array (
  466. 'account_id',
  467. 'ad_id',
  468. 'hour',
  469. 'view_count'
  470. ),
  471. );
  472. $parameters = array_merge($common_parameters, $parameters);
  473. foreach ($parameters as $key => $value) {
  474. if (!is_string($value)) {
  475. $parameters[$key] = json_encode($value);
  476. }
  477. }
  478. $request_url = $url . '?' . http_build_query($parameters);
  479. $curl = curl_init();
  480. curl_setopt($curl, CURLOPT_URL, $request_url);
  481. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
  482. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  483. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  484. $response = curl_exec($curl);
  485. if (curl_error($curl)) {
  486. $error_msg = curl_error($curl);
  487. $error_no = curl_errno($curl);
  488. curl_close($curl);
  489. throw new \Exception($error_msg, $error_no);
  490. }
  491. curl_close($curl);
  492. return $response;
  493. }
  494. }