TestWWW.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System.Collections;
  2. using System.Net;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System;
  7. using System.Collections.Generic;
  8. using UnityEngine.Networking;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text.RegularExpressions;
  12. using DG.Tweening.Plugins.Core.PathCore;
  13. public class TestWWW : MonoBehaviour
  14. {
  15. [SerializeField] string SetID;
  16. [SerializeField] int minCardID;
  17. [SerializeField] int maxCardID;
  18. [SerializeField] int minParallelID;
  19. [SerializeField] int maxParallelID;
  20. List<int> cardIDs
  21. {
  22. get
  23. {
  24. List<int> cardIDs = new List<int>();
  25. for (int i = minCardID; i < maxCardID + 1; i++)
  26. {
  27. cardIDs.Add(i);
  28. }
  29. return cardIDs;
  30. }
  31. }
  32. public void OnClickGetJPNCardListText()
  33. {
  34. StopGetCardImages();
  35. StartCoroutine(GetCardListTexts(false));
  36. }
  37. public void OnClickGetENGCardListText()
  38. {
  39. StopGetCardImages();
  40. StartCoroutine(GetCardListTexts(true));
  41. }
  42. IEnumerator GetCardListTexts(bool isEnglish)
  43. {
  44. Debug.Log("テキストファイル取得開始");
  45. yield return StartCoroutine(getCardListText(SetID));
  46. IEnumerator getCardListText(string SetID)
  47. {
  48. List<string> CardListIDs = DataBase.CardListIDs(SetID, isEnglish);
  49. foreach (string CardListID in CardListIDs)
  50. {
  51. string url = "";
  52. if (isEnglish)
  53. {
  54. url = $"https://world.digimoncard.com/cardlist/index.php?search=true&category={CardListID}";
  55. }
  56. else
  57. {
  58. url = $"https://digimoncard.com/cardlist/index.php?search=true&category={CardListID}";
  59. }
  60. WebClient wc = new WebClient();
  61. wc.Encoding = Encoding.UTF8;
  62. string resultText = wc.DownloadString(url);
  63. List<string> parseByEnter = resultText.Split('\n').ToList();
  64. int startIndex = 0;
  65. for (int i = 0; i < parseByEnter.Count; i++)
  66. {
  67. if (!string.IsNullOrEmpty(parseByEnter[i]))
  68. {
  69. bool replaceEmpty = true;
  70. if (isEnglish)
  71. {
  72. if (i >= 1)
  73. {
  74. if (!string.IsNullOrEmpty(parseByEnter[i]) && !string.IsNullOrEmpty(parseByEnter[i - 1]))
  75. {
  76. if (parseByEnter[i - 1].Contains("Digivolveeffect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  77. {
  78. replaceEmpty = false;
  79. }
  80. if (parseByEnter[i - 1].Contains("Securityeffect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  81. {
  82. replaceEmpty = false;
  83. }
  84. if (parseByEnter[i - 1].Contains("Effect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  85. {
  86. replaceEmpty = false;
  87. }
  88. }
  89. }
  90. }
  91. parseByEnter[i] = parseByEnter[i].Replace("\t", "").Replace("\n", "").Trim();
  92. if (replaceEmpty)
  93. {
  94. parseByEnter[i] = parseByEnter[i].Replace(" ", "").Trim();
  95. }
  96. else
  97. {
  98. Debug.Log($"!replaceEmpty:{parseByEnter[i]}");
  99. }
  100. if (parseByEnter[i].Contains($"<liclass=\"image_lists_itemdatapage-1\">"))
  101. {
  102. startIndex = i;
  103. break;
  104. }
  105. }
  106. }
  107. parseByEnter = parseByEnter.GetRange(startIndex, parseByEnter.Count - startIndex);
  108. int endIndex = 0;
  109. for (int i = 0; i < parseByEnter.Count; i++)
  110. {
  111. if (!string.IsNullOrEmpty(parseByEnter[i]))
  112. {
  113. bool replaceEmpty = true;
  114. if (isEnglish)
  115. {
  116. if (i >= 1)
  117. {
  118. if (!string.IsNullOrEmpty(parseByEnter[i]) && !string.IsNullOrEmpty(parseByEnter[i - 1]))
  119. {
  120. if (parseByEnter[i - 1].Contains("Digivolveeffect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  121. {
  122. replaceEmpty = false;
  123. }
  124. if (parseByEnter[i - 1].Contains("Securityeffect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  125. {
  126. replaceEmpty = false;
  127. }
  128. if (parseByEnter[i - 1].Contains("Effect") && parseByEnter[i - 1].Contains("<dt>") && parseByEnter[i - 1].Contains("</dt>"))
  129. {
  130. replaceEmpty = false;
  131. }
  132. }
  133. }
  134. }
  135. parseByEnter[i] = parseByEnter[i].Replace("\t", "").Replace("\n", "").Trim();
  136. if (replaceEmpty)
  137. {
  138. parseByEnter[i] = parseByEnter[i].Replace(" ", "").Trim();
  139. }
  140. if (parseByEnter[i].Contains($"clearfix"))
  141. {
  142. for (int j = 0; j < 7; j++)
  143. {
  144. if (!string.IsNullOrEmpty(parseByEnter[i - j]))
  145. {
  146. if (parseByEnter[i - j].Contains($"</ul>"))
  147. {
  148. if (parseByEnter[i - j].Contains($"</li>"))
  149. {
  150. endIndex = i - j;
  151. }
  152. else
  153. {
  154. endIndex = i - j - 1;
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. parseByEnter = parseByEnter.GetRange(0, endIndex + 1);
  164. parseByEnter[parseByEnter.Count - 1] = parseByEnter[parseByEnter.Count - 1].Replace("</ul>", "");
  165. string savingText = "";
  166. for (int i = 0; i < parseByEnter.Count; i++)
  167. {
  168. savingText += $"{parseByEnter[i]}\n";
  169. }
  170. StreamWriter sw = new StreamWriter(Application.dataPath + $"/TextAsset/{CardListID}.txt", false);// 指定されたファイル名のテキストファイルを新規で用意
  171. sw.WriteLine(savingText);// ファイルに書き出したあと改行
  172. sw.Flush();// StreamWriterのバッファに書き出し残しがないか確認
  173. sw.Close();// ファイルを閉じる
  174. Debug.Log(Application.dataPath + $"/TextAsset/{CardListID}.txt");
  175. Debug.Log($"保存:{SetID},{CardListID}");
  176. yield return new WaitForSeconds(0.5f);
  177. }
  178. }
  179. }
  180. public void StopGetCardImages()
  181. {
  182. StopAllCoroutines();
  183. }
  184. public void OnClickGetEnglishCardImageButton()
  185. {
  186. StopGetCardImages();
  187. StartCoroutine(GetCardImages(cardIDs, true));
  188. }
  189. public void OnClickGetJapaneseCardImageButton()
  190. {
  191. StopGetCardImages();
  192. StartCoroutine(GetCardImages(cardIDs, false));
  193. }
  194. IEnumerator GetCardImages(List<int> cardIDs, bool isEnglish)
  195. {
  196. picsCount = 0;
  197. Debug.Log("Getting Card Images....");
  198. if (minParallelID < 0)
  199. {
  200. minParallelID = 0;
  201. }
  202. for (int i = minParallelID; i < maxParallelID + 1; i++)
  203. {
  204. foreach (int cardID in cardIDs)
  205. {
  206. yield return StartCoroutine(GetCardImage(cardID, i, isEnglish));
  207. yield return new WaitForSeconds(0.1f);
  208. }
  209. }
  210. Debug.Log("Obtained Images");
  211. }
  212. int picsCount = 0;
  213. IEnumerator GetCardImage(int cardID, int ParallelID, bool isEnglish)
  214. {
  215. bool Parallel = ParallelID >= 1;
  216. while (Application.internetReachability == NetworkReachability.NotReachable)
  217. {
  218. Debug.Log("ネットワークに接続されていない");
  219. yield return new WaitForSeconds(5f);
  220. }
  221. string cardIDString = string.Format("{0:000}", cardID);
  222. if (SetID.Contains("ST"))
  223. {
  224. cardIDString = string.Format("{0:00}", cardID);
  225. }
  226. string cardImageURL = $"{SetID}-{cardIDString}";
  227. if (Parallel)
  228. {
  229. cardImageURL += $"_P{ParallelID}";
  230. }
  231. string picsURL = $"";
  232. if (isEnglish)
  233. {
  234. picsURL = $"https://world.digimoncard.com/images/cardlist/card/{cardImageURL}.png";
  235. }
  236. else
  237. {
  238. picsURL = $"https://digimoncard.com/images/cardlist/card/{cardImageURL}.png";
  239. }
  240. UnityWebRequest webReq_CardImage = UnityWebRequestTexture.GetTexture(picsURL);
  241. yield return webReq_CardImage.SendWebRequest();
  242. if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError || webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
  243. {
  244. }
  245. else
  246. {
  247. try
  248. {
  249. string fileName = $"{cardImageURL}.png";
  250. string ImagePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, @"..\..\Textures\Card\"));
  251. if (isEnglish)
  252. {
  253. Debug.Log(ImagePath);
  254. File.WriteAllBytes(ImagePath + fileName, webReq_CardImage.downloadHandler.data);
  255. }
  256. else
  257. {
  258. File.WriteAllBytes(@$"C:\Users\USER\Pictures\DigimonCard/Card_JPN/{fileName}", webReq_CardImage.downloadHandler.data);
  259. }
  260. picsCount++;
  261. Debug.Log($"{fileName}� {picsCount}/{maxCardID}");
  262. }
  263. catch (Exception ex)
  264. {
  265. Debug.Log(ex.Message);
  266. }
  267. }
  268. yield return null;
  269. }
  270. }