Automated the google forms to pdf generation process. Using pdflatex command (external program).
Only the download automation has to be done. > look at FileSystemWatcher for automatically detecting downloads
This commit is contained in:
76
Proostenkrant.NET/GoogleFormsReader.cs
Normal file
76
Proostenkrant.NET/GoogleFormsReader.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
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);
|
||||
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[] LoadImages(string[][] data)
|
||||
{
|
||||
var names = new List<string>();
|
||||
|
||||
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('\"');
|
||||
File.Copy(path, filename);
|
||||
}
|
||||
return names.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user