示例#1
0
文件: Trawl.cs 项目: t0rus1/AzShw
        /// <summary>
        /// Download Inhalt.txt from the BSB.de site.
        /// Inserts the last daysBack entries of that file into the InhaltIndex (TradesIndex) collection
        /// Note: The file Inhalt.txt is not saved
        /// </summary>
        /// <param name="daysBack"></param>
        /// <returns>Stop|Go and a ReplenishDate string</returns>
        internal static async Task <Guidance <string> > TrawlBsbIndexIntoTradesIndex(string[] bsbCredentialsPair, int daysBack)
        {
            Console.WriteLine("TrawlBsbShareData: Downloading Index file:");

            Guidance <string> retGuidance = new Guidance <string>(StopGo.Stop, "");

            using (WebClient webClient = new WebClient())
            {
                webClient.Credentials = new NetworkCredential(bsbCredentialsPair[0], bsbCredentialsPair[1]);

                try
                {
                    //need to get index file like so  http://www.bsb-software.de/rese/Inhalt.txt
                    var indexUriString = ConfigurationManager.AppSettings["tradesUrl"] + ConfigurationManager.AppSettings["tradesIndex"];
                    Console.WriteLine($"Downloading {indexUriString} ...");
                    if (Uri.TryCreate(indexUriString, UriKind.Absolute, out Uri indexUri))
                    {
                        // ......................... Getting Inhalt.TXT
                        string indexText = await webClient.DownloadStringTaskAsync(indexUri); // Task-based asynchronous version

                        // .........................

                        string[] indexLines = indexText.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                        Console.WriteLine($"Index contains {indexLines.Length} lines.");

                        // ..........................Stuffing new Inhalt.TXT entries into TradesIndex contianer
                        retGuidance.Payload = await InhaltOperations.ImportNewInhaltEntries(docClient, DATABASE_ID, tradesIndexCollectionId, indexLines, daysBack);

                        //...........................

                        if (retGuidance.Status.Equals(StopGo.Go) && retGuidance.Payload.Length > 0)
                        {
                            retGuidance.Status = StopGo.Go;
                            //Console.WriteLine($"Will import share trade files, starting with {retGuidance.Payload}...");
                        }
                        else
                        {
                            retGuidance.Status = StopGo.Stop;
                            //Console.WriteLine("Nothing further to do");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Error. Could not form Inhalt Uri from {indexUriString}");
                    }
                }
                catch (System.Exception e)
                {
                    Console.WriteLine("Exception: {0}", e.Message);
                }
            }

            return(retGuidance); // includes stop|go plus importation start date if applicable
        }
示例#2
0
        //2017_01_02.TXT 22164028 02.01.2017 22:30:38
        //2017_01_03.TXT 25772714 03.01.2017 22:30:50
        //2017_01_04.TXT 24926780 04.01.2017 22:30:50

        /// <summary>
        /// Creates and inserts documents (each created from a single Inhalt.TXT line) into the TradesIndex document collection
        /// </summary>
        /// <param name="client"></param>
        /// <param name="databaseName"></param>
        /// <param name="collectionName"></param>
        /// <param name="inhalts"></param>
        /// <param name="daysBack"></param>
        /// <returns></returns>
        public static async Task <string> ImportNewInhaltEntries(DocumentClient client, string databaseName, string collectionName, string[] inhalts, int daysBack)
        {
            string replenishDate = ""; // date at which we must re-import trades
            int    creations     = 0;
            int    updates       = 0;
            int    skips         = 0;
            int    trawlIndex    = 0;
            // daysBack allows us to calculate index from which to start the the trawl
            // if daysBack is -1, interpret as meaning start from beginning
            // NOTE daysBack=0 gives you just the (single) last entry
            //      daysBack=1 gives last two entries
            int trawlStartIndex = daysBack > -1 ? inhalts.Length - 1 - daysBack : 0;

            Console.WriteLine($"Importing each Inhalt line into InhaltDays collection...");

            foreach (string inhaltLine in inhalts)
            {
                if (trawlIndex >= trawlStartIndex)
                {
                    var    inhaltAry = inhaltLine.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    Inhalt inh       = new Inhalt
                    {
                        Id          = inhaltAry[0],
                        FileSize    = Convert.ToInt32(inhaltAry[1]),
                        DateSegment = inhaltAry[2],
                        TimeSegment = inhaltAry[3],
                    };

                    //insert (if not there) or update (if filesize has changed) or skip if same entry is already present
                    var result = await InhaltOperations.CreateInhaltDocumentIfNotExists(client, databaseName, collectionName, inh);

                    switch (result)
                    {
                    case CreatedUpdated.Created:
                        creations++;
                        if (replenishDate == "")
                        {
                            replenishDate = inh.DateSegment;                          // nab the earliest occurrence
                        }
                        break;

                    case CreatedUpdated.Updated:
                        updates++;
                        if (replenishDate == "")
                        {
                            replenishDate = inh.DateSegment;
                        }
                        break;

                    default:
                        skips++;
                        break;
                    }
                }
                trawlIndex++;
            }

            ConsoleAndPrompt(false, $"{creations} inhalt entries inserted, {updates} updated, {skips} skipped. Trawl started at line {trawlStartIndex}.");

            return(replenishDate);
        }