using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Printing; using System.Globalization; using System.IO; using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; namespace Proostenkrant.NET { internal static class GoogleFormsReader { const string tsvDataUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRfxfdT8f4MQjXWmZylnXCGl14T0x5mxNw3W1Xd8GrZfPivjcqRGIvmb2YoAf77M00PrGPSJ6Zo-hRs/pub?output=tsv"; const string downloadURL = "https://drive.google.com/uc?export=download&id=FILEID"; public static string[][] GetData() { var tsvData = GetTsvData(); var data = ConvertData(tsvData); data = FilterByTimestamp(data); var imageNames = LoadImages(data); return data.Select((v, i) => { v[5] = imageNames[i]; return v; }).ToArray(); } static string GetTsvData() { var client = new HttpClient(); var downloadTsvTask = client.GetStringAsync(tsvDataUrl); downloadTsvTask.Wait(); return downloadTsvTask.Result; } static string[][] ConvertData(string tsvData) { // Split rows var tsvRows = tsvData.Split('\r').Where(v => v.Length > 0); // Split values var rows = tsvRows.Select(r => r.Split('\t').Where(v => v.Length > 0)); // Remove header rows rows = rows.ToList().GetRange(2, rows.Count() - 2); return rows.Select(r => r.ToArray()).ToArray(); } static string[][] FilterByTimestamp(string[][] data) { Console.WriteLine("Filter by starting date"); var startDate = DateTime.Parse(Console.ReadLine(), CultureInfo.CreateSpecificCulture("nl-BE")); return data.Where(row => { var timeStamp = DateTime.Parse(row[0].Trim('\n'), CultureInfo.CreateSpecificCulture("nl-BE")); return startDate < timeStamp; }).ToArray(); } static string[] LoadImages(string[][] data) { var names = new List(); foreach (var row in data) { var name = Regex.Replace(row[2], string.Concat(Path.GetInvalidFileNameChars()), string.Empty, RegexOptions.Compiled); name = name.Replace(":", ""); names.Add(name); var filename = Program.c_articleImagesPath + $"/{name}.jpg"; if (File.Exists(filename)) continue; var id = row[5].Replace("https://drive.google.com/open?id=", string.Empty); var url = downloadURL.Replace("FILEID", id); Console.WriteLine("Downloading images using the browser..."); var process = Process.Start(url); process.WaitForExit(); Console.Write("Drag image here : "); var path = Console.ReadLine() .Trim('\"') .Replace("file:///", "") .Replace("%20", " "); File.Copy(path, filename); } return names.ToArray(); } } }