BookUpdateOne.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GuzzleHttp\Client;
  5. use App\Modules\Book\Models\Book;
  6. use App\Modules\Book\Models\Chapter;
  7. use App\Modules\Book\Services\ChapterService;
  8. use App\Modules\Book\Services\BookConfigService;
  9. use DB;
  10. use Log;
  11. class BookUpdateOne extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'book:updateone
  19. {--bid= : the id of a book}
  20. {--content}
  21. {--start=}
  22. {--end=}
  23. {--withname}';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = '更新图书章节内容';
  30. private $client;
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. $this->client = new Client(['timeout' => 8.0,'allow_redirects'=>true]);
  40. }
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return mixed
  45. */
  46. public function handle()
  47. {
  48. $bid = $this->option('bid');
  49. if(empty($bid)) return 0;
  50. $content = $this->option('content');
  51. if($content){
  52. $start = $this->option('start');
  53. $end = $this->option('end');
  54. $withname = $this->option('withname');
  55. return $this->updateExistContent($bid,$start,$end,$withname);
  56. }
  57. return $this->updateOne($bid);
  58. }
  59. /**
  60. * 更新章节内容 一本书的
  61. * @param $bid
  62. * @return int|string
  63. */
  64. public function updateOne($bid){
  65. set_time_limit(0);
  66. $book_info = Book::find($bid);
  67. if(empty($book_info) || empty($book_info->ly_bid)) return -1;
  68. $this->bookStatus($bid,$book_info->ly_bid);
  69. //$max_sequence = Chapter::where('bid',$bid)->max('sequence');
  70. //$max_sequence = $max_sequence?$max_sequence:-1;
  71. //$max_sequence_chapter = Chapter::where('bid',$bid)->where('sequence',$max_sequence)->select('id')->first();
  72. //$max_sequence_chapter_id = isset($max_sequence_chapter->id)?$max_sequence_chapter->id:0;
  73. $last_chapter = Chapter::where('bid',$bid)
  74. ->select('id','name','sequence','prev_cid','next_cid')
  75. ->orderBy('sequence','desc')
  76. ->first();
  77. $last_chapter_from_third = Chapter::where('bid',$bid)
  78. ->where('ly_chapter_id','>',0)
  79. ->select('id','name','sequence','prev_cid','next_cid','ly_chapter_id')
  80. ->orderBy('sequence','desc')
  81. ->first();
  82. $max_sequence_chapter_id = isset($last_chapter->id)?$last_chapter->id:0;
  83. $max_sequence = isset($last_chapter->sequence)?$last_chapter->sequence:0;
  84. $from_sequence = $max_sequence;
  85. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  86. $res = $this->client->get(sprintf($chapter_list_fromat,$book_info->ly_bid));
  87. $res = $res->getBody()->getContents();
  88. if(!$res) return '';
  89. $book_config_info = DB::table('book_configs')->where('bid',$bid)->first();
  90. $res = json_decode($res,true);
  91. $j = 0;
  92. $size = 0;
  93. $local_count = Chapter::where('bid',$bid)->where('ly_chapter_id','>',0)->count();
  94. $remote_count = 0;
  95. foreach ($res['data'] as $item1) {
  96. $remote_count += count($item1['chapters']);
  97. $item1 = null;
  98. }
  99. if($remote_count == $local_count) return '';
  100. $start = false;
  101. if(!$last_chapter){
  102. $start = true;
  103. }
  104. $ly_last_chapter_id = 0;
  105. if($last_chapter_from_third && isset($last_chapter_from_third->ly_chapter_id)){
  106. $ly_last_chapter_id = $last_chapter_from_third->ly_chapter_id;
  107. }
  108. foreach ($res['data'] as $v1) {
  109. foreach ($v1['chapters'] as $v) {
  110. /*if ($v['chapter_order_number'] <= $max_sequence-1) {
  111. continue;
  112. }*/
  113. if(!$start){
  114. if($v['chapter_id'] != $ly_last_chapter_id){
  115. continue;
  116. }else{
  117. \Log::info($v1);
  118. $start = true;
  119. continue;
  120. }
  121. }
  122. /*if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
  123. continue;
  124. }*/
  125. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  126. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $book_info->ly_bid, $v['chapter_id']));
  127. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  128. $temp = [
  129. 'bid' => $bid,
  130. 'name' => $v['chapter_name'],
  131. 'sequence' => ++$max_sequence,
  132. 'is_vip' => $v['chapter_need_pay'],
  133. 'prev_cid' => $max_sequence_chapter_id,
  134. 'next_cid' => '',
  135. 'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
  136. 'ly_chapter_id' => $v['chapter_id']
  137. ];
  138. if ($cahpter_content) {
  139. $cahpter_content = json_decode($cahpter_content, true);
  140. $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  141. $temp['content'] = $cahpter_content['data']['chapter_content'];
  142. $size += $temp['size'];
  143. }
  144. $insert_res = Chapter::create($temp);
  145. Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
  146. $max_sequence_chapter_id = $insert_res->id;
  147. $temp = null;
  148. $j++;
  149. }
  150. }
  151. $chapter_count = Chapter::where('bid',$bid)->count();
  152. $chapter_size = Chapter::where('bid',$bid)->sum('size');
  153. if($j>0){
  154. Book::where('id',$bid)->update(['chapter_count'=>$chapter_count,'last_cid'=>$max_sequence_chapter_id,'size'=>$chapter_size,'last_chapter'=>$v['chapter_name']]);
  155. $this->recordUpdateInfo($bid,[
  156. 'book_name'=>$book_config_info->book_name,
  157. 'update_chapter_count'=>$j,
  158. 'update_words'=>$size,
  159. 'update_type'=>'add_chapter',
  160. 'channel_name'=>$book_info->category_id >=13 ? '女频':'男频'
  161. ]);
  162. //ChapterService::splitContentAll($bid,$from_sequence);
  163. }
  164. return $j;
  165. }
  166. private function bookStatus($bid,$ly_bid){
  167. $status = 0;
  168. try{
  169. $book_info_url_format = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
  170. $res = $this->client->get(sprintf($book_info_url_format,$ly_bid));
  171. $res = $res->getBody()->getContents();
  172. if($res){
  173. $res = json_decode($res,true);
  174. if(isset($res['data']) && isset($res['data']['book_state'])){
  175. $status = $res['data']['book_state'];
  176. }
  177. }
  178. }catch (\Exception $e){
  179. }
  180. if($status){
  181. Book::where('id',$bid)->update(['status'=>$status]);
  182. }
  183. }
  184. private function updateExistContent($bid,$start=0,$end=0,$name=false){
  185. $book_info = Book::find($bid);
  186. if(empty($book_info) || empty($book_info->ly_bid)) return -1;
  187. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  188. $res = $this->client->get(sprintf($chapter_list_fromat,$book_info->ly_bid));
  189. $res = $res->getBody()->getContents();
  190. if(!$res) return '';
  191. $res = json_decode($res,true);
  192. if(!isset($res['data']))
  193. return '';
  194. foreach ($res['data'] as $v1){
  195. foreach ($v1['chapters'] as $v){
  196. if($start && ($v['chapter_order_number']+1<$start) ){
  197. continue;
  198. }
  199. if($end && ($v['chapter_order_number']+1>$end)){
  200. break;
  201. }
  202. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  203. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $book_info->ly_bid, $v['chapter_id']));
  204. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  205. $cahpter_content = json_decode($cahpter_content, true);
  206. if($cahpter_content && isset( $cahpter_content['data']['chapter_content']) && !empty( $cahpter_content['data']['chapter_content'])){
  207. //$temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  208. //$temp['content'] = $cahpter_content['data']['chapter_content'];
  209. $chapter = Chapter::where('bid', $bid)->where('sequence', $v['chapter_order_number']+1)->first();
  210. if($chapter){
  211. $chapter->content = $cahpter_content['data']['chapter_content'];
  212. if($name){
  213. $chapter->name =$v['chapter_name'];
  214. }
  215. $chapter->size = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  216. ChapterService::editChapter($chapter);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. private function recordUpdateInfo($bid, $data)
  223. {
  224. // 2 book_updates 的更新记录
  225. DB::table('book_updates')->insert([
  226. 'bid' => $bid,
  227. 'book_name' => $data['book_name'],
  228. 'channel_name' => $data['channel_name'],
  229. 'update_date' => date('Y-m-d'),
  230. 'update_chapter_count' => $data['update_chapter_count'],
  231. 'update_words' => $data['update_words'],
  232. 'update_type' => $data['update_type'],
  233. 'created_at' => date('Y-m-d H:i:s'),
  234. 'updated_at' => date('Y-m-d H:i:s')
  235. ]);
  236. }
  237. }