Пример #1
0
        private static async Task <int> MainAsync(string[] args)
        {
            // System.Console.TreatControlCAsInput = true;
            System.Console.CancelKeyPress += (sender, e) =>
            {
                e.Cancel = true;
                System.Console.WriteLine("CANCELLATION REQUEST!");
                CancellationTokenSource.Cancel();
            };

            var serviceCollection = new ServiceCollection();

            ConfigureServices(serviceCollection);
            var config = ConfigurationHelper.Configuration.Get <ApplicationOptions>();

            var isLocalDiskOnly = args?.Contains("/p") ?? false;

            SearchOptions photoSearchOptions = new SearchOptions
            {
                // See for List of Available Machine Tags
                //https://api-attractions.navigatorglass.com/swagger/#!/MachineKey/ApiMachineKeyPredicatesGet

                //MachineTags = "machine_tags => nycparks:",
                //MachineTags = "machine_tags => nychalloffame:",
                //MachineTags = "machine_tags => nycparks:m010=",
                MachineTags = "machine_tags => nycparks:m089=",
                //MachineTags = "machine_tags => nycparks:q436=",
                // MachineTags = "machine_tags => nycparks:m010=114",
                // UserId = "",
                //PhotoSize = "q",   //150x150
                //PhotoSize = "z",   // Medium 640
                PhotoSize = "q", //width="1024" height="768
            };

            TokenResponse token = null;

            if (!isLocalDiskOnly && !CancellationTokenSource.IsCancellationRequested)
            {
                try
                {
                    Metric.Start("token");
                    token = await TokenProvider.RequestResourceOwnerPasswordAsync(
                        config.ImagegalleryApiConfiguration.Login, config.ImagegalleryApiConfiguration.Password,
                        config.ImagegalleryApiConfiguration.Api);

                    if (token == null)
                    {
                        Log.Error("Token Request Failed. The app will close now.");
                        System.Console.ReadKey();
                        return(await Task.FromResult(1));
                    }

                    token.Show();
                }
                finally
                {
                    Metric.StopAndWriteConsole("token");
                }
            }

            try
            {
                Metric.Start("Flickr Search and Post");
                // start processing
                // waitForPostComplete is true by default, waiting when image has finished upload
                //  if we don't need to wait (e.g. no afterward actions are needed) we can set it to false to speed up even more
                PerformGetAndPost(CancellationTokenSource.Token, token, photoSearchOptions, 10, 10, false).ConfigureAwait(false).GetAwaiter()
                .OnCompleted(async() =>
                {
                    System.Console.WriteLine($"Flickr Total API requests: {ImageSearchService.FlickrQueriesCount}");
                    System.Console.WriteLine($"Flickr Total Bytes: {ImageSearchService.FlickrQueriesBytes}");
                    System.Console.WriteLine($"Total Photos Valid: XXXX");
                    System.Console.WriteLine($"Total Photos Flaged: XXXX");
                    for (int i = 0; i < 5; i++)
                    {
                        System.Console.WriteLine($"Dummy Photo Flaged PhotoId:{0}", i);
                    }

                    //if (!isLocalDiskOnly && !CSource.IsCancellationRequested)
                    //{
                    //    try
                    //    {
                    //        Metric.Start("get");
                    //        await GetImageGalleryApi(CSource.Token, token, imageGalleryApi);
                    //    }
                    //    finally
                    //    {
                    //        Metric.StopAndWriteConsole("get");
                    //    }
                    //}
                });
            }
            finally
            {
                Metric.StopAndWriteConsole("NEW flickrimg");
            }

            System.Console.ReadLine();

            return(0);
        }