Skip to content

Instantly share code, notes, and snippets.

@MiffOttah
Created February 10, 2014 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiffOttah/23e08f8a666f70ffbf58 to your computer and use it in GitHub Desktop.
Save MiffOttah/23e08f8a666f70ffbf58 to your computer and use it in GitHub Desktop.
internal static bool DoFile(string path, string prodcode, out string status, out bool skip)
{
// see if it's in there already
var checkQuery = new SqlCommand("SELECT COUNT(*) FROM Episodes WHERE ProductionCode = @PC", Program.DB);
checkQuery.Parameters.AddWithValue("PC", prodcode);
int checkCount = Convert.ToInt32(checkQuery.ExecuteScalar());
if (checkCount > 0)
{
status = "Already in DB.";
skip = true;
return true;
}
skip = false;
// start reading
var transcript = new StringBuilder();
bool reading = false;
using (StreamReader r = new StreamReader(path))
{
while (!r.EndOfStream)
{
string line = r.ReadLine().Trim();
if (line.Length == 0) continue;
if (line[0] == '=' || line[0] == '%' || line[0] == '-')
{
continue;
}
if (reading)
{
if (line.ToLowerInvariant() == "> contributors") break;
if (line.Contains(":"))
{
transcript.Append('\0');
}
else
{
transcript.Append(' ');
}
transcript.Append(line);
}
else
{
if (line[0] != '>') continue;
line = line.TrimStart('>', ' ');
if (line.ToLowerInvariant().StartsWith("quotes and scene summary"))
{
reading = true;
}
}
}
}
if (!reading)
{
status = "Cannot find QaSS. Nonstandard capsule.";
return false;
}
var transcriptPhase2 = new StringBuilder();
int linecount = 0;
foreach (string line in transcript.ToString().Split('\0'))
{
bool inBrackets = false;
bool characterName = true;
foreach (char c in line)
{
if (characterName)
{
if (c == ':') characterName = false;
}
else if (inBrackets)
{
if (c == ']') inBrackets = false;
}
else if (c == '[')
{
inBrackets = true;
}
else if (c != '{' && c != '}')
{
transcriptPhase2.Append(c);
}
}
transcriptPhase2.Append("\r\n");
linecount++;
}
if (linecount > 25)
{
// got the transcript, load it in!
var addQuery = new SqlCommand("INSERT INTO Episodes (ProductionCode, Transcript) VALUES (@PC, @T)", Program.DB);
addQuery.Parameters.AddWithValue("PC", prodcode);
addQuery.Parameters.AddWithValue("T", transcriptPhase2.ToString());
addQuery.ExecuteNonQuery();
status = string.Format("OK ({0} lines)", linecount);
return true;
}
else
{
status = string.Format("Not enough lines ({0})", linecount);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment