TestWWW.cs 11 KB

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