private void SetModelPropertiesValues <TModel>(string contents, TModel modelInstance) { Type modelType = modelInstance.GetType(); PropertyInfo[] modelProperties = modelType.GetProperties(); string pattern; foreach (PropertyInfo propertyInfo in modelProperties) { if ((pattern = propertyInfo.GetPropertyPattern()) == null) { continue; } switch (propertyInfo.PropertyType) { case var type when type == typeof(string): propertyInfo.SetValue(modelInstance, RegexParseHelper.GetSingleMatchString(contents, pattern)); break; case var type when type == typeof(int): propertyInfo.SetValue(modelInstance, RegexParseHelper.GetSingleMatchInt(contents, pattern)); break; default: throw new ArgumentException($"Type {propertyInfo.PropertyType.Name} of the value {propertyInfo.Name} is not supported. " + "See the list of supported types of properties for more information."); } } }
protected override object GetPropertyValue(Type type, string itemContents, string pattern) { switch (type) { case var t when type == typeof(string): return(RegexParseHelper.GetSingleMatchString(itemContents, pattern)); case var t when type == typeof(int): return(RegexParseHelper.GetSingleMatchInt(itemContents, pattern)); default: throw new ArgumentException($"Type {type.Name} is not supported. " + "See the list of supported types of properties for more information."); } }
protected IEnumerable <TorrentSearchResult> GetResultsFromSite(SearchUriBuilder searchUriBuilder, string query, int siteNumber, Func <Uri, string> downloadHtml) { Type searchResultType = typeof(TSearch); Uri searchUri = searchUriBuilder.GetUriWithParameters(query, siteNumber); string contents = downloadHtml(searchUri); // List of single items (item = raw search result (row) from the response) IEnumerable <string> rawResults = RegexParseHelper.GetMatchValues(contents, searchResultType.GetCustomAttribute <ItemPatternAttribute>().Pattern); foreach (string rawResult in rawResults) { TSearch searchResult = new TSearch(); SetModelPropertiesValues(rawResult, searchResult); yield return(searchResult); } }
protected override IEnumerable <string> GetItemRawResults(string contents, string pattern) { return(RegexParseHelper.GetMatchValues(contents, pattern)); }
public override string OnValueSetting(string value) { return(RegexParseHelper.GetSingleMatchString(value, Pattern)); }