-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
207 lines (184 loc) · 6.58 KB
/
Copy pathProgram.cs
File metadata and controls
207 lines (184 loc) · 6.58 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Linq;
using HtmlAgilityPack;
using System.Net.Mail;
using System.Net;
namespace MLB_Roster_Alerts
{
class Program
{
static void Main(string[] args)
{
// Validate arguments
if (AreArgsValid(args) == false)
{
// ERROR: an argument is invalid or missing
return;
}
// Arguments are valid - parse them
string team = args[0];
string teamFormatted = FormatTeam(team);
string toAddressString = args[1];
string fromAddressString = args[2];
string fromPasswordString = @args[3];
string yesterdayDate = DateTime.Now.AddDays(-1).ToString("MM/dd/yy"); ;
// Load roster move webpage
string url = "https://www.mlb.com/" + team + "/roster/transactions/2020/09/";
var web = new HtmlWeb();
var doc = web.Load(url);
// Scrape roster move dates and descriptions from 'roster_table' on the webpage
var scrapedElements = doc.DocumentNode.SelectNodes("//table[@class='roster__table']/tbody/tr/td");
List<string> datesList = new List<string>();
List<string> tradesList = new List<string>();
// Populate dates in datesList and roster moves in tradesList
// datesList[0] is the date for the roster move in tradesList[0]
int iteration = 0;
foreach (var x in scrapedElements)
{
iteration++;
if (iteration % 2 != 0) // Odd - element is date
{
datesList.Add(x.InnerText);
}
else // Even - element is roster move
{
tradesList.Add(x.InnerText);
}
}
// Find roster moves from yesterday, and add them to body of email
// Time zones may be an issue here?
string emailBody = "";
int tradeQuantity = tradesList.Count;
for (int x = 0; x < tradeQuantity; x++)
{
if (datesList[x] == yesterdayDate)
{
emailBody += tradesList[x] + "\n\n";
}
}
if (emailBody == "")
{
emailBody = "No roster moves on " + yesterdayDate;
}
// Bring together email details that have already been inputted
MailAddress toAddress = new MailAddress(toAddressString);
MailAddress fromAddress = new MailAddress(fromAddressString, teamFormatted + " Roster Alerts");
string fromPassword = fromPasswordString;
string subject = teamFormatted + " roster moves from " + yesterdayDate;
// Establish connection to SMTP server
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
// Send email
using MailMessage message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = emailBody
};
smtp.Send(message);
// Write to log file
using (System.IO.StreamWriter log = new System.IO.StreamWriter(@"log.txt", true))
{
log.Write(DateTime.Now + "\n");
log.Write(subject + "\n");
log.Write(emailBody + "\n\n");
}
}
static bool AreArgsValid(string[] args)
{
string[] validTeams =
{
"angels", "astros", "athletics", "bluejays", "braves", "brewers",
"cardinals", "cubs", "diamondbacks", "dodgers", "giants", "indians",
"mariners", "marlins", "mets", "mlb", "nationals", "orioles",
"padres", "phillies", "pirates", "rangers", "rays", "redsox", "reds",
"rockies", "royals", "tigers", "twins", "whitesox", "yankees"
};
if (args[0] == null)
{
// Error: no team
return false;
}
else if (validTeams.Contains(args[0]) == false)
{
// Error: invalid team
}
if (args[1] != null)
{
if (IsValidEmail(args[1]) == false)
{
// Error: invalid TO address
return false;
}
}
else
{
// Error: missing TO address
return false;
}
if (args[2] != null)
{
if (IsValidEmail(args[2]) == false)
{
// Error: invalid FROM address
return false;
}
}
else
{
// Error: missing FROM address
return false;
}
if (args[3] == null)
{
// Error: missing FROM password
return false;
}
return true;
}
// Thanks to https://stackoverflow.com/questions/498400/ for this method
static bool IsValidEmail(string email)
{
try
{
var addr = new MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
// Convert team name with all lowercase and no spaces to proper team name
static string FormatTeam(string team)
{
if (team == "redsox")
{
return "Red Sox";
}
else if (team == "whitesox")
{
return "White Sox";
}
else if (team == "bluejays")
{
return "Blue Jays";
}
else if (team == "mlb")
{
return "MLB";
}
else
{
return (char.ToUpper(team[0]) + team.Substring(1));
}
}
}
}