SohuUtil.java 45 KB
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 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
package com.topdraw.sohu.utils;

import com.alibaba.fastjson.JSONObject;
import org.afflatus.utility.DruidUtil;
import org.afflatus.utility.WebUtil;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SohuUtil {

	//private final String PROJECT_PATH = this.getClass().getClassLoader().getResource("../../") == null ?
	//		(System.getProperty("user.dir").endsWith("/") ? System.getProperty("user.dir")
	//				: System.getProperty("user.dir") + "/")
	//		: this.getClass().getClassLoader().getResource("../../").getPath();
	private final String PROJECT_PATH = "/prepared/";
	private final String M3U8_RELATIVE_PATH = "m3u8" + File.separatorChar;
	private final String XML_RELATIVE_PATH = "xml" + File.separatorChar;
	private final String TS_HLS_RELATIVE_PATH = "ts" + File.separatorChar;
	private final String API_KEY = "a1ab29b59ea9f581011f1eb6506c7ada";

	private static final String FILM_XML_URL = "http://ott.hd.sohu.com/hd/houyi/film.xml";
	private static final String DRAMA_XML_URL = "http://ott.hd.sohu.com/hd/houyi/drama.xml";
	private static final String VARIETY_XML_URL = "http://ott.hd.sohu.com/hd/houyi/variety.xml";
	private static final String DOCUMENTARY_XML_URL = "http://ott.hd.sohu.com/hd/houyi/documentary.xml";
	private static final String CARTOON_XML_URL = "http://ott.hd.sohu.com/hd/houyi/cartoon.xml";
	private static final String FEEFILM_XML_URL = "http://ott.hd.sohu.com/hd/houyi/feeFilm.xml";

	private static final String FILM_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/film_all_";
	private static final String DRAMA_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/drama_all_";
	private static final String VARIETY_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/variety_all_";
	private static final String DOCUMENTARY_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/documentary_all_";
	private static final String CARTOON_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/cartoon_all_";
	private static final String FEEFILM_ALL_XML_URL = "http://ott.hd.sohu.com/hd/all/fee_film_all_";

	private static final String M3U8_URL = "http://h5.ott.tv.sohu.com/api/partner/getM3u8VideoUrls.json";

	private Logger log = Logger.getLogger(this.getClass().getName());


	private void fullMetaDataInjection(String strUrl) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			File file = HttpConnectionUtil.downloadFile(strUrl, PROJECT_PATH, null);
			if (file != null) {
				SAXReader reader = new SAXReader();
				Document document = reader.read(file.getPath());
				Element root = document.getRootElement();

				int category_id = 0;
				int cate_code = 0;
				Date updated = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				for (Iterator<?> it = root.elementIterator(); it.hasNext();) {
					DruidUtil.beginTransaction(writeConnection);

					Element element = (Element) it.next();
					if ("category_id".equals(element.getName())) {
						category_id = Integer.parseInt(element.getText());
					}
					if ("cate_code".equals(element.getName())) {
						cate_code = Integer.parseInt(element.getText());
					}
					if ("updated".equals(element.getName())) {
						updated = sdf.parse(element.getText());
					}
					if ("album".equals(element.getName())) {
						Map<String, Object> mapAlbum = new HashMap<>();
						for (Iterator<?> itAlbum = element.elementIterator(); itAlbum.hasNext();) {
							Element elAlbum = (Element) itAlbum.next();
							if (elAlbum.getName().equals("publish_time")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? null : sdf.parse(
										elAlbum.getText()));
							} else if (elAlbum.getName().equals("episode_updated") ||
									elAlbum.getName().equals("episode_total") || elAlbum.getName().equals("year") ||
									elAlbum.getName().equals("fee") || elAlbum.getName().equals("is_clip") ||
									elAlbum.getName().equals("is_show") || elAlbum.getName().equals("is_early")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? 0 : Integer.parseInt(
										elAlbum.getText()
								));
							} else if (!elAlbum.getName().equals("videos")) {
								mapAlbum.put(elAlbum.getName().equals("update_time") ? "album_update_time" :
										elAlbum.getName(), elAlbum.getText().equals("") ? null : elAlbum.getText());
							} else {
								for (Iterator<?> itVideos = elAlbum.elementIterator(); itVideos.hasNext();) {
									Element elVideos = (Element) itVideos.next();
									if (elVideos.getName().equals("video")) {
										Map<String, Object> mapVideo = new HashMap<>();
										for (Iterator<?> itVideo = elVideos.elementIterator(); itVideo.hasNext();) {
											Element elVideo = (Element) itVideo.next();
											if (elVideo.getName().equals("issue_time")) {
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? null :
													sdf.parse(elVideo.getText()));
											} else if (elVideo.getName().equals("play_order") ||
													elVideo.getName().equals("time_length") ||
													elVideo.getName().equals("is_show") ||
													elVideo.getName().equals("fee") ||
													elVideo.getName().equals("tvPlayType")) {
												//log.info(elVideo.getName() + " " + mapAlbum.get("name"));
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? 0 :
														Integer.parseInt(elVideo.getText()));
											} else if (elVideo.getName().equals("start_time") ||
													elVideo.getName().equals("end_time")) {

											} else if (!elVideo.getName().equals("logoInfo")) {
												mapVideo.put(elVideo.getName().equals("update_time") ?
														"video_update_time" : elVideo.getName(),
														elVideo.getText().equals("") ? null : elVideo.getText());
											} else {
												for (Iterator<?> itLog = elVideo.elementIterator(); itLog.hasNext();) {
													Element elLog = (Element) itLog.next();
													if (elLog.getName().equals("dimension")) {
														mapVideo.put(elLog.getName(), elLog.getText());
													} else {
														mapVideo.put(elLog.getName(), elLog.getText().equals("") ?
															0 : Integer.parseInt(elLog.getText()));
													}
												}
											}
										}
										mapVideo.put("album_id", mapAlbum.get("album_id"));
										String sql = "SELECT id FROM sohu_video WHERE video_id = ?";
										Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection,
												sql, mapVideo.get("video_id"));
										if (mapExist == null) {
											DruidUtil.save(writeConnection, mapVideo, "sohu_video");
											log.info("---" + mapVideo.toString());
										} else {
											log.info("Video: " + mapVideo.get("video_id") + " already exists, just update");
											mapVideo.put("id", mapExist.get("id"));
											DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
										}

									}
								}
							}
						}
						mapAlbum.put("category_id", category_id);
						mapAlbum.put("cate_code", cate_code);
						mapAlbum.put("updated", updated);
						String sql = "SELECT id FROM sohu_album WHERE album_id = ?";
						Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection, sql,
								mapAlbum.get("album_id"));
						if (mapExist == null) {
							DruidUtil.save(writeConnection, mapAlbum, "sohu_album");
							log.info(mapAlbum.toString());
						} else {
							mapAlbum.put("id", mapExist.get("id"));
							DruidUtil.update(writeConnection, mapAlbum, "sohu_album", "id");
							log.info("Album: " + mapAlbum.get("album_id") + " already exists, just update");
						}

					}
					DruidUtil.commitTransaction(writeConnection);
				}

			} else {
				log.info("Download xml failed");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("failure in fullMetaDataInjection", ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}

	public void metaDataIncrementInjection() {
		log.info("------ start injecting film metadata ------");
		metaDataIncrementInjection(FILM_XML_URL);
		log.info("------ film metadata updated ------");

		log.info("------ start injecting drama metadata ------");
		metaDataIncrementInjection(DRAMA_XML_URL);
		log.info("------ drama metadata updated ------");

		log.info("------ start injecting variety metadata ------");
		metaDataIncrementInjection(VARIETY_XML_URL);
		log.info("------ variety metadata updated ------");

		log.info("------ start injecting documentary metadata ------");
		metaDataIncrementInjection(DOCUMENTARY_XML_URL);
		log.info("------ documentary metadata updated ------");

		log.info("------ start injecting cartoon metadata ------");
		metaDataIncrementInjection(CARTOON_XML_URL);
		log.info("------ cartoon metadata updated ------");

		log.info("------ start injecting feeFilm metadata ------");
		metaDataIncrementInjection(FEEFILM_XML_URL);
		log.info("------ feeFilm metadata updated ------");

		log.info("****************** 华丽的分割线 ******************");

		log.info("------ start self_produced documentary update ------");
		fullSelfProducedMetaDataUpdate("http://ott.hd.sohu.com/hd/houyi/sohu_documentary_update.xml");
		log.info("------ documentary metadata updated ------");

		log.info("------ start self_produced drama update ------");
		fullSelfProducedMetaDataUpdate("http://ott.hd.sohu.com/hd/houyi/sohu_drama_update.xml");
		log.info("------ drama metadata updated ------");

		log.info("------ start self_produced film update ------");
		fullSelfProducedMetaDataUpdate("http://ott.hd.sohu.com/hd/houyi/sohu_film_update.xml");
		log.info("------ film metadata updated ------");

		log.info("------ start self_produced variety update ------");
		fullSelfProducedMetaDataUpdate("http://ott.hd.sohu.com/hd/houyi/sohu_variety_update.xml");
		log.info("------ variety metadata updated ------");

	}

	// 搜狐增量接口(自制内容标识)
	private void fullSelfProducedMetaDataUpdate(String strUrl) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			Date dNow = new Date();
			SimpleDateFormat sdf_ymdh = new SimpleDateFormat("yyyyMMddHH");
			SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyyMMdd");

			String strDate = sdf_ymdh.format(dNow);
			String strDatePath = sdf_ymd.format(dNow) + File.separatorChar;

			String strCategory = strUrl.substring(strUrl.lastIndexOf("/") + 1, strUrl.lastIndexOf("."));

			File file = HttpConnectionUtil.downloadFile(strUrl, PROJECT_PATH + XML_RELATIVE_PATH + strDatePath +
					strCategory + File.separatorChar, strCategory + "_" + strDate + ".xml");
			if (file != null) {
				SAXReader reader = new SAXReader();
				Document document = reader.read(file.getPath());
				Element root = document.getRootElement();

				long lAlbumId = 0;
				for (Iterator<?> it = root.elementIterator(); it.hasNext();) {
					DruidUtil.beginTransaction(writeConnection);
					Element element = (Element) it.next();
					if ("album".equals(element.getName())) {
						int i = 0;
						for (Iterator<?> itAlbum = element.elementIterator(); itAlbum.hasNext();) {
							Element elAlbum = (Element) itAlbum.next();
							if (elAlbum.getName().equals("album_id")) {
								lAlbumId = Long.parseLong(elAlbum.getText());
								break;
							}
						}
						String sql = "SELECT id, self_produced FROM sohu_album WHERE album_id = ?";
						Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection, sql,
								lAlbumId);
						if (mapExist == null) {
							log.info("no such album: " + lAlbumId);
						} else {
							if ((Integer) mapExist.get("self_produced") == 1) {
								log.info("unnecessary to update: " + lAlbumId);
							} else {
								i++;
								mapExist.put("self_produced", 1);
								DruidUtil.update(writeConnection, mapExist, "sohu_album", "id");
								log.info("Album: " + lAlbumId + " self_produced updated");
							}
						}
						if (i == 0) {
							log.info("no records updated");
						} else {
							log.info(i + " records updated");
						}
					}

					DruidUtil.commitTransaction(writeConnection);
				}

			} else {
				log.info("Download xml failed");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("failure in fullSelfProducedMetaDataUpdate: " + strUrl, ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}

	private void metaDataIncrementInjection(String strURL) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			Date dNow = new Date();
			strURL = strURL + "?api_key=" + API_KEY + "&t=" + dNow.getTime();
			SimpleDateFormat sdf_ymdh = new SimpleDateFormat("yyyyMMddHH");
			SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyyMMdd");

			String strDate = sdf_ymdh.format(dNow);
			String strDatePath = sdf_ymd.format(dNow) + File.separatorChar;

			String strCategory = strURL.substring(strURL.lastIndexOf("/") + 1, strURL.lastIndexOf("."));

			File file = HttpConnectionUtil.downloadFile(strURL, PROJECT_PATH + XML_RELATIVE_PATH + strDatePath +
					strCategory + File.separatorChar, strCategory + "_" + strDate + ".xml");

			if (file != null) {
				SAXReader reader = new SAXReader();
				Document document = reader.read(file.getPath());
				Element root = document.getRootElement();

				int category_id = 0;
				int cate_code = 0;
				Date updated = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				DruidUtil.beginTransaction(writeConnection);
				for (Iterator<?> it = root.elementIterator(); it.hasNext();) {
					Element element = (Element) it.next();
					if ("category_id".equals(element.getName())) {
						category_id = Integer.parseInt(element.getText());
					}
					if ("cate_code".equals(element.getName())) {
						cate_code = Integer.parseInt(element.getText());
					}
					if ("updated".equals(element.getName())) {
						updated = sdf.parse(element.getText());
					}
					if ("album".equals(element.getName())) {
						Map<String, Object> mapAlbum = new HashMap<>();
						for (Iterator<?> itAlbum = element.elementIterator(); itAlbum.hasNext();) {
							Element elAlbum = (Element) itAlbum.next();
							if (elAlbum.getName().equals("publish_time")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? null : sdf.parse(
										elAlbum.getText()));
							} else if (elAlbum.getName().equals("episode_updated") ||
									elAlbum.getName().equals("episode_total") || elAlbum.getName().equals("year") ||
									elAlbum.getName().equals("fee") || elAlbum.getName().equals("is_clip") ||
									elAlbum.getName().equals("is_show") || elAlbum.getName().equals("is_early")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? 0 : Integer.parseInt(
										elAlbum.getText()
								));
							} else if (!elAlbum.getName().equals("videos")) {
								mapAlbum.put(elAlbum.getName().equals("update_time") ? "album_update_time" :
										elAlbum.getName(), elAlbum.getText().equals("") ? null : elAlbum.getText());
							} else {
								for (Iterator<?> itVideos = elAlbum.elementIterator(); itVideos.hasNext();) {
									Element elVideos = (Element) itVideos.next();
									if (elVideos.getName().equals("video")) {
										Map<String, Object> mapVideo = new HashMap<>();
										for (Iterator<?> itVideo = elVideos.elementIterator(); itVideo.hasNext();) {
											Element elVideo = (Element) itVideo.next();
											if (elVideo.getName().equals("issue_time")) {
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? null :
														sdf.parse(elVideo.getText()));
											} else if (elVideo.getName().equals("play_order") ||
													elVideo.getName().equals("time_length") ||
													elVideo.getName().equals("is_show") ||
													elVideo.getName().equals("fee") ||
													elVideo.getName().equals("tvPlayType")) {
												//log.info(elVideo.getName() + " " + mapAlbum.get("name"));
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? 0 :
														Integer.parseInt(elVideo.getText()));
											} else if (elVideo.getName().equals("start_time") ||
													elVideo.getName().equals("end_time")) {

											} else if (!elVideo.getName().equals("logoInfo")) {
												mapVideo.put(elVideo.getName().equals("update_time") ?
																"video_update_time" : elVideo.getName(),
														elVideo.getText().equals("") ? null : elVideo.getText());
											} else {
												for (Iterator<?> itLog = elVideo.elementIterator(); itLog.hasNext();) {
													Element elLog = (Element) itLog.next();
													if (elLog.getName().equals("dimension")) {
														mapVideo.put(elLog.getName(), elLog.getText());
													} else {
														mapVideo.put(elLog.getName(), elLog.getText().equals("") ?
																0 : Integer.parseInt(elLog.getText()));
													}
												}
											}
										}
										mapVideo.put("album_id", mapAlbum.get("album_id"));
										String sql = "SELECT id, is_show, m3u8_path FROM sohu_video WHERE video_id = ?";
										Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection,
												sql, mapVideo.get("video_id"));
										if (mapExist == null) {
											mapVideo.put("xml_path", file.getPath());
											try {
												DruidUtil.save(writeConnection, mapVideo, "sohu_video");
												log.info("video saved: " + mapVideo.get("video_id") + " [" +
														mapVideo.get("video_name") + "]");
 											} catch (SQLException sqle) {
												log.error("fail to save video", sqle);
											}
										} else {
											mapVideo.put("id", mapExist.get("id"));

											if (mapExist.get("m3u8_path") != null &&
													mapExist.get("m3u8_path").toString().equals("500")) {
												// 重新触发下载m3u8_path
												mapVideo.put("m3u8_path", null);
											}

											mapVideo.put("xml_path", file.getPath());
											DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
											log.info("video: " + mapVideo.get("video_id") +
													" already exists, just update");
											if (mapVideo.get("is_show") != mapExist.get("is_show")) {
												Map<String, Object> mapOnlineInfo = new HashMap<>();
												mapOnlineInfo.put("video_id", mapVideo.get("video_id"));
												mapOnlineInfo.put("is_online", mapVideo.get("is_show"));

												DruidUtil.save(writeConnection, mapOnlineInfo, "sohu_online_info");
											}
										}
									}
								}
							}
						}
						mapAlbum.put("category_id", category_id);
						mapAlbum.put("cate_code", cate_code);
						mapAlbum.put("updated", updated);
						String sql = "SELECT id, episode_updated FROM sohu_album WHERE album_id = ?";
						Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection, sql,
								mapAlbum.get("album_id"));
						if (mapExist == null) {
							mapAlbum.put("xml_path", file.getPath());
							DruidUtil.save(writeConnection, mapAlbum, "sohu_album");
							log.info("album saved: " + mapAlbum.get("album_id") + " [" +
									mapAlbum.get("album_name") + "]");
						} else {
							mapAlbum.put("id", mapExist.get("id"));
							if ((Integer) mapAlbum.get("episode_updated") < (Integer) mapExist.get("episode_updated")) {
								mapAlbum.put("episode_updated", mapExist.get("episode_updated"));
							}

							mapAlbum.put("xml_path", file.getPath());
							DruidUtil.update(writeConnection, mapAlbum, "sohu_album", "id");
							log.info("Album: " + mapAlbum.get("album_id") + " already exists, just update");

							if (mapAlbum.get("is_show") != mapExist.get("is_show")) {
								Map<String, Object> mapOnlineInfo = new HashMap<>();
								mapOnlineInfo.put("album_id", mapAlbum.get("album_id"));
								mapOnlineInfo.put("is_online", mapAlbum.get("is_show"));
								DruidUtil.save(writeConnection, mapOnlineInfo, "sohu_online_info");
							}
						}
					}
				}
				DruidUtil.commitTransaction(writeConnection);
			} else {
				log.info("Download xml failed");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("failure in metaDataIncrementInjection", ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}

	private String getCategoryByVideo(Map<String, Object> map) {
		String str = "";
		Connection readConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			String sql = "SELECT cate_code FROM sohu_album WHERE album_id = ?";
			Map<String, Object> mapAlbum = DruidUtil.queryUniqueResult(readConnection, sql, map.get("album_id"));
			int iCateCode = (Integer) mapAlbum.get("cate_code");

			switch (iCateCode) {
				case 100:
					if ((Integer) map.get("fee") == 0) {
						str = "film";
					} else {
						str = "feeFilm";
					}
					break;
				case 101:
					str = "drama";
					break;
				case 106:
					str = "variety";
					break;
				case 107:
					str = "documentary";
					break;
				case 115:
					str = "cartoon";
					break;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DruidUtil.close(readConnection);
		}
		return str;
	}

	public boolean downloadM3U8ByVideoId(long lVideoId) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			String sql = "SELECT id, album_id, video_id, definition, tvPlayType, fee FROM sohu_video WHERE video_id = ?";
			Map<String, Object> mapVideo = DruidUtil.queryUniqueResult(readConnection, sql, lVideoId);

			String strDefinition = mapVideo.get("definition").toString();
			String[] arr = strDefinition.split(",");

			String strCategory = getCategoryByVideo(mapVideo) + File.separatorChar;

			int iDefinition = 2;
			if (!strDefinition.contains(",")) {
				iDefinition = Integer.parseInt(strDefinition);
			} else {
				for (int i = 0; i < arr.length; i++) {
					if (i == arr.length - 1) {
						iDefinition = Integer.parseInt(arr[arr.length - 1]);
						break;
					}
					if (Integer.parseInt(arr[i]) > Integer.parseInt(arr[i + 1])) {
						if (Integer.parseInt(arr[i]) != 2) {
							arr[i + 1] = arr[i];
						}
					}
				}
			}

			if (mapVideo.get("m3u8_path") == null) {
				String strParam = "albumId=" + mapVideo.get("album_id") + "&versionId=" + iDefinition +
						"&tvVerId=" + lVideoId;

				String strResponse = WebUtil.sendGet(M3U8_URL, strParam, "UTF-8");
				JSONObject jsonObject = JSONObject.parseObject(strResponse);
				if (jsonObject.getIntValue("status") != 200) {
					log.info("fail to get m3u8 info from sohu by video_id: " + lVideoId);
					mapVideo.put("m3u8_path", jsonObject.getIntValue("status"));
					DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
					return false;
				} else {
					if (jsonObject.get("data") == null) {
						log.info("fail to get m3u8 [200 null] info from sohu by video_id: " + lVideoId);
						mapVideo.put("m3u8_path", "200 null");
						DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
						return false;
					}

					String str = jsonObject.getString("data");
					ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());

					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					String strDate = sdf.format(new Date()) + File.separatorChar;

					log.info("start downloading m3u8...");

					String strPath = PROJECT_PATH + M3U8_RELATIVE_PATH + strDate + strCategory +
							lVideoId + "_" + iDefinition + ".m3u8";
					File file = new File(strPath);
					if (!file.getParentFile().exists()) {
						file.getParentFile().mkdirs();
					}
					OutputStream os = new FileOutputStream(file);
					int iBytesRead;
					byte[] buffer = new byte[8192];
					while ((iBytesRead = is.read(buffer, 0, 8192)) != -1) {
						os.write(buffer, 0, iBytesRead);
					}
					os.close();
					is.close();

					log.info("downloaded successfully: " + file.getPath());

					mapVideo.put("m3u8_path", strPath);
					DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
				}
			} else {
				log.info("m3u8 already downloaded");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("fail to downloadM3U8ByVideoId: " + lVideoId, ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
		return true;
	}

	/*
	private M3U8 getM3U8ByURL(String strM3U8URL) {
		try {
			File file = new File(strM3U8URL);

			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			String strBasePath = strM3U8URL.substring(0, strM3U8URL.lastIndexOf(File.separatorChar) + 1);
			M3U8 m3u8Ret = new M3U8();
			m3u8Ret.setStrBasePath(strBasePath);

			String strLine;
			float fSeconds = 0;
			int iIndex;
			while ((strLine = reader.readLine()) != null) {
				if (strLine.startsWith("#")) {
					if (strLine.startsWith("#EXTINF:")) {
						strLine = strLine.substring(8);
						if ((iIndex = strLine.indexOf(",")) != -1) {
							strLine = strLine.substring(0, iIndex);
						}
						try {
							fSeconds = Float.parseFloat(strLine);
						} catch (Exception e) {
							fSeconds = 0;
						}
					}
					continue;
				}
				if (strLine.endsWith("m3u8")) {
					return getM3U8ByURL(strBasePath + strLine);
				}
				m3u8Ret.addTs(new M3U8.Ts(strLine, fSeconds));
				fSeconds = 0;
			}
			reader.close();

			return m3u8Ret;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void downloadTSHLSByVideoId(long lVideoId) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			String sql = "SELECT id, m3u8_path FROM sohu_video WHERE video_id = ?";
			Map<String, Object> mapVideo = DruidUtil.queryUniqueResult(readConnection, sql, lVideoId);
			if (mapVideo == null || mapVideo.get("m3u8_path") == null || mapVideo.get("m3u8_path").toString().equals("")) {
				log.info("invalid video_id: " + lVideoId);
				return;
			}
			SohuUtil util = new SohuUtil();
			M3U8 m3u8 = util.getM3U8ByURL(mapVideo.get("m3u8_path").toString());
			String strBasePath = m3u8.getStrBasePath();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String strDate = sdf.format(new Date()) + File.separatorChar;
			m3u8.getListTs().parallelStream().forEachOrdered(ts -> {
				//log.info(m3u8.getListTs().indexOf(ts) + ".ts");

				File file = new File(PROJECT_PATH + TS_HLS_RELATIVE_PATH + strDate + lVideoId + File.separatorChar +
						m3u8.getListTs().indexOf(ts) + ".ts");
				if (!file.exists()) {// 下载过的就不管了
					File fileTs = HttpConnectionUtil.downloadFile(ts.getStrFilePath(),
							PROJECT_PATH + TS_HLS_RELATIVE_PATH + strDate + lVideoId + File.separatorChar,
							m3u8.getListTs().indexOf(ts) + ".ts");
					if (fileTs == null) {
						log.info("fail to Download ts: " + ts.getStrFilePath());
					} else {
						log.info("ts hls downloaded: " + m3u8.getListTs().indexOf(ts) + ".ts");
					}
				}
			});
			log.info("Video Downloaded: " + lVideoId);

			//	mapVideo.put("m3u8_path", strPath);
			//	DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("fail to downloadTSHLSByVideoId", ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}

	public void downloadM3U8AndTSHLS(long lVideoId) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			String sql = "SELECT id, album_id, video_id, definition, m3u8_path FROM sohu_video WHERE video_id = ?";
			Map<String, Object> mapVideo = DruidUtil.queryUniqueResult(readConnection, sql, lVideoId);
			if (mapVideo == null) {
				log.info("invalid video_id: " + lVideoId);
				return;
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String strDate = sdf.format(new Date()) + File.separatorChar;

			String strDefinition = mapVideo.get("definition").toString();
			String[] arr = strDefinition.split(",");
			int iDefinition = 2;
			if (!strDefinition.contains(",")) {
				iDefinition = Integer.parseInt(strDefinition);
			} else {
				for (int i = 0; i < arr.length; i++) {
					if (i == arr.length - 1) {
						iDefinition = Integer.parseInt(arr[arr.length - 1]);
						break;
					}
					if (Integer.parseInt(arr[i]) > Integer.parseInt(arr[i + 1])) {
						if (Integer.parseInt(arr[i]) != 2) {
							arr[i + 1] = arr[i];
						}
					}
				}
			}

			if (mapVideo.get("m3u8_path") == null) {

				String strParam = "albumId=" + mapVideo.get("album_id") + "&versionId=" + iDefinition +
						"&tvVerId=" + lVideoId;

				String strResponse = WebUtil.sendGet(M3U8_URL, strParam, "UTF-8");
				JSONObject jsonObject = JSONObject.parseObject(strResponse);
				if (jsonObject.getIntValue("status") != 200) {
					log.info("fail to get m3u8 info from sohu by video_id: " + lVideoId);
					return;
				} else {
					String str = jsonObject.getString("data");
					ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());

					log.info("start downloading m3u8...");
					String strPath = PROJECT_PATH + M3U8_RELATIVE_PATH + strDate + lVideoId + "_" + iDefinition + ".m3u8";
					File file = new File(strPath);
					if (!file.getParentFile().exists()) {
						file.getParentFile().mkdirs();
					}
					OutputStream os = new FileOutputStream(file);
					int iBytesRead;
					byte[] buffer = new byte[8192];
					while ((iBytesRead = is.read(buffer, 0, 8192)) != -1) {
						os.write(buffer, 0, iBytesRead);
					}
					os.close();
					is.close();

					log.info("downloaded successfully: " + file.getPath());

					mapVideo.put("m3u8_path", strPath);
					DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
				}
			} else {
				log.info("m3u8 already downloaded, ready to download ts hls...");
			}


			SohuUtil util = new SohuUtil();
			M3U8 m3u8 = util.getM3U8ByURL(mapVideo.get("m3u8_path").toString());
			String strBasePath = m3u8.getStrBasePath();
			String strTsBaseUrl = PROJECT_PATH + TS_HLS_RELATIVE_PATH + strDate + lVideoId + "_" + iDefinition +
					File.separatorChar;
			m3u8.getListTs().parallelStream().forEachOrdered(ts -> {
				//log.info(m3u8.getListTs().indexOf(ts) + ".ts");

				File fileExists = new File(strTsBaseUrl + "ts_" + fillZero(m3u8.getListTs().indexOf(ts), 4) + ".ts");
				if (!fileExists.exists()) {// 下载过的就不管了
					File fileTs = null;
					while (fileTs == null) {
						fileTs = HttpConnectionUtil.downloadFile(ts.getStrFilePath(),
								strTsBaseUrl, "ts_" + fillZero(m3u8.getListTs().indexOf(ts), 4) + ".ts");
						if (fileTs == null) {
							log.info("fail to Download ts: " + ts.getStrFilePath() + ", ready to retry");
						} else {
							log.info("ts hls downloaded: " + fileTs.getPath());
						}
					}
				}
			});
			log.info("Video Downloaded: " + lVideoId);
			mapVideo.put("ts_path", strTsBaseUrl);
			DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");

			//log.info("start merging...");
			//mergeFiles(new File(mapVideo.get("ts_path").toString()), lVideoId + ".ts");

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("fail to downloadM3U8TS", ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}

	public boolean mergeFiles(File file, String strResultPath) {
		File[] fPaths = file.listFiles();
		if (fPaths == null || fPaths.length < 1) {
			return false;
		}

		if (fPaths.length == 1) {
			return fPaths[0].renameTo(new File(strResultPath));
		}
		for (int i = 0; i < fPaths.length; i++) {
			if (!fPaths[i].exists() || !fPaths[i].isFile()) {
				return false;
			}
		}
		List<File> listFile = Arrays.asList(fPaths);
		//Collections.sort(listFile, new Comparator<File>() {
		//	@Override
		//	public int compare(File o1, File o2) {
		//		if (o1.isDirectory() && o2.isFile())
		//			return -1;
		//		if (o1.isFile() && o2.isDirectory())
		//			return 1;
		//		return Integer.parseInt(o1.getName().split("\\.")[0]) - Integer.parseInt(o2.getName().split("\\.")[0]);
		//	}
		//});

		File fileResult = new File(file.getPath() + File.separatorChar + "full_" + strResultPath);
		try {
			//FileOutputStream fs = new FileOutputStream(fileResult, true);
			//FileChannel fileResultChannel = fs.getChannel();
			//FileInputStream fis;
			//for (int i = 0; i < listFile.size(); i++) {
			//	fis = new FileInputStream(listFile.get(i));
			//	FileChannel blk = fis.getChannel();
			//	fileResultChannel.transferFrom(blk, fileResultChannel.size(), blk.size());
			//	fis.close();
			//	blk.close();
			//	if (i % 20 == 0) {
			//		log.info(i + " ts merged");
			//	}
			//}
			//fs.close();
			//fileResultChannel.close();

			fileResult.createNewFile();
			RandomAccessFile raf = new RandomAccessFile(fileResult, "rw");
			raf.setLength(0);
			raf.seek(0);
			byte[] bytes = new byte[1024];
			int len = -1;
			for(int i = 0; i < listFile.size(); i++) {
				RandomAccessFile out = new RandomAccessFile(listFile.get(i), "rw");
				while((len = out.read(bytes)) != -1) {
					raf.write(bytes, 0, len);
				}
				out.close();
				if (i != 1 && (i + 1) % 20 == 0) {
					log.info((i + 1) + " ts merged");
				}
			}
			raf.close();

			log.info("merged successfully: " + fileResult.getPath());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		// for (int i = 0; i < fpaths.length; i ++) {
		// fpaths[i].delete();
		// }

		return true;
	}

	private static String fillZero (Integer i, Integer iTotal) {
		String strRet = "";
		String str = i.toString();
		if (str.length() >= iTotal) {
			strRet = str;
		} else {
			for (int j = 0; j < iTotal - str.length(); j++) {
				strRet += "0";
			}
			strRet += str;
		}
		return strRet;
	}

	public void metaDataIncrementInjectionByXml(String strDatePath) {
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, FILM_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, FILM_XML_URL, strDatePath + "21");

		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, DRAMA_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, DRAMA_XML_URL, strDatePath + "21");

		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, VARIETY_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, VARIETY_XML_URL, strDatePath + "21");

		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, DOCUMENTARY_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, DOCUMENTARY_XML_URL, strDatePath + "21");

		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, CARTOON_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, CARTOON_XML_URL, strDatePath + "21");

		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, FEEFILM_XML_URL, strDatePath + "09");
		metaDataIncrementInjectionByXml(strDatePath + File.separatorChar, FEEFILM_XML_URL, strDatePath + "21");
	}

	private void metaDataIncrementInjectionByXml(String strDatePath, String strURL, String strDate) {
		Connection readConnection = null;
		Connection writeConnection = null;
		try {
			readConnection = DruidUtil.getRandomReadConnection();
			writeConnection = DruidUtil.getRandomWriteConnection();

			String strCategory = strURL.substring(strURL.lastIndexOf("/") + 1, strURL.lastIndexOf("."));

			File file = new File(PROJECT_PATH + XML_RELATIVE_PATH + strDatePath +
					strCategory + File.separatorChar, strCategory + "_" + strDate + ".xml");

			if (file != null) {
				SAXReader reader = new SAXReader();
				Document document = reader.read(file.getPath());
				Element root = document.getRootElement();

				int category_id = 0;
				int cate_code = 0;
				Date updated = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

				DruidUtil.beginTransaction(writeConnection);
				for (Iterator<?> it = root.elementIterator(); it.hasNext();) {
					Element element = (Element) it.next();
					if ("category_id".equals(element.getName())) {
						category_id = Integer.parseInt(element.getText());
					}
					if ("cate_code".equals(element.getName())) {
						cate_code = Integer.parseInt(element.getText());
					}
					if ("updated".equals(element.getName())) {
						updated = sdf.parse(element.getText());
					}
					if ("album".equals(element.getName())) {
						Map<String, Object> mapAlbum = new HashMap<>();
						for (Iterator<?> itAlbum = element.elementIterator(); itAlbum.hasNext();) {
							Element elAlbum = (Element) itAlbum.next();
							if (elAlbum.getName().equals("publish_time")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? null : sdf.parse(
										elAlbum.getText()));
							} else if (elAlbum.getName().equals("episode_updated") ||
									elAlbum.getName().equals("episode_total") || elAlbum.getName().equals("year") ||
									elAlbum.getName().equals("fee") || elAlbum.getName().equals("is_clip") ||
									elAlbum.getName().equals("is_show") || elAlbum.getName().equals("is_early")) {
								mapAlbum.put(elAlbum.getName(), elAlbum.getText().equals("") ? 0 : Integer.parseInt(
										elAlbum.getText()
								));
							} else if (!elAlbum.getName().equals("videos")) {
								mapAlbum.put(elAlbum.getName().equals("update_time") ? "album_update_time" :
										elAlbum.getName(), elAlbum.getText().equals("") ? null : elAlbum.getText());
							} else {
								for (Iterator<?> itVideos = elAlbum.elementIterator(); itVideos.hasNext();) {
									Element elVideos = (Element) itVideos.next();
									if (elVideos.getName().equals("video")) {
										Map<String, Object> mapVideo = new HashMap<>();
										for (Iterator<?> itVideo = elVideos.elementIterator(); itVideo.hasNext();) {
											Element elVideo = (Element) itVideo.next();
											if (elVideo.getName().equals("issue_time")) {
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? null :
														sdf.parse(elVideo.getText()));
											} else if (elVideo.getName().equals("play_order") ||
													elVideo.getName().equals("time_length") ||
													elVideo.getName().equals("is_show") ||
													elVideo.getName().equals("fee") ||
													elVideo.getName().equals("tvPlayType")) {
												//log.info(elVideo.getName() + " " + mapAlbum.get("name"));
												mapVideo.put(elVideo.getName(), elVideo.getText().equals("") ? 0 :
														Integer.parseInt(elVideo.getText()));
											} else if (elVideo.getName().equals("start_time") ||
													elVideo.getName().equals("end_time")) {

											} else if (!elVideo.getName().equals("logoInfo")) {
												mapVideo.put(elVideo.getName().equals("update_time") ?
																"video_update_time" : elVideo.getName(),
														elVideo.getText().equals("") ? null : elVideo.getText());
											} else {
												for (Iterator<?> itLog = elVideo.elementIterator(); itLog.hasNext();) {
													Element elLog = (Element) itLog.next();
													if (elLog.getName().equals("dimension")) {
														mapVideo.put(elLog.getName(), elLog.getText());
													} else {
														mapVideo.put(elLog.getName(), elLog.getText().equals("") ?
																0 : Integer.parseInt(elLog.getText()));
													}
												}
											}
										}
										mapVideo.put("album_id", mapAlbum.get("album_id"));
										String sql = "SELECT id, is_show, m3u8_path FROM sohu_video WHERE video_id = ?";
										Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection,
												sql, mapVideo.get("video_id"));
										if (mapExist == null) {
											mapVideo.put("xml_path", file.getPath());
											DruidUtil.save(writeConnection, mapVideo, "sohu_video");
											log.info("video saved: " + mapVideo.get("video_id") + " [" +
													mapVideo.get("video_name") + "]");
										} else {
											mapVideo.put("id", mapExist.get("id"));

											if (mapExist.get("m3u8_path") != null &&
													mapExist.get("m3u8_path").toString().equals("500")) {
												// 重新触发下载m3u8_path
												mapVideo.put("m3u8_path", null);
											}

											mapVideo.put("xml_path", file.getPath());
											DruidUtil.update(writeConnection, mapVideo, "sohu_video", "id");
											log.info("video: " + mapVideo.get("video_id") +
													" already exists, just update");
											if (mapVideo.get("is_show") != mapExist.get("is_show")) {
												Map<String, Object> mapOnlineInfo = new HashMap<>();
												mapOnlineInfo.put("video_id", mapVideo.get("video_id"));
												mapOnlineInfo.put("is_online", mapVideo.get("is_show"));

												DruidUtil.save(writeConnection, mapOnlineInfo, "sohu_online_info");
											}
										}
									}
								}
							}
						}
						mapAlbum.put("category_id", category_id);
						mapAlbum.put("cate_code", cate_code);
						mapAlbum.put("updated", updated);
						String sql = "SELECT id, episode_updated FROM sohu_album WHERE album_id = ?";
						Map<String, Object> mapExist = DruidUtil.queryUniqueResult(readConnection, sql,
								mapAlbum.get("album_id"));
						if (mapExist == null) {
							mapAlbum.put("xml_path", file.getPath());
							DruidUtil.save(writeConnection, mapAlbum, "sohu_album");
							log.info("album saved: " + mapAlbum.get("album_id") + " [" +
									mapAlbum.get("album_name") + "]");
						} else {
							mapAlbum.put("id", mapExist.get("id"));
							if ((Integer) mapAlbum.get("episode_updated") < (Integer) mapExist.get("episode_updated")) {
								mapAlbum.put("episode_updated", mapExist.get("episode_updated"));
							}

							mapAlbum.put("xml_path", file.getPath());
							DruidUtil.update(writeConnection, mapAlbum, "sohu_album", "id");
							log.info("Album: " + mapAlbum.get("album_id") + " already exists, just update");

							if (mapAlbum.get("is_show") != mapExist.get("is_show")) {
								Map<String, Object> mapOnlineInfo = new HashMap<>();
								mapOnlineInfo.put("album_id", mapAlbum.get("album_id"));
								mapOnlineInfo.put("is_online", mapAlbum.get("is_show"));
								DruidUtil.save(writeConnection, mapOnlineInfo, "sohu_online_info");
							}
						}
					}
				}
				DruidUtil.commitTransaction(writeConnection);
			} else {
				log.info("Download xml failed");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("failure in metaDataIncrementInjection", ex);
		} finally {
			DruidUtil.close(readConnection);
			DruidUtil.close(writeConnection);
		}
	}
	*/

	private static void fullMetaDataInjection(String strAllPath, int iTotal) {
		switch (strAllPath) {
			case "film":
				strAllPath = FILM_ALL_XML_URL;
				break;
			case "drama":
				strAllPath = DRAMA_ALL_XML_URL;
				break;
			case "variety":
				strAllPath = VARIETY_ALL_XML_URL;
				break;
			case "documentary":
				strAllPath = DOCUMENTARY_ALL_XML_URL;
				break;
			case "cartoon":
				strAllPath = CARTOON_ALL_XML_URL;
				break;
			case "feeFilm":
				strAllPath = FEEFILM_ALL_XML_URL;
				break;
			default:
				System.out.println("uncorrect sohu category");
				return;
		}

		SohuUtil util = new SohuUtil();
		for (int i = 1; i <= iTotal; i++) {
			String strUrl = strAllPath + i + ".xml";
			System.out.println("start injection: " + strUrl);
			util.fullMetaDataInjection(strUrl);
		}
	}

	public static void main(String[] args) {
		//SohuUtil util = new SohuUtil();
		//util.fullSelfProducedMetaDataUpdate("http://ott.hd.sohu.com/hd/houyi/sohu_film.xml");

		if (args.length != 2) {
			System.out.println("wrong number of arguments");
		} else {
			String strAllPath = args[0];
			String strTotal = args[1];
			fullMetaDataInjection(strAllPath, Integer.parseInt(strTotal));
		}

		//SohuUtil util = new SohuUtil();
		//for (int i = 1; i <= 2; i++) {
		//	String strUrl = "http://ott.hd.sohu.com/hd/all/fee_film_all_" + i + ".xml";
		//	System.out.println("start injection: " + strUrl);
		//	util.fullMetaDataInjection(strUrl);
		//}
		//util.downloadM3U8TS(4372);
		//util.downloadTSHLSByVideoId(4372);
		//util.downloadM3U8AndTSHLS(3000504);
		//util.mergeFiles(new File("E:\\MyWorkSpace\\Eclipse\\hyperion\\hyperion.admin/ts\\20180607\\3000504_21\\"),
		//		"3000504.ts");
		//util.metaDataIncrementInjection(DRAMA_XML_URL);

		//Connection readConnection = null;
		//try {
		//	readConnection = DruidUtil.getRandomReadConnection();
		//	String sql = "SELECT * FROM sohu_video WHERE album_id = ?";
		//	List<Map<String, Object>> list = DruidUtil.queryList(readConnection, sql, 225);
		//	File file = new File("225.txt");
		//	if (!file.exists()) {
		//		file.createNewFile();
		//	}
		//	FileWriter fw = new FileWriter(file.getAbsoluteFile());
		//	BufferedWriter bw = new BufferedWriter(fw);
		//	for (Map<String, Object> map : list) {
		//		bw.write(map.get("m3u8_path") + "\r\n");
		//	}
		//	bw.close();
		//
		//} catch (Exception ex) {
		//	ex.printStackTrace();
		//} finally {
		//	DruidUtil.close(readConnection);
		//}

		//util.downloadM3U8ByVideoId(683180);
		//util.metaDataIncrementInjection(VARIETY_XML_URL);
	}

}