Exemplo n.º 1
0
        public async Task <double> StartScraping(AvailableCurrencies from, AvailableCurrencies to, int amount = 1)
        {
            string Url = SimpleUrl.Insert(SimpleUrl.IndexOf("guru") + "guru".Length + 1, from.ToString());

            Url = Url.Insert(Url.IndexOf(from.ToString()) + from.ToString().Length + 1, to.ToString());
            Url = Url.Insert(Url.IndexOf(to.ToString()) + to.ToString().Length + 1, amount.ToString());

            HttpClient client = new HttpClient();
            string     strHtml;

            try
            {
                strHtml = await client.GetStringAsync(Url);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(-1);
            }

            HtmlDocument htmlDoc = new HtmlDocument();

            htmlDoc?.LoadHtml(strHtml);

            HtmlNode valueNode = htmlDoc.DocumentNode.Descendants("input")
                                 .Where(node => node.GetAttributeValue("data-role", "")
                                        .Equals("secondary-input")).FirstOrDefault();

            string exchange_rate = valueNode.GetAttributeValue("value", "");
            double rate          = -1;

            try
            {
                if (!double.TryParse(exchange_rate, out rate))
                {
                    double.TryParse(exchange_rate.Replace('.', ','), out rate);
                }
            }
            catch (Exception)
            {
                return(rate);
            }

            return(rate);
        }
Exemplo n.º 2
0
 public IDictionary <Currency, decimal> RatesOf(Currency target) => AvailableCurrencies.ToDictionary(curr => curr, curr => RateOf(curr, target));
        private async Task <List <Item> > StartSearch(RequestModel model)
        {
            List <string> sites     = model.CheckedSites;
            string        itemName  = model.ItemName;
            int           itemCount = model.ItemCount;
            int           minPrice  = model.MinPrice == null ? 0 : (int)model.MinPrice;
            int           maxPrice  = model.MaxPrice == null ? 0 : (int)model.MaxPrice;
            List <Item>   AllItems  = new List <Item>();

            //AutoResetEvent auto = new AutoResetEvent(false);
            List <Task> Tasks = new List <Task>();

            foreach (string s in sites)
            {
                Task task = Task.Run(async() =>
                {
                    Assembly asm = null;
                    try
                    {
                        string asmPath = HttpRuntime.AppDomainAppPath + $"Plugins/{s}Library.dll";
                        asm            = Assembly.LoadFrom(asmPath);
                    }
                    catch (FileNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                        //auto.Set();
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                        //auto.Set();
                    }

                    var pluginTypes = from t in asm.GetTypes()
                                      where t.IsClass && (t.BaseType == typeof(BaseScraper))
                                      select t;

                    foreach (Type t in pluginTypes)
                    {
                        BaseScraper obj = (BaseScraper)Activator.CreateInstance(t);

                        int min = 0, max = 0;
                        if (minPrice != 0 || maxPrice != 0)
                        {
                            AvailableCurrencies currencyFrom = (AvailableCurrencies)model.Currency;
                            CurrencyAttribute curr_attr      = t.GetCustomAttribute(typeof(CurrencyAttribute)) as CurrencyAttribute;
                            AvailableCurrencies currencyTo   = curr_attr.Currency;
                            CurrencyScraper cs = new CurrencyScraper();

                            if (currencyFrom != currencyTo)
                            {
                                double rate = await cs.StartScraping(currencyFrom, currencyTo);
                                min         = Convert.ToInt32(rate * minPrice);
                                max         = Convert.ToInt32(rate * maxPrice);
                            }
                            else
                            {
                                min = minPrice;
                                max = maxPrice;
                            }
                        }

                        List <Item> items = await obj?.StartScraping(itemName, itemCount, min, max);
                        AllItems.AddRange(items);
                        //auto.Set();
                    }
                });
                Tasks.Add(task);
            }

            //foreach (Task t in Tasks)
            //    auto.WaitOne();

            await Task.WhenAll(Tasks);

            return(AllItems);
        }
 public CurrencyAttribute(AvailableCurrencies currency) => this.Currency = currency;
 private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
 {
     filteredCurrencies       = AvailableCurrencies.Where(x => x.Name.ToLower().Contains(e.NewTextValue.ToLower()) || x.Code.ToLower().Contains(e.NewTextValue.ToLower())).ToList();
     currencyList.ItemsSource = FilteredCurrencies;
 }