Advertisement
enkacang

FetchImage - QAR EN NIZAM

Mar 13th, 2023
2,800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using Unity.VisualScripting;
  5.  
  6. public class FetchImages : MonoBehaviour
  7. {
  8.     public string imagesUrl = "https://gementar.com/qar_en_nizam/";
  9.     private string[] imageUrls;
  10.     private string localFolderPath;
  11.  
  12.     public int totalImages;
  13.     public int currentImages;
  14.  
  15.     void Start()
  16.     {
  17.  
  18.         localFolderPath = Path.Combine(Application.persistentDataPath, "Images");
  19.         if (!Directory.Exists(localFolderPath))
  20.         {
  21.             Directory.CreateDirectory(localFolderPath);
  22.         }
  23.  
  24.         Debug.Log("Data Path : " + Application.persistentDataPath);
  25.         StartCoroutine(GetImageUrls());
  26.     }
  27.  
  28.     private bool downloadFinishedOneTimeTrigger = true;
  29.     private bool canCount = false;
  30.     private void Update()
  31.     {
  32.         if(isDownloadFinished() && downloadFinishedOneTimeTrigger && canCount)
  33.         {
  34.             Debug.Log("Finished");
  35.             downloadFinishedOneTimeTrigger = false;
  36.         }
  37.     }
  38.  
  39.     private bool isDownloadFinished()
  40.     {
  41.         if (currentImages >= totalImages)
  42.         {
  43.             return true;
  44.         }
  45.  
  46.         return false;
  47.  
  48.     }
  49.  
  50.     IEnumerator GetImageUrls()
  51.     {
  52.         WWW www = new WWW(imagesUrl);
  53.         yield return www;
  54.  
  55.         string html = www.text;
  56.         imageUrls = ParseImageUrls(html);
  57.  
  58.         foreach (string url in imageUrls)
  59.         {
  60.             StartCoroutine(DownloadImage(url));
  61.         }
  62.     }
  63.  
  64.     string[] ParseImageUrls(string html)
  65.     {
  66.         string[] splitHtml = html.Split('"');
  67.         ArrayList urls = new ArrayList();
  68.  
  69.        
  70.  
  71.         for (int i = 0; i < (splitHtml.Length); i++)
  72.         {
  73.             if (splitHtml[i].EndsWith(".jpg") || splitHtml[i].EndsWith(".png"))
  74.             {
  75.                 if (splitHtml[i].StartsWith("/")) continue;
  76.  
  77.                 urls.Add(imagesUrl + splitHtml[i]);
  78.  
  79.                 totalImages++;
  80.             }
  81.         }
  82.  
  83.         canCount = true;
  84.  
  85.         return (string[])urls.ToArray(typeof(string));
  86.     }
  87.  
  88.     IEnumerator DownloadImage(string imageUrl)
  89.     {
  90.         string filename = GetFileNameFromUrl(imageUrl);
  91.         string localFilePath = Path.Combine(localFolderPath, filename);
  92.  
  93.         if (!File.Exists(localFilePath))
  94.         {
  95.             using (WWW www = new WWW(imageUrl))
  96.             {
  97.                 yield return www;
  98.  
  99.                 if (www.error == null)
  100.                 {
  101.                     File.WriteAllBytes(localFilePath, www.bytes);
  102.                     Debug.Log("Downloaded: " + imageUrl);
  103.                 }
  104.                 else
  105.                 {
  106.                     Debug.Log("Error downloading: " + imageUrl + " - " + www.error);
  107.                 }
  108.             }
  109.         }
  110.         else
  111.         {
  112.             Debug.Log("File already exists: " + imageUrl);
  113.            
  114.         }
  115.  
  116.         currentImages++;
  117.  
  118.     }
  119.  
  120.     string GetFileNameFromUrl(string url)
  121.     {
  122.         int index = url.LastIndexOf("/") + 1;
  123.         return url.Substring(index, url.Length - index);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement