Day Helpers
net8.0 Wrap in try Zip with directories
This commit is contained in:
65
Day/Helper-2023-09-06.cs
Normal file
65
Day/Helper-2023-09-06.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static class Helper20230906
|
||||
{
|
||||
|
||||
internal static void SaveJson(ILogger log, string argsZero)
|
||||
{
|
||||
string[] lines;
|
||||
int? jsonBodyLine;
|
||||
string? userAgent;
|
||||
string[] segments;
|
||||
HttpClient httpClient;
|
||||
HttpRequestMessage httpRequestMessage;
|
||||
string[] files = Directory.GetFiles(argsZero, "*.post", SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
jsonBodyLine = null;
|
||||
userAgent = null;
|
||||
lines = File.ReadAllLines(file);
|
||||
if (lines.Length < 2)
|
||||
continue;
|
||||
httpRequestMessage = new(HttpMethod.Post, lines[1]);
|
||||
log.LogInformation("reading {fileName}", Path.GetFileName(file));
|
||||
for (int i = 2; i < lines.Length; i++)
|
||||
{
|
||||
if (lines.Length < 1)
|
||||
continue;
|
||||
if (lines[i][0] == '#')
|
||||
continue;
|
||||
else if (lines[i][0] == '{')
|
||||
jsonBodyLine = i;
|
||||
else
|
||||
{
|
||||
segments = lines[i].Split(": ");
|
||||
if (segments.Length < 2)
|
||||
continue;
|
||||
if (segments[0] == "User-Agent")
|
||||
userAgent = segments[1];
|
||||
else
|
||||
httpRequestMessage.Headers.Add(segments[0], segments[1]);
|
||||
}
|
||||
}
|
||||
if (jsonBodyLine is not null)
|
||||
httpRequestMessage.Content = JsonContent.Create(lines[jsonBodyLine.Value]);
|
||||
httpClient = new(new HttpClientHandler { UseCookies = false }) { BaseAddress = new Uri(lines[0]) };
|
||||
if (userAgent is not null)
|
||||
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
|
||||
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.SendAsync(httpRequestMessage);
|
||||
httpResponseMessageTask.Wait();
|
||||
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
|
||||
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
|
||||
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
|
||||
streamTask.Wait();
|
||||
if (!streamTask.Result.CanRead)
|
||||
throw new NullReferenceException(nameof(streamTask));
|
||||
using FileStream fileStream = new($"{file}.json", FileMode.CreateNew);
|
||||
Task task = streamTask.Result.CopyToAsync(fileStream);
|
||||
task.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
31
Day/Helper-2023-10-10.cs
Normal file
31
Day/Helper-2023-10-10.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static class Helper20231010
|
||||
{
|
||||
|
||||
internal static void HgCV(ILogger<Worker> logger, string argsZero)
|
||||
{
|
||||
long ticks;
|
||||
DateTime dateTime;
|
||||
TimeSpan timeSpan;
|
||||
string[] segments;
|
||||
DirectoryInfo argsZeroDirectoryInfo = new(argsZero);
|
||||
int padLength = "64-612464-4626_2023-10-10_06;37_AM_2529222011".Length;
|
||||
ReadOnlyCollection<DirectoryInfo> directoryInfoCollection = new(argsZeroDirectoryInfo.GetDirectories().OrderBy(l => l.LastWriteTime).ToArray());
|
||||
foreach (DirectoryInfo directoryInfo in directoryInfoCollection)
|
||||
{
|
||||
segments = directoryInfo.Name.Split('_');
|
||||
if (!long.TryParse(segments[^1], out ticks))
|
||||
continue;
|
||||
dateTime = new(ticks);
|
||||
timeSpan = new(ticks);
|
||||
dateTime = new(ticks);
|
||||
logger.LogInformation("{directory.Name} at {LastWriteTime} took {TotalMinutes} minutes(s)", directoryInfo.Name.PadRight(padLength, ' '), directoryInfo.LastWriteTime, Math.Round(timeSpan.TotalMinutes, 3));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
37
Day/Helper-2023-10-16.cs
Normal file
37
Day/Helper-2023-10-16.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Globalization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static class Helper20231016
|
||||
{
|
||||
|
||||
internal static void MoveDirectory(ILogger<Worker> logger, string argsZero)
|
||||
{
|
||||
string weekOfYear;
|
||||
string checkDirectory;
|
||||
long ticks = DateTime.Now.AddHours(-84).Ticks;
|
||||
DirectoryInfo argsZeroDirectoryInfo = new(argsZero);
|
||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||
DirectoryInfo[] directoryInfoCollection = argsZeroDirectoryInfo.GetDirectories();
|
||||
string fileShareDirectory = Path.GetDirectoryName(argsZero) ?? throw new Exception();
|
||||
string archiveDirectory = Path.Combine(fileShareDirectory, "Archive");
|
||||
if (!Directory.Exists(archiveDirectory))
|
||||
throw new NotSupportedException();
|
||||
foreach (DirectoryInfo directoryInfo in directoryInfoCollection)
|
||||
{
|
||||
foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
|
||||
{
|
||||
if (directory.CreationTime.Ticks > ticks)
|
||||
continue;
|
||||
weekOfYear = calendar.GetWeekOfYear(directory.CreationTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
||||
checkDirectory = Path.Combine(archiveDirectory, directoryInfo.Name, $"{directory.CreationTime.Year}_Week_{weekOfYear}", directory.CreationTime.ToString("yyyy-MM-dd"));
|
||||
if (!Directory.Exists(checkDirectory))
|
||||
continue;
|
||||
Directory.Move(directory.FullName, Path.Combine(checkDirectory, directory.Name));
|
||||
logger.LogInformation("{directory.CreationTime} - {directory.Name}", directory.CreationTime, directory.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
21
Day/HelperDay.cs
Normal file
21
Day/HelperDay.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using File_Folder_Helper.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace File_Folder_Helper.Helpers;
|
||||
|
||||
internal static class HelperDay
|
||||
{
|
||||
|
||||
internal static void Select(AppSettings appSettings, ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
if (args[1] == "Day-Helper-2023-09-06")
|
||||
Day.Helper20230906.SaveJson(logger, args[0]);
|
||||
else if (args[1] == "Day-Helper-2023-10-10")
|
||||
Day.Helper20231010.HgCV(logger, args[0]);
|
||||
else if (args[1] == "Day-Helper-2023-10-16")
|
||||
Day.Helper20231016.MoveDirectory(logger, args[0]);
|
||||
else
|
||||
throw new Exception(appSettings.Company);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user