public static SouqProductBucket GetProductBucket(string productFullId) { retry: SyncSouqCookies(); var url = $"https://egypt.souq.com/eg-en/{productFullId}/i/"; try { var document = WebScrapper.GetDownloadHtml(url); var globalBucketNode = document.DocumentNode.Descendants().First(l => l.Name == "script" && l.InnerHtml.Contains("globalBucket")); var globalBucketData = globalBucketNode.InnerText.Replace("var globalBucket =", "").TrimStart().TrimEnd(); return(JsonConvert.DeserializeObject <SouqProductBucket>(globalBucketData)); } catch (HtmlScapperException ex) { if (ex.InnerException.Message == "Too many automatic redirections were attempted.") { return(null); } goto retry; } }
public static HtmlDocument GetSellerProfile(string sellerId) { retry: var url = $"https://egypt.souq.com/eg-en/{sellerId.ToPercentageEncoding(SouqApiConstants.PercentageEncodingLevel)}/p/profile.html"; SyncSouqCookies(); try { var document = WebScrapper.GetDownloadHtml(url); return(document); } catch (HtmlScapperException ex) { goto retry; } }
public static HtmlDocument GetCategories() { retry: var url = $"https://egypt.souq.com/eg-en/shop-all-categories/c/?ref=nav"; SyncSouqCookies(); try { var document = WebScrapper.GetDownloadHtml(url); return(document); } catch (HtmlScapperException ex) { goto retry; } }
public static (SouqProductBucket bucket, IDictionary <string, SouqToken> tokens) GetProductResultsHtml(string productFullId) { retry: SyncSouqCookies(); var url = $"https://egypt.souq.com/eg-en/{productFullId}/i/"; HtmlDocument document; try { document = WebScrapper.GetDownloadHtml(url); } catch (HtmlScapperException ex) { goto retry; } #region Bucket var globalBucketNode = document.DocumentNode.Descendants().First(l => l.Name == "script" && l.InnerHtml.Contains("globalBucket")); var globalBucketData = globalBucketNode.InnerText.Replace("var globalBucket =", "").TrimStart().TrimEnd(); var bucketObj = JsonConvert.DeserializeObject <SouqProductBucket>(globalBucketData); #endregion #region Token Dictionary <string, SouqToken> tmpResult = new Dictionary <string, SouqToken>(); var formIds = new[] { "searchForm", "addItemToCart", "reviews-list-id" }; int index = 0; var formResults = new SouqToken[3]; foreach (var formId in formIds) { var filter_hitNodeExist = document.DocumentNode.Descendants() .AnyByNameNAttribute("form", "id", formId); if (filter_hitNodeExist) { var filter_hitsNode = document.DocumentNode.Descendants() .SingleByNameNAttribute("form", "id", formId); var filter_hitsCfs = filter_hitsNode.Descendants() .SingleByNameNAttribute("input", "name", "hitsCfs"); var filter_hitsCfsMeta = filter_hitsNode.Descendants() .SingleByNameNAttribute("input", "name", "hitsCfsMeta"); formResults[index] = new SouqToken() { hitsCfs = filter_hitsCfs.Attributes["value"].Value, hitsCfsMeta = filter_hitsCfsMeta.Attributes["value"].Value }; } else { formResults[index] = null; } index++; } for (int i = 0; i < formIds.Length; i++) { tmpResult.Add(formIds[i], formResults[i]); } #endregion return(bucketObj, tmpResult); }
public static SouqProduct GetProduct(string productFullId) { retry: SyncSouqCookies(); var url = $"https://egypt.souq.com/eg-en/{productFullId}/i/"; try { var obj = WebScrapper.GetDownloadJson(url, headers: new Dictionary <string, string>() { { "X-Requested-With", "XMLHttpRequest" } }); SouqProduct product = null; if (obj != null) { product = new SouqProduct { body = obj.Property("body").Value.ToString(), bundles = null }; if (obj.Property("aBundles").Value.ToString() != "[]") { var bundle = obj.Property("aBundles").Value.ToObject <JObject>(); var bundlesList = bundle.Properties().Values().Select(l => l.ToObject <JObject>()).ToList(); product.bundles = new List <SouqBundle>(); foreach (var tmpBundle in bundlesList) { var bundleObj = tmpBundle.ToObject <SouqBundle>(); if (tmpBundle.Property("units") != null) { var units = tmpBundle.Property("units").Value.ToObject <JObject>(); var unitsList = units.Properties().Values().Select(l => l.ToObject <JObject>()).ToList(); bundleObj.units = unitsList.Select(l => l.ToObject <SouqBundleUnits>()).ToList(); } product.bundles.Add(bundleObj); } } } else { var objHtml = WebScrapper.GetDownloadHtml(url); product = new SouqProduct { body = objHtml.DocumentNode.OuterHtml, bundles = null }; } return(product); } catch (HtmlScapperException ex) { goto retry; } }
public static IDictionary <string, SouqToken> GetProductAccessTokens(string productFullId) { retry: SyncSouqCookies(); var url = $"https://egypt.souq.com/eg-en/{productFullId}/i/"; try { Dictionary <string, SouqToken> tmpResult = new Dictionary <string, SouqToken>(); var document = WebScrapper.GetDownloadHtml(url, cookies: Cookies); var formIds = new[] { "searchForm", "addItemToCart", "reviews-list-id" }; int index = 0; var formResults = new SouqToken[3]; foreach (var formId in formIds) { var filter_hitNodeExist = document.DocumentNode.Descendants() .AnyByNameNAttribute("form", "id", formId); if (filter_hitNodeExist) { var filter_hitsNode = document.DocumentNode.Descendants() .SingleByNameNAttribute("form", "id", formId); var filter_hitsCfs = filter_hitsNode.Descendants() .SingleByNameNAttribute("input", "name", "hitsCfs"); var filter_hitsCfsMeta = filter_hitsNode.Descendants() .SingleByNameNAttribute("input", "name", "hitsCfsMeta"); formResults[index] = new SouqToken() { hitsCfs = filter_hitsCfs.Attributes["value"].Value, hitsCfsMeta = filter_hitsCfsMeta.Attributes["value"].Value }; } else { formResults[index] = null; } index++; } for (int i = 0; i < formIds.Length; i++) { tmpResult.Add(formIds[i], formResults[i]); } return(tmpResult); } catch (HtmlScapperException ex) { goto retry; } }