admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
package com.newvideo.youku;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Component;
 
import com.newvideo.dao.CategoryVideoDao;
import com.newvideo.domain.CategoryVideo;
import com.newvideo.domain.VideoDetailInfo;
import com.newvideo.domain.VideoInfo;
import com.newvideo.service.imp.VideoResourceService;
import com.newvideo.service.imp.VideoService;
import com.newvideo.service.imp.juhe.YouKuService;
import com.newvideo.util.JuheVideoUtil;
import com.newvideo.util.NumberUtil;
import com.newvideo.util.StringUtil;
import com.newvideo.util.TimeUtil;
import com.newvideo.youku.entity.Program;
import com.newvideo.youku.entity.ProgramVideo;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Component
public class YouKuUtil {
    @Resource
    private YouKuService youKuService;
    @Resource
    private VideoService videoService;
    @Resource
    private VideoResourceService videoResourceService;
 
    @Resource
    private CategoryVideoDao categoryVideoDao;
 
    public VideoResourceService getVideoResourceService() {
        return videoResourceService;
    }
 
    public void setVideoResourceService(VideoResourceService videoResourceService) {
        this.videoResourceService = videoResourceService;
    }
 
    public VideoService getVideoService() {
        return videoService;
    }
 
    public void setVideoService(VideoService videoService) {
        this.videoService = videoService;
    }
 
    public YouKuService getYouKuService() {
        return youKuService;
    }
 
    public void setYouKuService(YouKuService youKuService) {
        this.youKuService = youKuService;
    }
 
    public static VideoDetailInfo convertProgramVideoToVideoDetail(ProgramVideo pv, Program p) {
        VideoDetailInfo vinfo = new VideoDetailInfo();
        vinfo.setExtraId(pv.getId());
        vinfo.setName(pv.getRc_title());
        if ("纪录片".equalsIgnoreCase(p.getCategory()) || "综艺".equalsIgnoreCase(p.getCategory()))
            vinfo.setTag(pv.getTitle());
        else
            vinfo.setTag(pv.getStage());
        vinfo.setType("youku");
        return vinfo;
    }
 
    public static List<ProgramVideo> getProgramVideoList(String pid) {
        return programVideoList(pid);
    }
 
    public static VideoInfo convertProgramToVideoInfo(Program p) {
        String time = p.getReleased();
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(TimeUtil.convertDateToTemp(time));
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH) + 1;
        int day = ca.get(Calendar.DAY_OF_MONTH);
        String director = "";
        String mainActor = "";
        if (!StringUtil.isNullOrEmpty(p.getAttrStr())) {
            JSONObject root = JSONObject.fromObject(p.getAttrStr());
            JSONArray da = root.optJSONArray("director");
            if (da != null)
                for (int i = 0; i < da.size(); i++) {
                    director += da.optJSONObject(i).optString("name") + " ";
                }
 
            if (director.endsWith(" "))
                director = director.substring(0, director.length() - 1);
 
            da = root.optJSONArray("performer");
            if (da != null)
                for (int i = 0; i < da.size(); i++) {
                    mainActor += da.optJSONObject(i).optString("name") + " ";
                }
 
            mainActor = (mainActor + "").trim();
        }
        String score = NumberUtil.get1PointNumber(Double.parseDouble(p.getScore()));
 
