-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataExctractor.cs
183 lines (148 loc) · 6.02 KB
/
DataExctractor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using RedditSharp.Things;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace HWSPriceBot
{
public class DataExctractor : IDataExtractor
{
ExtractedData extractedData;
public List<string> GetPrice(Post post)
{
extractedData = new ExtractedData();
extractedData.Price = new List<string>();
extractedData.Date = post.Created;
var postText = post.SelfText;
var charSearch = '$';
int j = 0;
for (int i = 0; i < postText.Length; i++)
{
string totalPrice = "";
if (postText[i] == charSearch)
{
i++;
while (Char.IsDigit(postText[i]) && i < postText.Length)
{
totalPrice += postText[i];
if (i + 1 == postText.Length)
{
break;
}
i++;
}
extractedData.Price.Add(totalPrice.Trim());
j++;
}
}
if (extractedData.Price.Count < 0)
{ extractedData.Price = new List<string>(); }
return extractedData.Price;
}
public string ParseTitle(string title)
{
if(title.Contains("[H]") || title.Contains("[h]"))
{
return title.Split(']')[2].Split('[')[0].Trim();
}
return "0";
}
public void AddToDatabase(ExtractedData data, string connectionString)
{
MergeUser(data.AuthorDto, connectionString);
if (data.TextHtml == null)
{
data.TextHtml = "";
}
MergePost(data, connectionString);
}
private void MergePost(ExtractedData data, string connectionString)
{
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("dbo.MergePost", myConnection);
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@Name", data.Author);
myCommand.Parameters.AddWithValue("@Items", data.Item);
myCommand.Parameters.AddWithValue("@Date", data.Date.ToString());
myCommand.Parameters.AddWithValue("@Price", String.Join(", ", data.Price.ToArray()));
myCommand.Parameters.AddWithValue("@Url", data.Url.ToString());
myCommand.Parameters.AddWithValue("@Text", data.Text);
myCommand.Parameters.AddWithValue("@TextHtml", data.TextHtml);
myCommand.Parameters.AddWithValue("@Flair", data.Flair);
myCommand.Parameters.AddWithValue("@Subreddit", data.Subreddit);
myCommand.Parameters.AddWithValue("@Title", data.Title);
myConnection.Open();
myCommand.ExecuteNonQuery();
}
private void MergeUser(Author data, string connectionString)
{
if (UserExists(data.Name, connectionString))
{
return;
}
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("dbo.MergeAuthor", myConnection);
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@Name", data.Name);
myCommand.Parameters.AddWithValue("@PostKarma", data.PostKarma);
myCommand.Parameters.AddWithValue("@CommentKarma", data.CommentKarma);
myCommand.Parameters.AddWithValue("@Created", data.Created);
myConnection.Open();
myCommand.ExecuteNonQuery();
}
private bool UserExists(string name, string connectionString)
{
bool valueExists = false;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("dbo.ReadAuthor", myConnection);
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@SearchTerm", name);
myConnection.Open();
var reader = myCommand.ExecuteReader();
while (reader.Read())
{
if (reader.HasRows)
{
valueExists = true;
}
}
reader.Close();
return valueExists;
}
public bool ValueExistsInDatabase(ExtractedData data, string connectionString)
{
SqlConnection myConnection = new SqlConnection(connectionString);
bool valueExists = false;
SqlCommand myCommand = new SqlCommand("dbo.CheckPost", myConnection);
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@Name", data.Author);
myCommand.Parameters.AddWithValue("@Items", data.Item);
myCommand.Parameters.AddWithValue("@Date", data.Date.ToString());
myConnection.Open();
var reader = myCommand.ExecuteReader();
while (reader.Read())
{
if (reader.HasRows)
{
valueExists = true;
}
}
reader.Close();
return valueExists;
}
public void WriteToFile(Post post, string items, List<string> price)
{
string combindedPrice = string.Join(",", price.ToArray());
string directory = Environment.CurrentDirectory + "WriteLines.txt";
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(directory, true))
{
file.WriteLine("");
file.WriteLine("Flair: " + post.LinkFlairText);
file.WriteLine("Name: " + post.AuthorName);
file.WriteLine("Date: " + post.Created);
file.WriteLine("Items: " + items);
file.WriteLine("Prices: " + combindedPrice);
}
}
}
}