reqularDetail.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'package:video/model/videoDetail.dart';
  2. import 'request.dart';
  3. class RegularDetail {
  4. static getHttpDetail(String url) async {
  5. var responseBody = await Request.getByHttpHtml(url);
  6. VideoDetail recommends = getRecommend(responseBody);
  7. return recommends;
  8. }
  9. static VideoDetail getRecommend(String responseBody) {
  10. RegExp regHeader =
  11. new RegExp(r'class="vodBox">[\s\S]*?</div>[\s\S]*?</div>');
  12. RegExp regInfo = new RegExp(r'class="vodinfobox">[\s\S]*?</div>');
  13. RegExp regPlayInfo = new RegExp(r'id="play_1">[\s\S]*?</div>');
  14. RegExp regDownloadInfo = new RegExp(r'id="down_1">[\s\S]*?</div>');
  15. Iterable<Match> matchesHeader = regHeader.allMatches(responseBody);
  16. Iterable<Match> matchesInfo = regInfo.allMatches(responseBody);
  17. Iterable<Match> matchesPlayInfo = regPlayInfo.allMatches(responseBody);
  18. Iterable<Match> matchesDownloadInfo =
  19. regDownloadInfo.allMatches(responseBody);
  20. VideoDetail listHeader = new VideoDetail();
  21. VideoDetail listInfo = new VideoDetail();
  22. VideoDetail listPlayInfo = new VideoDetail();
  23. VideoDetail listDownloadinfo = new VideoDetail();
  24. listPlayInfo.videoUrl = [];
  25. VideoDetail videoDetail;
  26. for (Match m in matchesHeader) {
  27. //groupCount返回正则表达式的分组数
  28. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  29. String match = m.group(0);
  30. //在此匹配
  31. listHeader = getHeaderItems(match);
  32. }
  33. for (Match m in matchesInfo) {
  34. String match = m.group(0);
  35. //在此匹配
  36. listInfo = getInfoItems(match);
  37. }
  38. for (Match m in matchesPlayInfo) {
  39. String match = m.group(0);
  40. //在此匹配
  41. listPlayInfo.videoUrl = getPlayInfoItems(match);
  42. }
  43. for (Match m in matchesDownloadInfo) {
  44. String match = m.group(0);
  45. listDownloadinfo.downloadUrl = getDownloadItems(match);
  46. }
  47. videoDetail = new VideoDetail(
  48. title: listHeader.title,
  49. imageUrl: listHeader.imageUrl,
  50. score: listHeader.score,
  51. starring: listInfo.starring,
  52. releaseTime: listInfo.releaseTime,
  53. description: listInfo.description,
  54. videoUrl: listPlayInfo.videoUrl,
  55. downloadUrl: listDownloadinfo.downloadUrl,
  56. );
  57. return videoDetail;
  58. }
  59. static VideoDetail getHeaderItems(String match) {
  60. RegExp reg = new RegExp(
  61. r'class="vodBox">[\s\S]*?src="(.*?)"[\s\S]*?h2>(.*?)</h2>[\s\S]*?<label>(.*?)</label>[\s\S]*?</div>');
  62. Iterable<Match> matches = reg.allMatches(match);
  63. String url = "";
  64. String title = "";
  65. String score = "";
  66. VideoDetail video;
  67. for (Match m in matches) {
  68. //groupCount返回正则表达式的分组数
  69. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  70. for (int i = 0; i < m.groupCount + 1; i++) {
  71. String match = m.group(i);
  72. switch (i) {
  73. case 1:
  74. url = match;
  75. break;
  76. case 2:
  77. title = match;
  78. break;
  79. case 3:
  80. score = match;
  81. break;
  82. }
  83. }
  84. }
  85. video = new VideoDetail(
  86. imageUrl: url,
  87. title: title,
  88. score: score,
  89. );
  90. return video;
  91. }
  92. static VideoDetail getInfoItems(String match) {
  93. RegExp reg = new RegExp(r'li[\s\S]*?>(.*?)<span>(.*?)</span>[\s\S]*?</li>');
  94. Iterable<Match> matches = reg.allMatches(match);
  95. String starring = "";
  96. String releaseTime = "";
  97. String str = "";
  98. String description = "";
  99. VideoDetail video;
  100. for (Match m in matches) {
  101. //groupCount返回正则表达式的分组数
  102. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  103. for (int i = 0; i < m.groupCount + 1; i++) {
  104. String match = m.group(i);
  105. switch (i) {
  106. case 1:
  107. str = match;
  108. break;
  109. case 2:
  110. str += match;
  111. break;
  112. }
  113. }
  114. if (str.contains("主演")) {
  115. starring = str;
  116. } else if (str.contains("上映")) {
  117. releaseTime = str;
  118. }
  119. }
  120. RegExp regDescription =
  121. new RegExp(r'class="more" txt="(.*?)"[\s\S]*?>[\s\S]*?</span>');
  122. Iterable<Match> matcheDescription = regDescription.allMatches(match);
  123. for (Match m in matcheDescription) {
  124. description = m.group(1);
  125. }
  126. video = new VideoDetail(
  127. starring: starring,
  128. releaseTime: releaseTime,
  129. description: description,
  130. );
  131. return video;
  132. }
  133. static List<String> getPlayInfoItems(String match) {
  134. RegExp reg = new RegExp(r'value="(.*?)"[\s\S]*?checked="" />');
  135. Iterable<Match> matches = reg.allMatches(match);
  136. List<String> videoUrl = [];
  137. for (Match m in matches) {
  138. //groupCount返回正则表达式的分组数
  139. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  140. if (m.group(1).contains("http")) {
  141. videoUrl.add(m.group(1));
  142. }
  143. }
  144. return videoUrl;
  145. }
  146. static List<String> getDownloadItems(String match) {
  147. RegExp reg = new RegExp(r'value="(.*?)"[\s\S]*?checked="" />');
  148. Iterable<Match> matches = reg.allMatches(match);
  149. List<String> videoUrl = [];
  150. for (Match m in matches) {
  151. //groupCount返回正则表达式的分组数
  152. //由于group(0)保存了匹配信息,因此字符串的总长度为:分组数+1
  153. if (m.group(1).contains("http")) {
  154. videoUrl.add(m.group(1));
  155. }
  156. }
  157. return videoUrl;
  158. }
  159. }