        String tag = "";
        if (p.getCategory().contains("电影")) {
            tag = "评分:" + score;
        } else if (p.getCategory().contains("电视剧")) {
            if (p.getCompleted() == 0) {// 未更新完
                tag = "更新至" + p.getEpisode_updated() + "集";
            } else if (p.getCompleted() == 1) {// 更新完
                tag = p.getEpisode_count() + "集全";
            }
        } else if (p.getCategory().contains("综艺")) {
            Comparator<ProgramVideo> cm = new Comparator<ProgramVideo>() {
                public int compare(ProgramVideo o1, ProgramVideo o2) {
                    long t2 = Long.parseLong(o2.getStage());
                    long t1 = Long.parseLong(o1.getStage());
                    return (int) (t2 - t1);// 倒序排列
                }
            };
            Collections.sort(p.getVideoList(), cm);
            String t = "";
            long ti = TimeUtil.convertGernalTime(p.getVideoList().get(0).getStage(), "yyyyMMdd");
            if (ti > 0)
                t = TimeUtil.getGernalTime(ti, "yyyy-MM-dd");
            else
                t = p.getVideoList().get(0).getStage();
            tag = "更新至" + t;
        } else if (p.getCategory().contains("动漫")) {
            if (Integer.parseInt(p.getEpisode_count()) <= 1) {
                tag = "评分:" + score;
            } else {
                if (p.getCompleted() == 0) {// 未更新完
                    tag = "更新至" + p.getEpisode_updated() + "集";
                } else if (p.getCompleted() == 1) {// 更新完
                    tag = p.getEpisode_count() + "集全";
                }
            }
        } else if (p.getCategory().contains("纪录片") || p.getCategory().contains("纪实")) {
            Comparator<ProgramVideo> cm = new Comparator<ProgramVideo>() {
                public int compare(ProgramVideo o1, ProgramVideo o2) {
                    long t2 = Long.parseLong(o2.getStage());
                    long t1 = Long.parseLong(o1.getStage());
                    return (int) (t2 - t1);// 倒序排列
                }
            };
            Collections.sort(p.getVideoList(), cm);
            String t = "";
            long ti = TimeUtil.convertGernalTime(p.getVideoList().get(0).getStage(), "yyyyMMdd");
            if (ti > 0)
                t = TimeUtil.getGernalTime(ti, "yyyy-MM-dd");
            else
                t = p.getVideoList().get(0).getStage();
            tag = "更新至" + t;
 
        }
 
        Comparator<ProgramVideo> cm = new Comparator<ProgramVideo>() {
            public int compare(ProgramVideo o1, ProgramVideo o2) {
                long t2 = TimeUtil.convertGernalTime(o2.getPublished(), "yyyy-MM-dd HH:mm:ss");
                long t1 = TimeUtil.convertGernalTime(o1.getPublished(), "yyyy-MM-dd HH:mm:ss");
                return (int) (t2 - t1);// 倒序排列
            }
        };
 
        Collections.sort(p.getVideoList(), cm);
        String latestHpicture = StringUtil.isNullOrEmpty(p.getVideoList().get(0).getBigThumbnail())
                ? p.getVideoList().get(0).getThumbnail_v2() : p.getVideoList().get(0).getBigThumbnail();
        long updateTime = TimeUtil.convertGernalTime(time, "yyyy-MM-dd");
 
