using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace NewsExtractor
{
class Program
{
static void Main(string[] args)
{
NewsEntities newsEntities = new NewsEntities();
var list = newsEntities.NewsURLs;
foreach (var url in list)
{
var data = GetURLData(url.URL.Replace("200", url.Count.ToString()));
AddNews(data);
}
}
private static string GetURLData(string url)
{
Console.WriteLine(url);
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36)");
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
var s = reader.ReadToEnd();
data.Close();
reader.Close();
return s;
}
private static void AddNews(string fileData)
{
NewsEntities newsEntities = new NewsEntities();
var weatherForecast = JsonConvert.DeserializeObject<RootNew>(fileData);
foreach (var item in weatherForecast.data.items)
{
if (!newsEntities.News.Any(p => p.TickerTapeId == item._id))
{
News n = new News();
n.Title = item.title;
n.TickerTapeId = item._id;
n.Description = item.description;
n.NewsDate2 = item.date;
n.NewsDate = item.date.ToString();
n.Image = item.image;
n.Stocks = string.Join(",", item.stocks.Select(p => p.sid));
n.link = item.link;
newsEntities.News.Add(n);
Console.WriteLine(item.title);
}
}
newsEntities.SaveChanges();
}
}
}