)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Default (Ia.Statistics.Cl.Models.Taw9eel)

Public general use code classes and xml files that we've compiled and used over the years:

Structure of the taw9eel.com website.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.IO;
    4: using System.Linq;
    5: using System.Reflection;
    6: using System.Xml.Linq;
    7: using System.Text.RegularExpressions;
    8: using System.Text;
    9:  
   10: namespace Ia.Statistics.Cl.Models.Taw9eel
   11: {
   12:     ////////////////////////////////////////////////////////////////////////////
   13:  
   14:     /// <summary publish="true">
   15:     /// Structure of the taw9eel.com website.
   16:     /// </summary>
   17:     /// <remarks> 
   18:     /// Copyright © 2024-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   19:     ///
   20:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   21:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   22:     ///
   23:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   24:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   25:     /// 
   26:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   27:     /// 
   28:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   29:     /// </remarks> 
   30:     public class Default
   31:     {
   32:         private static XDocument xd;
   33:  
   34:         private static List<Ia.Statistics.Cl.Models.Category> categoryList;
   35:  
   36:         private static List<Ia.Statistics.Cl.Models.Product> productList;
   37:  
   38:         private static Dictionary<Ia.Statistics.Cl.Models.Category, string> categoryToNextUrlDictionary = new Dictionary<Ia.Statistics.Cl.Models.Category, string>();
   39:  
   40:         ////////////////////////////////////////////////////////////////////////////
   41:  
   42:         /// <summary>
   43:         /// 
   44:         /// </summary>
   45:         public Default() { }
   46:  
   47:         ////////////////////////////////////////////////////////////////////////////
   48:  
   49:         /// <summary>
   50:         ///
   51:         /// </summary>
   52:         public static string ReadUpdateProductListOfCategorySequentially()
   53:         {
   54:             string s;
   55:  
   56:             Ia.Statistics.Cl.Models.Category category;
   57:  
   58:             if (categoryToNextUrlDictionary.Count > 0)
   59:             {
   60:                 var categoryToNextUrl = categoryToNextUrlDictionary.First();
   61:  
   62:                 category = categoryToNextUrl.Key;
   63:                 category.Url = categoryToNextUrl.Value;
   64:  
   65:                 categoryToNextUrlDictionary.Remove(category);
   66:             }
   67:             else
   68:             {
   69:                 category = Ia.Statistics.Cl.Models.Taw9eel.Default.CategoryList.RandomThenNext();
   70:                 //category = (from c in Ia.Statistics.Cl.Model.Taw9eel.Default.CategoryList where c.Url.Contains("chocolate-candy.html") select c).SingleOrDefault();
   71:             }
   72:  
   73:             var site = Ia.Statistics.Cl.Models.Site.SiteFromId(category.SiteId);
   74:  
   75:             var productPriceStockQuantitySoldList = ProductPriceStockListOfCategory(site, category, out string exception);
   76:  
   77:             if (string.IsNullOrEmpty(exception))
   78:             {
   79:                 if (productPriceStockQuantitySoldList.Count > 0)
   80:                 {
   81:                     s = string.Empty;
   82:  
   83:                     foreach (var productPriceStockQuantitySold in productPriceStockQuantitySoldList)
   84:                     {
   85:                         if (!string.IsNullOrEmpty(productPriceStockQuantitySold.Product.Sku))
   86:                         {
   87:                             Ia.Statistics.Cl.Models.Product.Create(productPriceStockQuantitySold.Product);
   88:                             Ia.Statistics.Cl.Models.ProductPriceSpot.Create(productPriceStockQuantitySold.Product, productPriceStockQuantitySold.Price);
   89:                             Ia.Statistics.Cl.Models.ProductStockSpot.Create(productPriceStockQuantitySold.Product, productPriceStockQuantitySold.Stock);
   90:  
   91:                             s += "Product: " + productPriceStockQuantitySold.Product.Name + "; price: " + productPriceStockQuantitySold.Price + "; stock: " + productPriceStockQuantitySold.Stock + ". ";
   92:                         }
   93:                         else
   94:                         {
   95:                             s += "Product: " + productPriceStockQuantitySold.Product.Name + ": SKU is null. ";
   96:                         }
   97:                     }
   98:                 }
   99:                 else s = "Product list is 0. ";
  100:  
  101:                 s = site.Name + ": " + category.Url + ": " + s;
  102:             }
  103:             else s = exception;
  104:  
  105:             return s;
  106:         }
  107:  
  108:         ////////////////////////////////////////////////////////////////////////////
  109:  
  110:         /// <summary>
  111:         ///
  112:         /// </summary>
  113:         public static List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> ProductPriceStockListOfCategory(Ia.Statistics.Cl.Models.Site site, Ia.Statistics.Cl.Models.Category category, out string exception)
  114:         {
  115:             var productPriceStockQuantitySoldList = new List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold>();
  116:  
  117:             using (var webClient = new System.Net.WebClient())
  118:             {
  119:                 webClient.Headers.Clear();
  120:                 webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  121:  
  122:                 var content = webClient.DownloadString(category.Url);
  123:  
  124:                 ProductPriceStockListFromCategoryContent(site, category, content, out productPriceStockQuantitySoldList, out exception);
  125:             }
  126:  
  127:             return productPriceStockQuantitySoldList;
  128:         }
  129:  
  130:         ////////////////////////////////////////////////////////////////////////////
  131:  
  132:         /// <summary>
  133:         ///
  134:         /// </summary>
  135:         public static void ProductPriceStockListFromCategoryContent(Ia.Statistics.Cl.Models.Site site, Ia.Statistics.Cl.Models.Category category, string content, out List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> productPriceStockQuantitySoldList, out string exception)
  136:         {
  137:             productPriceStockQuantitySoldList = new List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold>();
  138:  
  139:             // below: check if content has a Next link
  140:             var match0 = Regex.Match(content, @"<a class=""next i-next"" href=""(.+?)"" title=""Next"">.+?</a>", RegexOptions.Singleline, Ia.Statistics.Cl.Models.Default.TimeoutTimeSpan);
  141:  
  142:             if (match0.Success)
  143:             {
  144:                 var nextUrl = match0.Groups[1].Value;
  145:  
  146:                 categoryToNextUrlDictionary[category] = nextUrl;
  147:             }
  148:  
  149:  
  150:             var matchCollection = Regex.Matches(content, @"<h2 class=""product-name""><a href=""(.+?)"".+?<button.+?id-data=""(\d+?)"".+?name-data=""(.+?)"".+?category-data=""(.+?)"".+?price-USD-data=""(.+?)"".+?quantity-data=""(.+?)"".+?price-data=""(.+?)"".+?</button>", RegexOptions.Singleline, Ia.Statistics.Cl.Models.Default.TimeoutTimeSpan);
  151:  
  152:             foreach (Match match in matchCollection)
  153:             {
  154:                 var product = new Ia.Statistics.Cl.Models.Product();
  155:                 var productPriceStockQuantitySold = new Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold();
  156:  
  157:                 var siteId = site.Id;
  158:                 var sku = match.Groups[2].Value; // id-data
  159:  
  160:                 var name = match.Groups[3].Value; // name-data
  161:                 name = Ia.Statistics.Cl.Models.Default.NormalizeName(name);
  162:  
  163:                 var url = match.Groups[1].Value; // slug;
  164:  
  165:                 product.Id = Ia.Statistics.Cl.Models.Product.ProductId(site, sku);
  166:                 product.SiteId = siteId;
  167:                 //product.Category = category;
  168:                 product.Sku = sku;
  169:                 product.Name = name;
  170:                 product.Url = url;
  171:  
  172:                 var price = decimal.TryParse(match.Groups[7].Value, out decimal d) ? d : 0; // price-data
  173:  
  174:                 var stock = (int)decimal.Parse(match.Groups[6].Value); // quantity-data
  175:                 stock = stock <= Ia.Statistics.Cl.Models.Default.MaximumReasonableStockValue ? stock : Ia.Statistics.Cl.Models.Default.MaximumReasonableStockValue;
  176:  
  177:                 productPriceStockQuantitySold.Product = product;
  178:                 productPriceStockQuantitySold.Price = price;
  179:                 productPriceStockQuantitySold.Stock = stock;
  180:  
  181:                 productPriceStockQuantitySoldList.Add(productPriceStockQuantitySold);
  182:             }
  183:  
  184:             exception = string.Empty;
  185:         }
  186:  
  187:         ////////////////////////////////////////////////////////////////////////////
  188:  
  189:         /// <summary>
  190:         ///
  191:         /// </summary>
  192:         public static List<Ia.Statistics.Cl.Models.Category> CategoryList
  193:         {
  194:             get
  195:             {
  196:                 if (categoryList == null || categoryList.Count == 0)
  197:                 {
  198:                     categoryList = (from c in XDocument.Element("taw9eel").Descendants("category")
  199:                                     where !c.HasElements
  200:                                     select new Ia.Statistics.Cl.Models.Category
  201:                                     {
  202:                                         //Id = Ia.Statistics.Cl.Model.Category.CategoryId(site, int.Parse(c.Attribute("id").Value)),
  203:                                         Id = 0, //int.Parse(c.Attribute("id").Value),
  204:                                         SiteId = Ia.Statistics.Cl.Models.Site.Taw9eel.Id,
  205:                                         Url = Ia.Statistics.Cl.Models.Site.Taw9eel.BaseUrl + c.Attribute("url").Value,
  206:                                         Name = c.Attribute("name").Value
  207:                                     }
  208:                                     ).ToList<Ia.Statistics.Cl.Models.Category>();
  209:  
  210:                     categoryList = categoryList.Shuffle().ToList();
  211:                 }
  212:  
  213:                 return categoryList;
  214:             }
  215:         }
  216:  
  217:         ////////////////////////////////////////////////////////////////////////////
  218:  
  219:         /// <summary>
  220:         /// 
  221:         /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
  222:         /// 
  223:         /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
  224:         /// 2. Add "using System.Reflection".
  225:         /// 3. Manifest resource stream will start with the project namespace, the location of XML file.
  226:         /// 
  227:         /// </summary>
  228:  
  229:         private static XDocument XDocument
  230:         {
  231:             get
  232:             {
  233:                 Assembly assembly;
  234:                 StreamReader streamReader;
  235:  
  236:                 xd = null;
  237:                 assembly = Assembly.GetExecutingAssembly();
  238:                 streamReader = new StreamReader(assembly.GetManifestResourceStream("Ia.Statistics.Cl.Models.taw9eel.com.default.xml"));
  239:  
  240:                 try
  241:                 {
  242:                     if (streamReader.Peek() != -1)
  243:                     {
  244:                         xd = System.Xml.Linq.XDocument.Load(streamReader);
  245:                     }
  246:                 }
  247:                 catch (Exception)
  248:                 {
  249:                 }
  250:                 finally
  251:                 {
  252:                 }
  253:  
  254:                 return xd;
  255:             }
  256:         }
  257:  
  258:         ////////////////////////////////////////////////////////////////////////////
  259:         ////////////////////////////////////////////////////////////////////////////
  260:     }
  261: }