예제 #1
0
        private static async Task <bool> RunAsync(CommandLineArgs args)
        {
            using (var scraper = new EndomondoScraper(args.UserToken))
            {
                // Scrape user id from endoConfig.
                var userId = await scraper.GetUserIdAsync();

                if (!userId.HasValue)
                {
                    Console.Error.WriteLine("Cannot get Endomondo user id. Is your USER_TOKEN still valid? Try again with a new one.");
                    return(false);
                }

                Console.WriteLine($"Found account id: {userId}");

                int offset = 0;
                while (true)
                {
                    // Scrape history from API.
                    Console.Error.WriteLine($"Processing workouts from offset={offset}");
                    var history = await scraper.GetHistoryAsync(userId.Value, offset, 100);

                    offset += 100;
                    if (history?.Data == null || !history.Data.Any())
                    {
                        Console.Error.WriteLine("No more workouts found.");
                        return(true);
                    }

                    Console.WriteLine($"Found {history.Data.Count} workouts.");

                    // Slowly, synchronously scrape workouts. Not doing any parallel work as seems to trigger 429 responses.
                    foreach (var item in history.Data)
                    {
                        foreach (var format in args.GetFormats())
                        {
                            var fileName = $"{item.Id}.{format}";
                            Console.Write($" -> saving {fileName} ({item.LocalStartTime}) -> ");

                            if (File.Exists(fileName))
                            {
                                Console.WriteLine("already exists.");
                                continue;
                            }

                            using (var data = await scraper.GetWorkout(userId.Value, item.Id, format))
                                using (var file = File.OpenWrite(fileName))
                                {
                                    await data.Stream.CopyToAsync(file);

                                    Console.WriteLine(data.Length.Bytes());
                                }
                        }
                    }
                }
            }
        }
예제 #2
0
        public static int Main(string[] args)
        {
            // Parse command line args.
            var commandLineArgs = new CommandLineArgs();

            if (!CommandLine.Parser.Default.ParseArguments(args, commandLineArgs))
            {
                return(1);
            }

            using (var scraper = new EndomondoScraper(commandLineArgs.UserToken))
            {
                // Scrape user id from endoConfig.
                var userId = scraper.GetUserId();
                if (!userId.HasValue)
                {
                    Console.Error.WriteLine("Cannot get Endomondo user id. Is your USER_TOKEN still valid? Try again with a new one.");
                    return(1);
                }

                Console.WriteLine($"Found account id: {userId}");

                // Scrape history from API.
                var history = scraper.GetHistory(userId.Value);
                if (history?.Data == null || !history.Data.Any())
                {
                    Console.Error.WriteLine("No workout history found.");
                    return(1);
                }

                Console.WriteLine($"Found {history.Data.Count} workouts.");

                var format = commandLineArgs.TcxFileFormat ? "tcx" : "gpx";

                // Scrape workouts.
                Directory.CreateDirectory(format);
                foreach (var data in history.Data)
                {
                    var file = format + Path.DirectorySeparatorChar + data.Id + "." + format;
                    Console.Write($"Saving workout: {data.LocalStartTime} -> {file}");

                    if (File.Exists(file))
                    {
                        Console.WriteLine(" -> already exists.");
                        continue;
                    }

                    var workout = scraper.GetWorkout(userId.Value, data.Id, format);
                    File.WriteAllBytes(file, workout);
                    Console.WriteLine($" -> {workout.Length} bytes");
                }
            }

            return(0);
        }