Exemplo n.º 1
0
        public bool Parse(string trContent)
        {
            string[] parts = trContent.Split(new string[] { "/td>" }, StringSplitOptions.RemoveEmptyEntries);
            
            // Symbol
            if (m_regexSymbol.IsMatch(parts[0]))
                m_symbol = TrimBrackets(m_regexSymbol.Matches(parts[0])[0].Value);
            else
                return false;

            // Name
            if (m_regexName.IsMatch(parts[1]))
            {
                m_name = TrimBrackets(m_regexName.Matches(parts[1])[0].Value);
                if (m_name.IndexOf("'") >= 0)
                {
                    while (m_name.IndexOf("'") >= 0)
                    {
                        m_name = m_name.Replace("'", "{##}");
                    }                    
                }
                m_name = m_name.Replace("{##}", "''");
            }
            else
                return false;

            // Last trade
            if (m_regexLastTrade.IsMatch(parts[2]))
                m_lastTrade = TrimBrackets(m_regexLastTrade.Matches(parts[2])[0].Value);
            else
                return false;
            if (m_regexLastTradeTime.IsMatch(parts[2]))
                m_lastTradeTime = TrimBrackets(m_regexLastTradeTime.Matches(parts[2])[0].Value);
            else
                return false;

            // Change
            if (m_regexChange.IsMatch(parts[3]))
            {
                MatchCollection mc = m_regexChange.Matches(parts[3]);
                m_change = mc[0].Value;
                m_changePercent = mc[1].Value;
            }
            else
                return false;

            // Volume
            if (m_regexVolume.IsMatch(parts[4]))
                m_volume = TrimBrackets(m_regexVolume.Matches(parts[4])[0].Value);
            else
                return false;

            // YahooCSV
            string ycsvUrl = string.Format("http://download.finance.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgv&e=.csv", m_symbol);
            YahooCSV ycsv = new YahooCSV();
            if (ycsv.Initialize(ycsvUrl))
            {
                m_lastTrade = ycsv.fields[1];
                m_change = ycsv.fields[4];
            }
            else
                Console.WriteLine("StockObject.Parse: Error: failed to load {0}", ycsvUrl);

            return true;
        }
Exemplo n.º 2
0
        private static void ProcessYahooCSV(ref Config config)
        {
            Console.WriteLine("ProcessYahooCSV: processing ProcessYahooCSV...");
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("ConfigYahooCSV.xml");
                XmlNodeList nodeList = xmlDoc.SelectNodes("/Config/CSVS/CSV");
                Console.WriteLine("ProcessYahooCSV: Number of nodes found {0}", nodeList.Count);
                XmlNodeList nodeListDelete = xmlDoc.SelectNodes("/Config/Delete/stype");
                List<YahooCSV> csvList = new List<YahooCSV>();
                foreach (XmlNode xn in nodeList)
                {
                    YahooCSV ycsv = new YahooCSV();
                    // Read the attributes
                    ycsv.url = xn.Attributes["url"].InnerText;
                    ycsv.symbol = xn.Attributes["symbol"].InnerText;
                    ycsv.stype = xn.Attributes["stype"].InnerText;
                    ycsv.name = xn.Attributes["name"].InnerText;
                    ycsv.fieldIndex = xn.Attributes["fieldIndex"].InnerText;
                    ycsv.extraField = xn.Attributes["extraField"].InnerText;
                    Console.Write(string.Format("ProcessYahooCSV: downloading {0}...", ycsv.url));
                    // Initialize
                    if (ycsv.Initialize(ycsv.url))
                    {
                        Console.WriteLine("DONE");
                        csvList.Add(ycsv);
                    }
                    else
                        Console.WriteLine("Error: failed to load {0}", ycsv.url);
                }
                xmlDoc = null;

                // Write data into database
                DBConnection dbConnection = null;
                try
                {
                    dbConnection = new DBConnection(config.connectionString);
                    if (dbConnection.Connect())
                    {
                        // Delete the records
                        foreach (XmlNode xn in nodeListDelete)
                        {
                            string queryDelete = string.Format("DELETE FROM {0} WHERE stype=N'{1}'", "stock_summary", xn.Attributes["stype"].InnerText);
                            int rowsDelete = dbConnection.ExecuteQuery(queryDelete);
                        }
                        // Insert all objects
                        for (int i = 0; i < nodeList.Count; i++)
                        {
                            Console.Write(string.Format("ProcessYahooCSV: saving {0}...", csvList[i].url));
                            Console.WriteLine("DONE");
                            string queryInsert = csvList[i].CreateInsertQuery("stock_summary", nodeList[i]);
                            int rowsInserted = dbConnection.ExecuteQuery(queryInsert);
                        }
                        // Close DB connection
                        dbConnection.Close();
                    }
                }
                catch (Exception ex)
                {
                    dbConnection.Close();
                    Console.WriteLine("Exception: {0}", ex.Message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
        }