        VideoInfo vi = new VideoInfo();
        vi.setArea(p.getArea());
        vi.setBaseurl(p.getLink());
        vi.setCanSave(false);
        vi.setCommentCount(0);
        vi.setContentType(1);
        vi.setCreatetime(System.currentTimeMillis());
        vi.setDay(day + "");
        vi.setDirector(director);
        vi.setFocus("");
        vi.setHpicture(p.getThumbnail_large());
        vi.setIntroduction(p.getDescription());
        vi.setLatestHpicture(latestHpicture);
        vi.setLatestVpicture("");
        vi.setMainActor(mainActor);
        vi.setMonth(month + "");
        vi.setName(p.getName());
        vi.setNowNumber(0 + "");
        vi.setOrderby("0");
        vi.setPicture(p.getPoster_large());
        vi.setScore(score);
        vi.setShare("0");
        vi.setShow(1 + "");
        vi.setTag(tag);
        vi.setVpicture(p.getPoster_large());
        vi.setWatchCount(0 + "");
        vi.setYear(year + "");
        vi.setUpdatetime(updateTime + "");
        vi.setVideocount(p.getVideoList() != null ? p.getVideoList().size() : 1);
        return vi;
    }
 
    public static VideoInfo convertVideoToVideoInfo(ProgramVideo p) {
        String time = p.getPublished();
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(TimeUtil.convertDateToTemp2(time));
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH) + 1;
        int day = ca.get(Calendar.DAY_OF_MONTH);
        String score = "9.0";
        String tag = "";
        if (!StringUtil.isNullOrEmpty(p.getDuration())) {
            tag = NumberUtil.convertSecondToString((int) Double.parseDouble(p.getDuration()));
        }
        long updateTime = ca.getTimeInMillis();
        VideoInfo vi = new VideoInfo();
        vi.setArea("");
        vi.setBaseurl(p.getLink());
        vi.setCanSave(false);
        vi.setCommentCount(0);
        vi.setContentType(1);
        vi.setCreatetime(System.currentTimeMillis());
        vi.setDay(day + "");
        vi.setDirector("");
        vi.setFocus("");
        vi.setHpicture(StringUtil.isNullOrEmpty(p.getBigThumbnail()) ? p.getThumbnail() : p.getBigThumbnail());
        vi.setIntroduction("");
        vi.setLatestHpicture("");
        vi.setLatestVpicture("");
        vi.setMainActor("");
        vi.setMonth(month + "");
        vi.setName(p.getTitle());
        vi.setNowNumber(0 + "");
        vi.setOrderby("0");
        vi.setPicture(p.getThumbnail());
        vi.setScore(score);
        vi.setShare("0");
        vi.setShow(1 + "");
        vi.setTag(tag);
        vi.setVpicture(StringUtil.isNullOrEmpty(p.getBigThumbnail()) ? p.getThumbnail() : p.getBigThumbnail());
        vi.setWatchCount(0 + "");
        vi.setYear(year + "");
        vi.setUpdatetime(updateTime + "");
        vi.setVideocount(1);
        return vi;
    }
 
    public static int getProgramType(Program p, String genra) {
        if (p.getCategory().equalsIgnoreCase("电影")) {
            if ("短片".equalsIgnoreCase(genra)) {
                return 281;
            } else if ("优酷出品".equalsIgnoreCase(genra)) {
                return 281;
            } else if ("纪录片".equalsIgnoreCase(genra))// 科技->纪录片
                return 284;
            return 151;
 
        } else if (p.getCategory().equalsIgnoreCase("电视剧")) {
            return 150;
        } else if (p.getCategory().equalsIgnoreCase("综艺")) {
            // 排除 美食 旅游 汽车 时尚 搞笑
            if (!genra.equalsIgnoreCase("美食") && !genra.equalsIgnoreCase("旅游") && !genra.equalsIgnoreCase("汽车")
                    && !genra.equalsIgnoreCase("时尚") && !genra.equalsIgnoreCase("搞笑")
                    && !genra.equalsIgnoreCase("优酷出品")) {
                JSONObject tv = JSONObject.fromObject(p.getAttrStr()).optJSONObject("tv_station");
                // 是否包含电视台
                if (tv != null && !StringUtil.isNullOrEmpty(tv.optString("name"))) {// 直接加入综艺
                    return 152;
                } else {
                    if (genra.equalsIgnoreCase("生活"))
                        return 230;
 
                    if (genra.equalsIgnoreCase("脱口秀"))
                        return 285;
 
                    // 不是电视台的,待定
                    return 0;
                }
            } else {// 处理 美食 旅游 汽车 时尚 搞笑
                if (genra.equalsIgnoreCase("搞笑"))
                    return 216;
                if (genra.equalsIgnoreCase("美食")) {
                    JSONObject tv = JSONObject.fromObject(p.getAttrStr()).optJSONObject("tv_station");
                    // 不包含电视台
                    if (tv == null || StringUtil.isNullOrEmpty(tv.optString("name"))) {
                        return 228;
                    }
                }
 
                if (genra.equalsIgnoreCase("旅游")) {
                    JSONObject tv = JSONObject.fromObject(p.getAttrStr()).optJSONObject("tv_station");
                    // 不包含电视台
                    if (tv == null || StringUtil.isNullOrEmpty(tv.optString("name"))) {
                        return 229;
                    }
                }
 
                if (genra.equalsIgnoreCase("汽车")) {
                    JSONObject tv = JSONObject.fromObject(p.getAttrStr()).optJSONObject("tv_station");
                    // 不包含电视台
                    if (tv == null || StringUtil.isNullOrEmpty(tv.optString("name"))) {
                        return 226;
                    }
                }
 
                if (genra.equalsIgnoreCase("时尚")) {
                    JSONObject tv = JSONObject.fromObject(p.getAttrStr()).optJSONObject("tv_station");
                    // 不包含电视台
                    if (tv == null || StringUtil.isNullOrEmpty(tv.optString("name"))) {
                        return 232;
                    }
                }
 
                if (genra.equalsIgnoreCase("优酷出品")) {
                    return 219;
                }
 
            }
 
        } else if (p.getCategory().equalsIgnoreCase("动漫")) {
            return 153;
        } else if (p.getCategory().equalsIgnoreCase("音乐")) {// 此处不处理, 音乐放在小视频中处理
 
        } else if (p.getCategory().equalsIgnoreCase("纪录片")) {
            if (!genra.equalsIgnoreCase("军事"))
                return 284;
            else
                return 223;
 
        } else if (p.getCategory().equalsIgnoreCase("教育")) {
            return 225;
        }
        return 0;
    }
 
    public static List<Integer> getProgramVideoType(ProgramVideo p, String genra) {
        List<Integer> list = new ArrayList<Integer>();
 
        if (p.getCategory().equalsIgnoreCase("娱乐")) {
            if (genra.equalsIgnoreCase("曲艺"))
                list.add(217);
            else if (genra.equalsIgnoreCase("戏剧"))
                // list.add(0);
                ;
            else if (genra.equalsIgnoreCase("曲艺"))
                ;
            else if (genra.equalsIgnoreCase("艺术"))
                ;
            else if (genra.equalsIgnoreCase("魔术"))
                list.add(286);
            else
                list.add(219);
 
        } else if (p.getCategory().equalsIgnoreCase("资讯")) {
            if (genra.equalsIgnoreCase("科技资讯")) {
                list.add(222);
                list.add(244);
            } else if (genra.equalsIgnoreCase("军事资讯")) {
                list.add(223);
                list.add(243);
            } else if (genra.equalsIgnoreCase("财经资讯")) {
                list.add(289);
                list.add(227);
            } else if (genra.equalsIgnoreCase("社会资讯")) {
                list.add(242);
            } else if (genra.equalsIgnoreCase("生活资讯"))
                list.add(288);
            else if (genra.equalsIgnoreCase("时政资讯"))
                list.add(290);
            else if (genra.equalsIgnoreCase("法制"))
                list.add(291);
            else
                list.add(159);
 
        } else if (p.getCategory().equalsIgnoreCase("体育")) {
            if (genra.equalsIgnoreCase("健身")) {
                list.add(222);
                list.add(235);
            } else if (genra.equalsIgnoreCase("体育资讯")) {
                list.add(287);
            } else if (genra.equalsIgnoreCase("极限运动")) {
                list.add(234);
            } else if (genra.equalsIgnoreCase("跑酷")) {
                list.add(234);
            } else if (genra.equalsIgnoreCase("自行车")) {
                list.add(234);
            } else if (genra.equalsIgnoreCase("轮滑")) {
                list.add(234);
            } else if (genra.equalsIgnoreCase("滑板")) {
                list.add(234);
            } else if (genra.equalsIgnoreCase("篮球")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("CBA")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("足球")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("英超")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("大球运动")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("网球")) {
                list.add(292);
            } else if (genra.equalsIgnoreCase("小球运动")) {
                list.add(293);
            } else if (genra.equalsIgnoreCase("赛车")) {
                list.add(294);
            } else if (genra.equalsIgnoreCase("模型")) {
                list.add(294);
            } else if (genra.equalsIgnoreCase("武术")) {
                list.add(295);
            } else if (genra.equalsIgnoreCase("格斗")) {
                list.add(295);
            } else if (genra.equalsIgnoreCase("田径")) {
                list.add(296);
            } else if (genra.equalsIgnoreCase("水上项目")) {
                list.add(296);
            } else if (genra.equalsIgnoreCase("力量项目")) {
                list.add(296);
            } else if (genra.equalsIgnoreCase("冰雪项目")) {
                list.add(296);
            } else if (genra.equalsIgnoreCase("棋牌")) {
                list.add(297);
            } else if (genra.equalsIgnoreCase("射击")) {
                list.add(273);
            } else if (genra.equalsIgnoreCase("体育舞蹈")) {
                list.add(273);
            } else if (genra.equalsIgnoreCase("技巧")) {
                list.add(273);
            } else if (genra.equalsIgnoreCase("其他")) {
                list.add(273);
            }
 
        } else if (p.getCategory().equalsIgnoreCase("游戏")) {
            if (genra.equalsIgnoreCase("游戏资讯")) {
                list.add(272);
            } else if (genra.equalsIgnoreCase("游戏达人")) {
                list.add(212);
            } else if (genra.equalsIgnoreCase("魔兽世界")) {
                list.add(212);
            } else if (genra.equalsIgnoreCase("地下城与勇士")) {
                list.add(212);
            } else if (genra.equalsIgnoreCase("单机游戏")) {
                list.add(213);
            } else if (genra.equalsIgnoreCase("手机游戏")) {
                list.add(214);
            } else if (genra.equalsIgnoreCase("游戏音乐")) {
                list.add(298);
            }
            //
 
        } else if (p.getCategory().equalsIgnoreCase("搞笑")) {
            if (genra.equalsIgnoreCase("搞笑动物")) {
                list.add(216);
                list.add(220);
            } else
                list.add(216);
 
        } else if (p.getCategory().equalsIgnoreCase("生活")) {
            if (genra.equalsIgnoreCase("美食")) {
                list.add(228);
            } else if (genra.equalsIgnoreCase("居家")) {
                list.add(230);
            } else if (genra.equalsIgnoreCase("健康")) {
                list.add(230);
            } else if (genra.equalsIgnoreCase("家居")) {
                list.add(230);
            }
        } else if (p.getCategory().equalsIgnoreCase("汽车")) {
            list.add(226);
        } else if (p.getCategory().equalsIgnoreCase("科技")) {
            list.add(221);
        } else if (p.getCategory().equalsIgnoreCase("时尚")) {
            list.add(232);
        } else if (p.getCategory().equalsIgnoreCase("旅游")) {
            list.add(229);
        } else if (p.getCategory().equalsIgnoreCase("音乐")) {
            if (genra.equalsIgnoreCase("音乐MV")) {
                list.add(301);
            } else if (genra.equalsIgnoreCase("现场版")) {
                list.add(301);
            } else if (genra.equalsIgnoreCase("演唱会")) {
                list.add(301);
            } else if (genra.equalsIgnoreCase("电影原声")) {
                list.add(279);
            } else if (genra.equalsIgnoreCase("电视剧原声")) {
                list.add(279);
            } else if (genra.equalsIgnoreCase("动漫音乐")) {
                list.add(300);
            } else if (genra.equalsIgnoreCase("游戏音乐")) {
                list.add(300);
            } else if (genra.equalsIgnoreCase("广告音乐")) {
                list.add(280);
            }
 
        }
 
        if (list.size() == 0)// 其他无用的加入杂类
            list.add(299);
 
        return list;
    }
 
    // 节目列表
    public static Map<String, Object> programList(String cate, String genre, int year, int page) {
        try {
            Map<String, Object> map = YouKuApi.getProgramList(cate, genre, year, page);
            System.out.println(cate + "--" + genre + "--" + year + "--" + map.get("total"));
            // System.out.println(cate + "--" + genre + "--"
            // + ((List<Program>) map.get("data")).size());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    @SuppressWarnings("unchecked")
    public static List<ProgramVideo> programVideoList(String id) {
        List<ProgramVideo> list = new ArrayList<ProgramVideo>();
        int page = 1;
        Map<String, Object> map = YouKuApi.getProgramVideo(id, page);
        List<ProgramVideo> li = (List<ProgramVideo>) map.get("data");
        if (li != null && li.size() > 0)
            list.addAll(li);
        while (li != null && li.size() > 0) {
            page++;
            map = YouKuApi.getProgramVideo(id, page);
            li = (List<ProgramVideo>) map.get("data");
            list.addAll(li);
        }
        int freeCount = YouKuApi.getProgramNotPayCount(id);
        if (list.size() > freeCount) {
            list = list.subList(0, freeCount);
        }
        return list;
    }
 
    // 解析节目信息
    public void startParseShow() {
        final int[] years = new int[] { 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004,
                2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987,
                1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970 };
 
        List<YouKuCategory> list = YouKuApi.getProgramCategory();
        for (int y = 0; y < list.size(); y++) {
            final YouKuCategory yc = list.get(y);
            // if (!yc.getLabel().contains("综艺"))
            // continue;
            for (int c = 0; c < yc.getGenre().size(); c++) {
                final YouKuCategory child = yc.getGenre().get(c);
                // if (!child.getLabel().contains("搞笑"))
                // continue;
 
                for (int year = 0; year < 1; year++)
                    try {
                        int page = 1;
                        Map<String, Object> map = programList(yc.getLabel(), child.getLabel(), years[year], page);
                        List<Program> li = (List<Program>) map.get("data");
                        int total = Integer.parseInt(map.get("total") + "");
                        int count = 0;
                        int zeroCount = 0;
                        for (int p = 1; p < 61; p++) {
                            map = programList(yc.getLabel(), child.getLabel(), years[year], p);
                            li = (List<Program>) map.get("data");
                            count += (li != null ? li.size() : 0);
                            if (li == null || li.size() == 0)
                                zeroCount++;
                            else
                                zeroCount = 0;
                            if (zeroCount > 5)
                                break;
                            System.out.println("数量:" + count);
                            if (li != null)
                                for (int i = 0; i < li.size(); i++) {
                                    li.set(i, YouKuApi.getProgramDetail(li.get(i).getId()));
                                    if (StringUtil.isNullOrEmpty(li.get(i).getGenre()))
                                        li.get(i).setGenre(child.getLabel());
                                    li.get(i).setVideoList(programVideoList(li.get(i).getId()));
 
                                }
                            // 加入数据库
                            for (Program pm : li) {
                                // if (!pm.getName().equalsIgnoreCase("每日一囧
                                // 2016"))
                                // continue;
                                saveProgram(pm);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }
    }
 
    // 解析小视频信息
    public void startParseVideo() {
        List<YouKuCategory> list = YouKuApi.getVideoCategory();
        for (YouKuCategory ycy : list) {
            final YouKuCategory yc = ycy;
            if (!yc.getLabel().contains("游戏"))
                continue;
            // 排除7大版权分类
            if (yc.getLabel().contains("电影") || yc.getLabel().contains("电视剧") || yc.getLabel().contains("综艺")
                    || yc.getLabel().contains("动漫") || yc.getLabel().contains("纪实") || yc.getLabel().contains("教育"))
                continue;
            if (!yc.getLabel().contains("音乐")) {
                new Thread(new Runnable() {
 
                    @SuppressWarnings("unchecked")
                    public void run() {
 
                        if (!yc.getLabel().contains("资讯"))
                            for (YouKuCategory genre : yc.getGenre()) {
 
                                try {
                                    Map<String, Object> map = YouKuApi.getVideoListByCategory(yc.getLabel(),
                                            genre.getLabel(), 1);
                                    int total = Integer.parseInt(map.get("total") + "");
                                    if (total > 2000)
                                        total = 2000;
 
                                    if (total < 400)
                                        total = 400;
 
                                    int page = total % 50 == 0 ? total / 50 : total / 50 + 1;
                                    for (int n = 0; n < 3; n++) {// 做三次重复爬去,防止丢失部分视频
                                        for (int p = 1; p < page; p++) {
                                            map = YouKuApi.getVideoListByCategory(yc.getLabel(), genre.getLabel(), p);
                                            List<ProgramVideo> videoList = (List<ProgramVideo>) map.get("data");
                                            addProgramVideo(videoList, genre.getLabel());
                                        }
                                    }
 
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                            }
                    }
                }).start();
 
            } else {
                new Thread(new Runnable() {
                    public void run() {
                        yc.setGenre(new ArrayList<YouKuCategory>());
                        YouKuCategory child = new YouKuCategory();
                        child.setLabel("音乐MV");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("现场版");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("演唱会");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("电影原声");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("电视剧原声");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("动漫音乐");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("游戏音乐");
                        yc.getGenre().add(child);
                        child = new YouKuCategory();
                        child.setLabel("广告音乐");
                        yc.getGenre().add(child);
 
                        for (YouKuCategory genre : yc.getGenre()) {
                            for (int p = 1; p < 4; p++) {
                                List<ProgramVideo> videoList = YouKuApi.getMusicVideoList(genre.getLabel(), p);
                                addProgramVideo(videoList, genre.getLabel());
                            }
                        }
                    }
                }).start();
 
            }
        }
 
    }
 
    // 解析小视频信息
    public void startParseZiXun() {
        List<YouKuCategory> list = YouKuApi.getVideoCategory();
        for (YouKuCategory ycy : list) {
            if (!ycy.getLabel().contains("资讯"))
                continue;
 
            // 获取资讯
            for (int p = 1; p < 10; p++) {
                Map<String, Object> map = null;
                try {
                    map = YouKuApi.getZiXunVideoList("资讯", p);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                List<ProgramVideo> videoList = (List<ProgramVideo>) map.get("data");
                System.out.println(videoList.size());
                addProgramVideo(videoList, "资讯");
            }
        }
 
    }
 
    private void addProgramVideo(List<ProgramVideo> videoList, String genre) {
        for (ProgramVideo pv : videoList) {
            final String name = pv.getTitle();
            if (!(Boolean) categoryVideoDao.excute(new HibernateCallback<Boolean>() {
                public Boolean doInHibernate(Session session) throws HibernateException {
                    return JuheVideoUtil.isNeedAdd(session, name);
                }
            }))
                continue;
 
            System.out.println(genre);
            // 加入videoInfo
            try {
                ProgramVideo video = YouKuApi.getVideoDetail(pv.getId());
                if (video != null) {
                    pv.setCopyright_type(video.getCopyright_type());
                    pv.setPublic_type(video.getPublic_type());
                    pv.setOperationLimit(video.getOperationLimit());
                }
                System.out.println(pv.getCopyright_type() + "-" + pv.getPublic_type());
                if ("all".equalsIgnoreCase(pv.getPublic_type())) {// 不是版权并且是公开的
                    youKuService.saveVideo(pv);// 存入数据库
                    youKuService.addVideoToVideoInfo(pv, genre);
                }
            } catch (Exception e) {
 
            }
        }
    }
 
    public void saveProgram(Program pm) {
        final String name = pm.getName();
        if (!(Boolean) categoryVideoDao.excute(new HibernateCallback<Boolean>() {
            public Boolean doInHibernate(Session session) throws HibernateException {
                return JuheVideoUtil.isNeedAdd(session, name);
            }
        }))
            return;
 
        ProgramVideo pv = youKuService.getLatestProgramVideo(pm.getId());
        Comparator<ProgramVideo> cp = new Comparator<ProgramVideo>() {
            public int compare(ProgramVideo o1, ProgramVideo o2) {
                long t1 = Long.parseLong(o1.getStage());
                long t2 = Long.parseLong(o2.getStage());
                return (int) (t1 - t2);
            }
        };
 
        Collections.sort(pm.getVideoList(), cp);
        List<ProgramVideo> vlist = new ArrayList<ProgramVideo>();
 
        boolean isUpdate = false;
        if (pm.getVideoList().size() > 0 && pv != null)
            isUpdate = Long.parseLong(pm.getVideoList().get(pm.getVideoList().size() - 1).getStage()) > Long
                    .parseLong(pv.getStage());
        if (pv == null || isUpdate) {
            if (pv == null)// 首次加入
                youKuService.saveProgram(pm, pm.getVideoList());// 保存数据到本地
            else {
                for (int i = 0; i < pm.getVideoList().size(); i++) {
                    if (pm.getVideoList().get(i).getId().equalsIgnoreCase(pv.getId())) {
                        vlist = pm.getVideoList().subList(i, pm.getVideoList().size());
                        break;
                    }
                }
                youKuService.saveProgram(pm, vlist);
            }
        }
 
        if (pm.getVideoList() != null && pm.getVideoList().size() > 0)
            if (!"original".equalsIgnoreCase(pm.getVideoList().get(0).getCopyright_type()))
                youKuService.addProgramToVideoInfo(pm, isUpdate);
    }
 
    public void updateProgramById(String id) {
        try {
            Program pm = YouKuApi.getProgramDetail(id);
            pm.setVideoList(programVideoList(id));
            saveProgram(pm);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 
    }
 
    public static void parseSingleProgram(String id) {
        try {
            Program pm = YouKuApi.getProgramDetail(id);
            pm.setVideoList(programVideoList(pm.getId()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
 
    @SuppressWarnings("unchecked")
    public static boolean isSameVideo(List<Integer> typeList, Program p, VideoInfo v, Session session) {
        try {
            if (p.getName().equalsIgnoreCase(v.getName())) {
                // 判断是否含有优酷来源
                Object c = session
                        .createQuery("select count(*) from ResourceVideo rv where rv.video.id=? and rv.resource.id=15")
                        .setParameter(0, v.getId()).uniqueResult();
                if (Integer.parseInt(c + "") > 0) {
                    VideoInfo tempVideo = convertProgramToVideoInfo(p);
                    if (v.getPicture().equalsIgnoreCase(tempVideo.getPicture())
                            && (tempVideo.getYear() + "").equalsIgnoreCase(v.getYear()))// 封面图标相同且是同一年份则判断相同
                        return true;
                }
 
                if (p.getCategory().contains("电影") || p.getCategory().contains("电视剧") || p.getCategory().contains("综艺")
                        || p.getCategory().contains("动漫") || p.getCategory().contains("纪录片")) {
                    String time = p.getReleased();
                    Calendar ca = Calendar.getInstance();
                    ca.setTimeInMillis(TimeUtil.convertDateToTemp(time));
                    int year = ca.get(Calendar.YEAR);
                    String wheres = "";
                    for (Integer type : typeList)
                        wheres += String.format(" cv.videoType.id=%s or", type + "");
                    if (wheres.endsWith("or"))
                        wheres = wheres.substring(0, wheres.length() - 2);
                    List<CategoryVideo> cvList = session
                            .createQuery("from CategoryVideo cv where cv.video.id=? and (" + wheres + ")")
                            .setParameter(0, v.getId()).list();
                    boolean isSame = false;
                    if (cvList == null || cvList.size() == 0)
                        isSame = false;
                    else
                        isSame = true;
 
                    if (Math.abs(
                            year - Integer.parseInt(StringUtil.isNullOrEmpty(v.getYear()) ? "2016" : v.getYear())) < 3
                            && isSame)
                        return true;
                } else {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
 
    public static String parseShowIdByUrl(String url) {
        try {
            Document doc = Jsoup.connect(url).timeout(60 * 1000)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.59 QQ/7.8.16379.201 Safari/537.36")
                    .get();
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                if (els.get(i).toString().contains("var showid_en")) {
                    String id = els.get(i).toString().split("var showid_en=")[1].split(";")[0].replace("\"", "");
                    return id;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static List<String> getProgramIdList(String type, int page) {
        List<String> idList = new ArrayList<String>();
        String url = "";
        if (type.equalsIgnoreCase("电影")) {
            url = String.format("http://list.youku.com/category/show/c_96_pt_1_s_6_d_1_p_%s.html?spm=0.0.0.0.djf5YC",
                    page + "");
        } else if (type.equalsIgnoreCase("电视剧")) {
            url = String.format("http://list.youku.com/category/show/c_97_pt_1_s_6_d_1_p_%s.html?spm=0.0.0.0.djf5YC",
                    page + "");
        } else if (type.equalsIgnoreCase("综艺")) {
            url = String.format("http://list.youku.com/category/show/c_85_pt_1_s_6_d_1_p_%s.html?spm=0.0.0.0.djf5YC",
                    page + "");
        } else if (type.equalsIgnoreCase("动漫")) {
            url = String.format("http://list.youku.com/category/show/c_100_pt_1_s_6_d_1_p_%s.html?spm=0.0.0.0.djf5YC",
                    page + "");
        }
 
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(10 * 1000)
                    .userAgent(
                            "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25")
                    .get();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
 
        Elements lis = doc.getElementsByAttributeValue("class", "box-series").get(0).getElementsByTag("ul").get(0)
                .getElementsByTag("li");
        for (int i = 0; i < lis.size(); i++) {
            String u = lis.get(i).getElementsByTag("a").attr("href");
            if (!StringUtil.isNullOrEmpty(u)) {
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
 
                String id = parseShowIdByUrl(u);
                System.out.println(id);
                if (!StringUtil.isNullOrEmpty(id)) {
                    boolean isC = true;
                    for (int j = 0; j < idList.size(); j++) {
                        if (idList.get(j).equalsIgnoreCase(id))
                            isC = false;
 
                    }
                    if (isC)
                        idList.add(id);
                }
            }
        }
 
        return idList;
    }
 
}