using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using System.Xml.XPath;
//<table class="common-table medium js-table js-streamable-table">
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public async Task<string> Get()
{
try
{
//HttpClient client = new HttpClient();
//var uri = new Uri("" ,
//var x = await client.GetStringAsync()"https://in.investing.com/equities/trending-stocks/technical");
//https://in.investing.com/stock-screener/?sp=country::14|sector::a|industry::a|equityType::a<eq_market_cap:1
//https://in.investing.com/stock-screener/?sp=country::14|sector::a|industry::a|equityType::a%3Ceq_market_cap;1
//var url = "https://in.investing.com/equities/trending-stocks/technical";
//var url = "https://in.investing.com/stock-screener/?" + Uri.EscapeDataString("sp=country::14|sector::a|industry::a|equityType::a<eq_market_cap:1");
var url = "https://in.investing.com/equities/trending-stocks/technical";
using var client = new HttpClient();
var msg = new HttpRequestMessage(HttpMethod.Get, url);
msg.Headers.Add("User-Agent", "Mozilla/5.0");
var res = await client.SendAsync(msg);
var content = await res.Content.ReadAsStringAsync();
//XPathDocument xPath = new XPathDocument(content);
//var nav = xPath.CreateNavigator();
//var iter = nav.Select("/");
//StreamReader textReader = new StreamReader(content);
//XDocument doc = XDocument.Parse(content);
//XElement xElement = XElement.Parse(content);
int StartIndex = content.IndexOf("<table class=\"common-table medium js-table js-streamable-table\">");
int EndIndex = content.IndexOf("</table>", StartIndex);
string usefulContent = content.Substring(StartIndex, EndIndex+ 8 - StartIndex);
//XDocument doc = XDocument.Parse(usefulContent);
//XElement xElement = XElement.Parse(usefulContent);
var lst = ParseString(usefulContent);
Console.WriteLine(content);
}
catch ( Exception ex)
{
}
return "test";
}
private List<string> ParseString(string inpString)
{
string StartPattern = "<tr class=\"common-table-item u-clickable \"";
string EndPattern = "</tr>";
var lst = ExtractHtmlTags(inpString, StartPattern, EndPattern);
var titleList = new List<String>();
var hourList = new List<String>();
var dayList = new List<String>();
var weekList = new List<String>();
var monthList = new List<String>();
var companyList = new List<Company>();
foreach (var item in lst)
{
Company company = new Company();
company.Title = ExtractHtmlTags(ExtractHtmlTags(item, "<a", "</a>")[0], ">", "</", true)[0];
company.Hourly = ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_hour", "</td>")[0], "<span class=\"text\">", "</span>", true)[0];
company.Daily = ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_day", "</td>")[0], "<span class=\"text\">", "</span>", true)[0];
company.Weekly = ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_week", "</td>")[0], "<span class=\"text\">", "</span>", true)[0];
company.Monthly = ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_month", "</td>")[0], "<span class=\"text\">", "</span>", true)[0];
companyList.Add(company);
titleList.Add(ExtractHtmlTags(ExtractHtmlTags(item, "<a", "</a>")[0] , ">" , "</", true)[0]);
hourList.Add(ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_hour", "</td>")[0] , "<span class=\"text\">" , "</span>", true)[0]);
dayList.Add(ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_day", "</td>")[0], "<span class=\"text\">", "</span>", true)[0]);
weekList.Add(ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_week", "</td>")[0], "<span class=\"text\">", "</span>", true)[0]);
monthList.Add(ExtractHtmlTags(ExtractHtmlTags(item, "<td class=\"col-technical_month", "</td>")[0], "<span class=\"text\">", "</span>", true)[0]);
}
return lst;
}
private List<string> ExtractHtmlTags(string inpString , string StartPattern, string EndPattern, bool excludePatterns = false)
{
List<string> lst = new List<string>();
int StartIndex = 0; int EndIndex = 0;
StartIndex = inpString.IndexOf(StartPattern);
EndIndex = inpString.IndexOf(EndPattern, StartIndex + 1);
while (StartIndex > 0 && EndIndex > 0)
{
int substringStartIndex = StartIndex;
int substringEndIndex = EndIndex - StartIndex + EndPattern.Length;
if ( excludePatterns)
{
substringStartIndex = StartIndex + StartPattern.Length;
substringEndIndex = EndIndex - substringStartIndex;
}
string trString = inpString.Substring(substringStartIndex, substringEndIndex);
lst.Add(trString);
if (EndIndex + EndPattern.Length + 1 >= inpString.Length) break;
StartIndex = inpString.IndexOf(StartPattern, EndIndex);
EndIndex = inpString.IndexOf(EndPattern, StartIndex + 1);
}
return lst;
}
}
public class Company
{
public string? Title { get; set; }
public string? Hourly { get; set; }
public string? Daily { get; set; }
public string? Weekly { get; set; }
public string? Monthly { get; set; }
}
}