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); }
private void LoadAttributes(ModelMetadata metadata) { //TO-DO: Refazer os métodos para tornar-los mais dinâmicos... if (metadata != null) { MetadataAttribute commonAttribute = new MetadataAttribute() { AttributeName = "Common" + "Attribute", Property = new Dictionary <string, object>() { { "DisplayName", metadata.DisplayName }, { "ShortDisplayName", metadata.ShortDisplayName }, { "IsRequired", metadata.IsRequired }, { "IsReadOnly", metadata.IsReadOnly }, { "IsNullableValueType", metadata.IsNullableValueType }, { "Description", metadata.Description }, { "Watermark", metadata.Watermark }, { "ShowForDisplay", metadata.ShowForDisplay }, { "ShowForEdit", metadata.ShowForEdit }, { "DataTypeName", metadata.DataTypeName }, { "IsComplexType", metadata.IsComplexType }, { "EditFormatString", metadata.EditFormatString }, { "HideSurroundingHtml", metadata.HideSurroundingHtml }, { "HtmlEncode", metadata.HtmlEncode }, { "ConvertEmptyStringToNull", metadata.ConvertEmptyStringToNull }, { "NullDisplayText", metadata.NullDisplayText }, { "SimpleDisplayText", metadata.SimpleDisplayText }, { "TemplateHint", metadata.TemplateHint }, { "DisplayFormatString", metadata.DisplayFormatString }, } }; metadataAttributes.Add(commonAttribute); } HtmlAttributesAttribute htmlAttributesAttribute = GetModelMetadataAttributes(metadata).OfType <HtmlAttributesAttribute>().FirstOrDefault(); if (htmlAttributesAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "HtmlAttributes" + "Attribute", Property = new Dictionary <string, object>() { { "ID", htmlAttributesAttribute.ID }, { "Name", htmlAttributesAttribute.Name }, { "Class", htmlAttributesAttribute.Class }, { "Style", htmlAttributesAttribute.Style }, { "Width", htmlAttributesAttribute.Width }, { "Height", htmlAttributesAttribute.Height }, { "Placeholder", htmlAttributesAttribute.Placeholder }, } }; metadataAttributes.Add(metaAttribute); } DataTypeAttribute dataTypeAttribute = GetModelMetadataAttributes(metadata).OfType <DataTypeAttribute>().FirstOrDefault(); if (dataTypeAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "DataType" + "Attribute", Property = new Dictionary <string, object>() { { "DataType", dataTypeAttribute.DataType }, { "ErrorMessage", dataTypeAttribute.ErrorMessage }, { "ErrorMessageResourceName", dataTypeAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", dataTypeAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } DataTypeFieldAttribute dataTypeFieldAttribute = GetModelMetadataAttributes(metadata).OfType <DataTypeFieldAttribute>().FirstOrDefault(); if (dataTypeFieldAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "DataTypeField" + "Attribute", Property = new Dictionary <string, object>() { { "DataType", dataTypeFieldAttribute.DataType }, { "ErrorMessage", dataTypeFieldAttribute.ErrorMessage }, { "ErrorMessageResourceName", dataTypeFieldAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", dataTypeFieldAttribute.RequiresValidationContext }, { "Cols", dataTypeFieldAttribute.Cols }, { "Rows", dataTypeFieldAttribute.Rows }, { "Wrap", (dataTypeFieldAttribute.HardWrap ? "hard" : null) }, { "MinLength", dataTypeFieldAttribute.MinLength }, { "MaxLength", dataTypeFieldAttribute.MaxLength }, } }; metadataAttributes.Add(metaAttribute); } RegularExpressionAttribute regularExpressionAttribute = GetModelMetadataAttributes(metadata).OfType <RegularExpressionAttribute>().FirstOrDefault(); if (regularExpressionAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "RegularExpression" + "Attribute", Property = new Dictionary <string, object>() { { "Pattern", regularExpressionAttribute.Pattern }, { "ErrorMessage", regularExpressionAttribute.ErrorMessage }, { "ErrorMessageResourceName", regularExpressionAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", regularExpressionAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } StringLengthAttribute stringLengthAttribute = GetModelMetadataAttributes(metadata).OfType <StringLengthAttribute>().FirstOrDefault(); if (stringLengthAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "StringLength" + "Attribute", Property = new Dictionary <string, object>() { { "MinimumLength", stringLengthAttribute.MinimumLength }, { "MaximumLength", stringLengthAttribute.MaximumLength }, { "ErrorMessage", stringLengthAttribute.ErrorMessage }, { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) }, { "ErrorMessageResourceName", stringLengthAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", stringLengthAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } MinLengthAttribute minLengthAttribute = GetModelMetadataAttributes(metadata).OfType <MinLengthAttribute>().FirstOrDefault(); if (minLengthAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "MinLength" + "Attribute", Property = new Dictionary <string, object>() { { "Length", minLengthAttribute.Length }, { "TypeId", minLengthAttribute.TypeId }, { "ErrorMessage", minLengthAttribute.ErrorMessage }, { "ErrorMessageResourceName", minLengthAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", minLengthAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } MaxLengthAttribute maxLengthAttribute = GetModelMetadataAttributes(metadata).OfType <MaxLengthAttribute>().FirstOrDefault(); if (maxLengthAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "MaxLength" + "Attribute", Property = new Dictionary <string, object>() { { "Length", maxLengthAttribute.Length }, { "TypeId", maxLengthAttribute.TypeId }, { "ErrorMessage", maxLengthAttribute.ErrorMessage }, { "ErrorMessageResourceName", maxLengthAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", maxLengthAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } DisplayAttribute displayAttribute = GetModelMetadataAttributes(metadata).OfType <DisplayAttribute>().FirstOrDefault(); if (displayAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "Display" + "Attribute", Property = new Dictionary <string, object>() { { "ShortName", displayAttribute.ShortName }, { "Name", displayAttribute.Name }, { "Prompt", displayAttribute.Prompt }, { "GroupName", displayAttribute.GroupName }, { "Description", displayAttribute.Description }, } }; metadataAttributes.Add(metaAttribute); } RequiredAttribute requiredAttribute = GetModelMetadataAttributes(metadata).OfType <RequiredAttribute>().FirstOrDefault(); if (requiredAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "Required" + "Attribute", Property = new Dictionary <string, object>() { { "IsRequired", true }, { "AllowEmptyStrings", requiredAttribute.AllowEmptyStrings }, { "ErrorMessage", requiredAttribute.ErrorMessage }, { "ErrorMessageResourceName", requiredAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", requiredAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } RangeAttribute rangeAttribute = GetModelMetadataAttributes(metadata).OfType <RangeAttribute>().FirstOrDefault(); if (rangeAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "Range" + "Attribute", Property = new Dictionary <string, object>() { { "OperandType", rangeAttribute.OperandType }, { "AllowEmptyStrings", rangeAttribute.Minimum }, { "Maximum", rangeAttribute.Maximum }, { "ErrorMessage", rangeAttribute.ErrorMessage }, { "ErrorMessageResourceName", rangeAttribute.ErrorMessageResourceName }, { "RequiresValidationContext", rangeAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } DisplayFormatAttribute displayFormatAttribute = GetModelMetadataAttributes(metadata).OfType <DisplayFormatAttribute>().FirstOrDefault(); if (displayFormatAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "DisplayFormat" + "Attribute", Property = new Dictionary <string, object>() { { "DataFormatString", displayFormatAttribute.DataFormatString }, { "ApplyFormatInEditMode", displayFormatAttribute.ApplyFormatInEditMode }, { "ConvertEmptyStringToNull", displayFormatAttribute.ConvertEmptyStringToNull }, { "HtmlEncode", displayFormatAttribute.HtmlEncode }, { "NullDisplayText", displayFormatAttribute.NullDisplayText }, { "IsDefault" + "Attribute", displayFormatAttribute.IsDefaultAttribute() }, } }; metadataAttributes.Add(metaAttribute); } CreditCardAttribute creditCardAttribute = GetModelMetadataAttributes(metadata).OfType <CreditCardAttribute>().FirstOrDefault(); if (creditCardAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "CreditCard" + "Attribute", Property = new Dictionary <string, object>() { { "DataType", creditCardAttribute.DataType }, { "CustomDataType", creditCardAttribute.CustomDataType }, { "DisplayFormat", creditCardAttribute.DisplayFormat }, { "ErrorMessage", creditCardAttribute.ErrorMessage }, { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) }, { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName }, { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType }, { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } CustomValidationAttribute customValidationAttribute = GetModelMetadataAttributes(metadata).OfType <CustomValidationAttribute>().FirstOrDefault(); if (customValidationAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "CustomValidation" + "Attribute", Property = new Dictionary <string, object>() { { "ValidatorType", customValidationAttribute.ValidatorType }, { "Method", customValidationAttribute.Method }, { "ErrorMessage", creditCardAttribute.ErrorMessage }, { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) }, { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName }, { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType }, { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } EmailAddressAttribute emailAddressAttribute = GetModelMetadataAttributes(metadata).OfType <EmailAddressAttribute>().FirstOrDefault(); if (emailAddressAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "EmailAddress" + "Attribute", Property = new Dictionary <string, object>() { { "DataType", emailAddressAttribute.DataType }, { "CustomDataType", emailAddressAttribute.CustomDataType }, { "DisplayFormat", emailAddressAttribute.DisplayFormat }, { "ErrorMessage", creditCardAttribute.ErrorMessage }, { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) }, { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName }, { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType }, { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } FileExtensionsAttribute fileExtensionsAttribute = GetModelMetadataAttributes(metadata).OfType <FileExtensionsAttribute>().FirstOrDefault(); if (fileExtensionsAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "FileExtensions" + "Attribute", Property = new Dictionary <string, object>() { { "DataType", emailAddressAttribute.DataType }, { "CustomDataType", emailAddressAttribute.CustomDataType }, { "DisplayFormat", emailAddressAttribute.DisplayFormat }, { "ErrorMessage", creditCardAttribute.ErrorMessage }, { "FormatErrorMessage", stringLengthAttribute.FormatErrorMessage(metadata.PropertyName) }, { "ErrorMessageResourceName", creditCardAttribute.ErrorMessageResourceName }, { "ErrorMessageResourceType", creditCardAttribute.ErrorMessageResourceType }, { "RequiresValidationContext", creditCardAttribute.RequiresValidationContext }, } }; metadataAttributes.Add(metaAttribute); } TimestampAttribute timestampAttribute = GetModelMetadataAttributes(metadata).OfType <TimestampAttribute>().FirstOrDefault(); if (timestampAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "FileExtensions" + "Attribute", Property = new Dictionary <string, object>() { { "TypeId", timestampAttribute.TypeId }, } }; metadataAttributes.Add(metaAttribute); } ViewDisabledAttribute viewDisabledAttribute = GetModelMetadataAttributes(metadata).OfType <ViewDisabledAttribute>().FirstOrDefault(); if (viewDisabledAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "ViewDisabled" + "Attribute", Property = new Dictionary <string, object>() { { "Declared", true }, } }; metadataAttributes.Add(metaAttribute); } TextAreaAttribute textAreaAttribute = GetModelMetadataAttributes(metadata).OfType <TextAreaAttribute>().FirstOrDefault(); if (textAreaAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "TextArea" + "Attribute", Property = new Dictionary <string, object>() { { "Cols", textAreaAttribute.Cols }, { "Rows", textAreaAttribute.Rows }, { "Wrap", (textAreaAttribute.HardWrap ? "hard" : null) }, { "MinLength", textAreaAttribute.MinLength }, { "MaxLength", textAreaAttribute.MaxLength }, } }; metadataAttributes.Add(metaAttribute); } OnlyNumberAttribute onlyNumberAttribute = GetModelMetadataAttributes(metadata).OfType <OnlyNumberAttribute>().FirstOrDefault(); if (onlyNumberAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "OnlyNumber" + "Attribute", Property = new Dictionary <string, object>() { { "Declared", true }, { "ClassDecorator", "onlyNumber" }, } }; metadataAttributes.Add(metaAttribute); } CurrencyAttribute currencyAttribute = GetModelMetadataAttributes(metadata).OfType <CurrencyAttribute>().FirstOrDefault(); if (currencyAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "Currency" + "Attribute", Property = new Dictionary <string, object>() { { "Declared", true }, { "ClassDecorator", "onlyNumber" }, { "Pattern", "currency" }, } }; metadataAttributes.Add(metaAttribute); } NoEspecialCharsAttribute noEspecialCharsAttribute = GetModelMetadataAttributes(metadata).OfType <NoEspecialCharsAttribute>().FirstOrDefault(); if (noEspecialCharsAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "NoEspecialChars" + "Attribute", Property = new Dictionary <string, object>() { { "Declared", true }, { "ClassDecorator", "noCaracEsp" }, } }; metadataAttributes.Add(metaAttribute); } ProgressAttribute progressAttribute = GetModelMetadataAttributes(metadata).OfType <ProgressAttribute>().FirstOrDefault(); if (progressAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "Progress" + "Attribute", Property = new Dictionary <string, object>() { { "MinValue", progressAttribute.MinValue }, { "MaxValue", progressAttribute.MaxValue }, { "Step", progressAttribute.Step }, } }; metadataAttributes.Add(metaAttribute); } PlaceHolderAttribute placeHolderAttribute = GetModelMetadataAttributes(metadata).OfType <PlaceHolderAttribute>().FirstOrDefault(); if (placeHolderAttribute != null) { MetadataAttribute metaAttribute = new MetadataAttribute() { AttributeName = "PlaceHolder" + "Attribute", Property = new Dictionary <string, object>() { { "Declared", true }, { "Text", placeHolderAttribute.Text }, } }; metadataAttributes.Add(metaAttribute); } }