Add project files.

This commit is contained in:
2021-02-12 08:09:38 +01:00
parent 1644fce30a
commit fcad75f530
33 changed files with 2768 additions and 0 deletions

18
StockDal/AppConnection.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RepositoryPattern
{
public class AppConnection
{
public static string ConnectionString => ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
public class StockDBConnection
{
public static string ConnectionString => ConfigurationManager.ConnectionStrings["StockDB"].ConnectionString;
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("StockDal")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("StockDal")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4a000259-9a8e-4a83-bdcf-40d06a2ee6cf")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

104
StockDal/StockDal.csproj Normal file
View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4A000259-9A8E-4A83-BDCF-40D06A2EE6CF}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StockDal</RootNamespace>
<AssemblyName>StockDal</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dapper, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.2.0.78\lib\net461\Dapper.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.11.30.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.11.30\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Selenium.WebDriverBackedSelenium, Version=3.141.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.WebDriverBackedSelenium.3.141.0\lib\net45\Selenium.WebDriverBackedSelenium.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Configuration.ConfigurationManager.5.0.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Data.OracleClient" />
<Reference Include="System.Data.SqlClient, Version=4.6.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SqlClient.4.8.2\lib\net461\System.Data.SqlClient.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Net" />
<Reference Include="System.Security" />
<Reference Include="System.Security.AccessControl, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.AccessControl.5.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
</Reference>
<Reference Include="System.Security.Permissions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Permissions.5.0.0\lib\net461\System.Security.Permissions.dll</HintPath>
</Reference>
<Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WebDriver, Version=3.141.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Selenium.WebDriver.3.141.0\lib\net45\WebDriver.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppConnection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StockMarketRepository.cs" />
<Compile Include="StockMemberRepository.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StockDal.Interface\StockDal.Interface.csproj">
<Project>{3e23c2d6-3257-42b1-a6df-f6f7b7e71b17}</Project>
<Name>StockDal.Interface</Name>
</ProjectReference>
<ProjectReference Include="..\StockDomain\StockDomain.csproj">
<Project>{a4b17e50-0ff3-4b94-81f8-e7aab3d333df}</Project>
<Name>StockDomain</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,230 @@
using Newtonsoft.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using StockDal.Interface;
using StockDomain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StockDal
{
public class StockMarketRepository : IStockMarketRepository
{
public Dictionary<string, DiTraderStockRow> StockMarketList { get; set; }
public List<DiTraderStockRow> DumpObjects { get; set; } = new List<DiTraderStockRow>();
public DiTraderStockRow SaveRow { get; set; }
public StringBuilder TextResults { get; set; }
public bool ViewBrowser { get; set; }
public IWebDriver driver;
public StockMarketRepository()
{
ViewBrowser = false;
}
private void Find_Data()
{
TextResults = new StringBuilder();
IList<IWebElement> searchElements = driver.FindElements(By.TagName("tbody"));
foreach (IWebElement i in searchElements)
{
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
var text = i.GetAttribute("innerHTML");
htmlDocument.LoadHtml(text);
var inputs = htmlDocument.DocumentNode.Descendants("tr").ToList();
foreach (var items in inputs)
{
HtmlAgilityPack.HtmlDocument htmlDocument1 = new HtmlAgilityPack.HtmlDocument();
htmlDocument1.LoadHtml(items.InnerHtml);
var tds = htmlDocument1.DocumentNode.Descendants("td").ToList();
var appendText = "";
var fNr = 0;
foreach (var item in tds)
{
var intext = item.InnerText.Replace("\r\n", "");
appendText += appendText.Length == 0 ? intext : " " + intext;
if (tds.Count == 10)
{
AddValueToListRow(fNr++, intext);
}
}
if (!string.IsNullOrEmpty(appendText))
{
TextResults.Append(appendText + "\r\n");
}
htmlDocument1 = null;
}
htmlDocument = null;
TextResults.Append("\r\n");
}
// var oxe = StockMarketList;
}
private void SaveLogging()
{
var output = JsonConvert.SerializeObject(DumpObjects, Formatting.Indented);
File.WriteAllText($"D:\\TimCoDemos\\DemoLogs\\Log{DateTime.Now.ToShortDateString()}.txt",output);
}
private void AddValueToListRow(int pos, string value)
{
switch (pos)
{
case 0:
{
SaveRow = new DiTraderStockRow();
SaveRow.StockName = value;
break;
}
case 1:
{
SaveRow.ProcChange = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 2:
{
SaveRow.RealChange = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 3:
{
SaveRow.BuyPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 4:
{
SaveRow.SellPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 5:
{
SaveRow.LatestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 6:
{
SaveRow.HighestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 7:
{
SaveRow.LowestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
break;
}
case 8:
{
SaveRow.Volume = string.IsNullOrWhiteSpace(value) ? 0 : long.Parse(value.Replace(" ", ""));
break;
}
case 9:
{
SaveRow.TimeOfDay = value==""?TimeSpan.Parse("00:01"): TimeSpan.Parse(value);
//StockMarketList.Add(SaveRow.StockName, SaveRow);
try
{
StockMarketList.Add(SaveRow.StockName, SaveRow);
}
catch (ArgumentException ae)
{
try
{
StockMarketList.Add(SaveRow.StockName + "-2", SaveRow);
}
catch (Exception)
{
DumpObjects.Add(SaveRow);
}
}
break;
}
default:
break;
}
}
private void OpenBrowser(bool burl2 = false)
{
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
if (ViewBrowser)
{
if (driver == null)
{
driver = new ChromeDriver(driverService);
}
}
else
{
if (driver == null)
{
var options = new ChromeOptions();
options.AddArgument("headless");
driver = new ChromeDriver(driverService, options);
}
}
try
{
//var url0 = "https://money.cnn.com/data/hotstocks/index.html";
var url = "https://trader.di.se/index.php/stocklist/index/2055?list=7126";
var url2 = "https://trader.di.se/index.php/stocklist/index/2055?list=7116";
//var url1 = "https://www.finansportalen.se/aktiekurser/";
driver.Navigate().GoToUrl(burl2 ? url2 : url);
}
catch
{
throw;
}
}
public void LoadStockMarketList()
{
StockMarketList = new Dictionary<string, DiTraderStockRow>();
DumpObjects = new List<DiTraderStockRow>();
OpenBrowser();
Find_Data();
OpenBrowser(true);
Find_Data();
if (DumpObjects.Any())
{
SaveLogging();
}
}
public void LoadStockMarketList(bool viewBrowser)
{
StockMarketList = new Dictionary<string, DiTraderStockRow>();
ViewBrowser = viewBrowser;
OpenBrowser();
Find_Data();
}
public void RefreshMarketList()
{
StockMarketList = new Dictionary<string, DiTraderStockRow>();
driver.Navigate().Refresh();
Find_Data();
}
public void Clean()
{
driver?.Quit();
}
}
}

View File

@ -0,0 +1,89 @@
using Dapper;
using RepositoryPattern;
using StockDal.Interface;
using StockDomain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace StockDal
{
public class StockMemberRepository : IStockMemberRepository
{
public bool Delete(string stockMemberId)
{
throw new NotImplementedException();
}
public void InsertMany(List<StockMember> stockMembers)
{
using (IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString))
{
if (db.State == ConnectionState.Closed)
db.Open();
db.Execute(
@"INSERT INTO [dbo].[StockMember]
(StockId
,StockExtId
,BuyValue
,BuyDate
,ActValue
,ActDate
,SoldValue
,SoldDate
,Comment
,PostAmount)
VALUES
(@StockId
,@StockExtId
,@BuyValue
,@BuyDate
,@ActValue
,@ActDate
,@SoldValue
,@SoldDate
,@Comment
,@PostAmount)
", stockMembers);
}
}
public void UpdateActPrice(int Id, decimal price)
{
using (IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString))
{
if (db.State == ConnectionState.Closed)
db.Open();
db.Execute(
@"UPDATE [dbo].[StockMember]
SET ActValue = @val,
ActDate = @date
WHERE Id = @id
", new { val = price, date = DateTime.Today, id = Id });
}
}
IEnumerable<StockMember> IStockMemberRepository.GetStocks()
{
using (IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString))
{
if (db.State == ConnectionState.Closed)
db.Open();
return db.Query<StockMember>(@"SELECT Id
,StockId
,StockExtId
,BuyValue
,BuyDate
,ActValue
,ActDate
,SoldValue
,SoldDate
,Comment
,PostAmount
FROM dbo.StockMember", commandType: CommandType.Text);
}
}
}
}

13
StockDal/packages.config Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dapper" version="2.0.78" targetFramework="net472" />
<package id="HtmlAgilityPack" version="1.11.30" targetFramework="net472" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
<package id="Selenium.WebDriver" version="3.141.0" targetFramework="net472" />
<package id="Selenium.WebDriverBackedSelenium" version="3.141.0" targetFramework="net472" />
<package id="System.Configuration.ConfigurationManager" version="5.0.0" targetFramework="net472" />
<package id="System.Data.SqlClient" version="4.8.2" targetFramework="net472" />
<package id="System.Security.AccessControl" version="5.0.0" targetFramework="net472" />
<package id="System.Security.Permissions" version="5.0.0" targetFramework="net472" />
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net472" />
</packages>