Skip to content

Commit

Permalink
Now reads a local csv
Browse files Browse the repository at this point in the history
  • Loading branch information
outerwinnie committed Sep 4, 2024
1 parent bd98cf8 commit 47fcc8f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 54 deletions.
60 changes: 17 additions & 43 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Discord.Rest;
using CsvHelper;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;

namespace DiscordBotExample
{
Expand All @@ -21,8 +17,7 @@ class Program
private static Random _random = new Random();
private static DiscordSocketClient _client;
private static ulong _channelId;
private static string _fileId;
private static string _credentialsPath;
private static string _csvFilePath;
private static TimeSpan _postTimeSpain;
private static TimeZoneInfo _spainTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
private static bool _isImageUrlsLoaded = false; // Flag to track if image URLs are loaded
Expand All @@ -32,12 +27,11 @@ static async Task Main(string[] args)
// Read environment variables
var token = Environment.GetEnvironmentVariable("DISCORD_BOT_TOKEN");
var channelIdStr = Environment.GetEnvironmentVariable("DISCORD_CHANNEL_ID");
_fileId = Environment.GetEnvironmentVariable("GOOGLE_DRIVE_FILE_ID");
_credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_CREDENTIALS_PATH");
_csvFilePath = Environment.GetEnvironmentVariable("CSV_FILE_PATH");
var postTimeStr = Environment.GetEnvironmentVariable("POST_TIME");

// Check if token, channelId, fileId, credentialsPath, or postTime is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr))
// Check if token, channelId, csvFilePath, or postTime is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_csvFilePath) || string.IsNullOrEmpty(postTimeStr))
{
Console.WriteLine("Environment variables are not set correctly.");
return;
Expand Down Expand Up @@ -81,8 +75,8 @@ private static async Task OnReady()
{
Console.WriteLine("Bot is connected.");

// Download and process the CSV file from Google Drive
var csvData = await DownloadCsvFromGoogleDrive();
// Read and process the CSV file from the local filesystem
var csvData = await ReadLocalCsvFile();

if (csvData != null)
{
Expand All @@ -105,7 +99,7 @@ private static async Task OnReady()
}
else
{
Console.WriteLine("Failed to download or read the CSV file. Exiting...");
Console.WriteLine("Failed to read the CSV file. Exiting...");
return;
}

Expand Down Expand Up @@ -170,7 +164,6 @@ private static async Task ScheduleNextPost()
var nowUtc = DateTime.UtcNow;
var spainTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, _spainTimeZone);

// Specify that nextPostTimeSpain is unspecified in terms of kind because we will convert it to a specific time zone
var nextPostTimeSpain = DateTime.SpecifyKind(DateTime.Today.Add(_postTimeSpain), DateTimeKind.Unspecified);

if (nextPostTimeSpain <= spainTime)
Expand All @@ -195,42 +188,23 @@ private static async Task ScheduleNextPost()
await ScheduleNextPost();
}

private static async Task<string> DownloadCsvFromGoogleDrive()
private static async Task<string> ReadLocalCsvFile()
{
try
{
// Set up Google Drive API service
var credential = GoogleCredential.FromFile(_credentialsPath)
.CreateScoped(DriveService.Scope.DriveReadonly);

var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "DiscordBotExample",
});

// Download the file
var request = service.Files.Get(_fileId);
var stream = new MemoryStream();
request.MediaDownloader.ProgressChanged += progress =>
{
if (progress.Status == Google.Apis.Download.DownloadStatus.Completed)
{
Console.WriteLine("Download complete.");
}
};

await request.DownloadAsync(stream);

stream.Position = 0;
using (var reader = new StreamReader(stream))
// Check if the file exists
if (!File.Exists(_csvFilePath))
{
return reader.ReadToEnd();
Console.WriteLine("CSV file not found.");
return null;
}

// Read the CSV file content
return await File.ReadAllTextAsync(_csvFilePath);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine($"An error occurred while reading the CSV file: {ex.Message}");
return null;
}
}
Expand All @@ -247,7 +221,7 @@ private static async Task PostRandomImageUrl()
}
else
{
Console.WriteLine("No URLs available.");
Console.WriteLine("Channel not found or no URLs available.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Recuerdense_Bot
build_property.ProjectDir = D:\Winnie\Documentos\recuerdate\
build_property.ProjectDir = C:\Users\Winnie\Desktop\recuerdate\
Binary file modified obj/Debug/net7.0/Recuerdense-Bot.assets.cache
Binary file not shown.
10 changes: 5 additions & 5 deletions obj/Recuerdense-Bot.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"format": 1,
"restore": {
"D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj": {}
"C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj": {}
},
"projects": {
"D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj": {
"C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"projectUniqueName": "C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj",
"projectName": "Recuerdense-Bot",
"projectPath": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"projectPath": "C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj",
"packagesPath": "C:\\Users\\Winnie\\.nuget\\packages\\",
"outputPath": "D:\\Winnie\\Documentos\\recuerdate\\obj\\",
"outputPath": "C:\\Users\\Winnie\\Desktop\\recuerdate\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Winnie\\AppData\\Roaming\\NuGet\\NuGet.Config"
Expand Down
6 changes: 3 additions & 3 deletions obj/project.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,11 +1247,11 @@
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"projectUniqueName": "C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj",
"projectName": "Recuerdense-Bot",
"projectPath": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"projectPath": "C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj",
"packagesPath": "C:\\Users\\Winnie\\.nuget\\packages\\",
"outputPath": "D:\\Winnie\\Documentos\\recuerdate\\obj\\",
"outputPath": "C:\\Users\\Winnie\\Desktop\\recuerdate\\obj\\",
"projectStyle": "PackageReference",
"configFilePaths": [
"C:\\Users\\Winnie\\AppData\\Roaming\\NuGet\\NuGet.Config"
Expand Down
4 changes: 2 additions & 2 deletions obj/project.nuget.cache
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"version": 2,
"dgSpecHash": "OqRsFgHRl7cY56bSI1IQL7iCdqXvdVoikKRr6yUyu6ZULVMINRj33//CN4Vf3HCgadJw8B+pv+bzRlgGvMU/YA==",
"dgSpecHash": "r/YKXmdLcgi6IgChQg+U/vu/9WcZ/djCzFCFdjCxMeZ7a9Qy1PlM9JBhoHn2UgA/Uu4nR7wvO5ZqARLLCVR1yQ==",
"success": true,
"projectFilePath": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"projectFilePath": "C:\\Users\\Winnie\\Desktop\\recuerdate\\Recuerdense-Bot.csproj",
"expectedPackageFiles": [
"C:\\Users\\Winnie\\.nuget\\packages\\csvhelper\\33.0.1\\csvhelper.33.0.1.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\discord.net\\3.15.3\\discord.net.3.15.3.nupkg.sha512",
Expand Down

0 comments on commit 47fcc8f

Please sign in to comment.