Added download method to the media data instances...
This commit is contained in:
parent
fb364f154d
commit
c2cfe54bee
|
@ -175,7 +175,7 @@ namespace DML.Application.Classes
|
||||||
if (!string.IsNullOrEmpty(Settings.LoginToken))
|
if (!string.IsNullOrEmpty(Settings.LoginToken))
|
||||||
{
|
{
|
||||||
Logger.Debug("Trying to login with last known token...");
|
Logger.Debug("Trying to login with last known token...");
|
||||||
loggedIn= await DMLClient.Login(Settings.LoginToken);
|
loggedIn = await DMLClient.Login(Settings.LoginToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!loggedIn)
|
if (!loggedIn)
|
||||||
|
@ -230,7 +230,7 @@ namespace DML.Application.Classes
|
||||||
splash.Close();
|
splash.Close();
|
||||||
|
|
||||||
Logger.Info("Starting scheduler...");
|
Logger.Info("Starting scheduler...");
|
||||||
Scheduler.Start();
|
Scheduler.ScanAll();
|
||||||
|
|
||||||
System.Windows.Forms.Application.Run(new MainForm());
|
System.Windows.Forms.Application.Run(new MainForm());
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,8 @@ namespace DML.AppCore.Classes
|
||||||
ChannelId = r.Channel.Id,
|
ChannelId = r.Channel.Id,
|
||||||
DownloadSource = a.Url,
|
DownloadSource = a.Url,
|
||||||
Filename = a.Filename,
|
Filename = a.Filename,
|
||||||
TimeStamp = SweetUtils.DateTimeToUnixTimeStamp(r.CreatedAt.UtcDateTime)
|
TimeStamp = SweetUtils.DateTimeToUnixTimeStamp(r.CreatedAt.UtcDateTime),
|
||||||
|
FileSize = a.Size
|
||||||
};
|
};
|
||||||
mediaData.Store();
|
mediaData.Store();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,11 @@ using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using DML.Application.Classes;
|
using DML.AppCore.Classes;
|
||||||
using SweetLib.Utils;
|
using SweetLib.Utils;
|
||||||
using SweetLib.Utils.Logger;
|
using SweetLib.Utils.Logger;
|
||||||
|
|
||||||
namespace DML.AppCore.Classes
|
namespace DML.Application.Classes
|
||||||
{
|
{
|
||||||
public class JobScheduler
|
public class JobScheduler
|
||||||
{
|
{
|
||||||
|
@ -82,7 +82,7 @@ namespace DML.AppCore.Classes
|
||||||
Run = false;
|
Run = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartAll()
|
public void ScanAll()
|
||||||
{
|
{
|
||||||
Logger.Info("Started JobScheduler...");
|
Logger.Info("Started JobScheduler...");
|
||||||
|
|
||||||
|
@ -106,136 +106,5 @@ namespace DML.AppCore.Classes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WorkQueue(int jobId)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Logger.Debug("Beginning attachment download...");
|
|
||||||
Logger.Trace("Finding job...");
|
|
||||||
var job = (from j in JobList where j.Id == jobId select j).FirstOrDefault();
|
|
||||||
|
|
||||||
if (job == null)
|
|
||||||
{
|
|
||||||
Logger.Warn($"Associating job not found! JobId: {jobId}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Logger.Trace("Found job.");
|
|
||||||
|
|
||||||
Queue<IMessage> queue;
|
|
||||||
Logger.Trace("Locking scheduler...");
|
|
||||||
lock (this)
|
|
||||||
{
|
|
||||||
Logger.Trace("Finiding queue...");
|
|
||||||
if (!RunningJobs.TryGetValue(jobId, out queue))
|
|
||||||
{
|
|
||||||
Logger.Warn($"Queue for job {jobId} not found!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Logger.Trace("Queue found.");
|
|
||||||
}
|
|
||||||
Logger.Trace("Unlocked scheduler.");
|
|
||||||
|
|
||||||
Logger.Debug($"Messages to process for job {jobId}: {queue.Count}");
|
|
||||||
while (queue.Count > 0)
|
|
||||||
{
|
|
||||||
Logger.Trace("Locking scheduler...");
|
|
||||||
lock (this)
|
|
||||||
{
|
|
||||||
Logger.Trace("Checking if still a job...");
|
|
||||||
if (!RunningJobs.ContainsKey(jobId))
|
|
||||||
{
|
|
||||||
Logger.Warn($"Queue for job {jobId} not found!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Logger.Trace("Continue working...");
|
|
||||||
}
|
|
||||||
Logger.Trace("Unlocked scheduler.");
|
|
||||||
|
|
||||||
Logger.Trace("Dequeueing message...");
|
|
||||||
var message = queue.Dequeue();
|
|
||||||
|
|
||||||
Logger.Debug($"Attachments for message {message.Id}: {message.Attachments.Count}");
|
|
||||||
foreach (var a in message.Attachments)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme);
|
|
||||||
|
|
||||||
Logger.Trace("Replacing filename placeholders...");
|
|
||||||
|
|
||||||
var extensionRequired = !fileName.EndsWith("%name%");
|
|
||||||
|
|
||||||
var serverName = "unknown";
|
|
||||||
|
|
||||||
var socketTextChannel = message.Channel as SocketTextChannel;
|
|
||||||
if (socketTextChannel != null)
|
|
||||||
{
|
|
||||||
serverName = socketTextChannel.Guild.Name;
|
|
||||||
serverName = Path.GetInvalidFileNameChars().Aggregate(serverName, (current, c) => current.Replace(c, ' '));
|
|
||||||
}
|
|
||||||
|
|
||||||
var channelName = message.Channel.Name;
|
|
||||||
channelName = Path.GetInvalidFileNameChars().Aggregate(channelName, (current, c) => current.Replace(c, ' '));
|
|
||||||
|
|
||||||
fileName =
|
|
||||||
fileName.Replace("%guild%", serverName)
|
|
||||||
.Replace("%channel%", channelName)
|
|
||||||
.Replace("%timestamp%", SweetUtils.DateTimeToUnixTimeStamp(message.CreatedAt.UtcDateTime).ToString())
|
|
||||||
.Replace("%name%", a.Filename)
|
|
||||||
.Replace("%id%", a.Id.ToString());
|
|
||||||
|
|
||||||
if (extensionRequired)
|
|
||||||
fileName += Path.GetExtension(a.Filename);
|
|
||||||
|
|
||||||
Logger.Trace($"Detemined file name: {fileName}.");
|
|
||||||
|
|
||||||
|
|
||||||
if (File.Exists(fileName) && new FileInfo(fileName).Length == a.Size)
|
|
||||||
{
|
|
||||||
Logger.Debug($"{fileName} already existing with its estimated size. Skipping...");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.Trace("Determining directory...");
|
|
||||||
var fileDirectory = Path.GetDirectoryName(fileName);
|
|
||||||
|
|
||||||
if (!Directory.Exists(fileDirectory))
|
|
||||||
{
|
|
||||||
Logger.Info($"Directory {fileDirectory} does not exist. Creating directory...");
|
|
||||||
Directory.CreateDirectory(fileDirectory);
|
|
||||||
Logger.Debug("Created directory.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var wc = new WebClient();
|
|
||||||
Logger.Debug($"Starting downloading of attachment {a.Id}...");
|
|
||||||
|
|
||||||
wc.DownloadFile(new Uri(a.Url), fileName);
|
|
||||||
Logger.Debug($"Downloaded attachment {a.Id}.");
|
|
||||||
|
|
||||||
Logger.Trace("Updating known timestamp for job...");
|
|
||||||
job.KnownTimestamp = SweetUtils.DateTimeToUnixTimeStamp(message.CreatedAt.UtcDateTime);
|
|
||||||
job.Store();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
AttachmentsDownloaded++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Logger.Trace("Locking scheduler...");
|
|
||||||
lock (this)
|
|
||||||
{
|
|
||||||
Logger.Trace($"Removing {jobId} from running jobs...");
|
|
||||||
RunningJobs.Remove(jobId);
|
|
||||||
Logger.Trace("Decreasing thread count...");
|
|
||||||
RunningThreads--;
|
|
||||||
}
|
|
||||||
Logger.Trace("Unlocked scheduler.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DML.AppCore.Classes;
|
using DML.AppCore.Classes;
|
||||||
|
using DML.Client;
|
||||||
|
using SweetLib.Utils;
|
||||||
|
using SweetLib.Utils.Logger;
|
||||||
using static SweetLib.Utils.Logger.Logger;
|
using static SweetLib.Utils.Logger.Logger;
|
||||||
|
|
||||||
namespace DML.Application.Classes
|
namespace DML.Application.Classes
|
||||||
|
@ -13,9 +18,10 @@ namespace DML.Application.Classes
|
||||||
public ulong Id { get; set; }
|
public ulong Id { get; set; }
|
||||||
public string DownloadSource { get; set; }
|
public string DownloadSource { get; set; }
|
||||||
public ulong GuildId { get; set; }
|
public ulong GuildId { get; set; }
|
||||||
public ulong ChannelId { get; set;}
|
public ulong ChannelId { get; set; }
|
||||||
public double TimeStamp { get; set; }
|
public double TimeStamp { get; set; }
|
||||||
public string Filename { get; set; }
|
public string Filename { get; set; }
|
||||||
|
public int FileSize { get; set; }
|
||||||
|
|
||||||
internal void Store()
|
internal void Store()
|
||||||
{
|
{
|
||||||
|
@ -44,5 +50,50 @@ namespace DML.Application.Classes
|
||||||
Trace("Deleting value...");
|
Trace("Deleting value...");
|
||||||
db.Delete(Id);
|
db.Delete(Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal async Task Download()
|
||||||
|
{
|
||||||
|
Trace("Beginning attachment download...");
|
||||||
|
|
||||||
|
Debug("Building filename...");
|
||||||
|
var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme);
|
||||||
|
|
||||||
|
Debug($"Base filename: {fileName}");
|
||||||
|
|
||||||
|
Trace("Determining if extension is required");
|
||||||
|
var extensionRequired = !fileName.EndsWith("%name%");
|
||||||
|
Trace($"Extension required: {extensionRequired}");
|
||||||
|
|
||||||
|
Trace("Replacing filename placeholders...");
|
||||||
|
var guildName = DMLClient.Client.GetGuild(GuildId)?.Name ?? GuildId.ToString();
|
||||||
|
var channelName = DMLClient.Client.GetGuild(GuildId)?.GetChannel(ChannelId)?.Name ?? ChannelId.ToString();
|
||||||
|
|
||||||
|
fileName =
|
||||||
|
fileName.Replace("%guild%", guildName)
|
||||||
|
.Replace("%channel%", channelName)
|
||||||
|
.Replace("%timestamp%", TimeStamp.ToString("####"))
|
||||||
|
.Replace("%name%", Filename)
|
||||||
|
.Replace("%id%", Id.ToString());
|
||||||
|
|
||||||
|
Trace("Adding extension if required");
|
||||||
|
if (extensionRequired)
|
||||||
|
fileName += Path.GetExtension(Filename);
|
||||||
|
|
||||||
|
Debug($"Final filename: {fileName}");
|
||||||
|
|
||||||
|
if (File.Exists(fileName) && new FileInfo(fileName).Length == FileSize)
|
||||||
|
{
|
||||||
|
Logger.Debug($"{fileName} already existing with its estimated size. Skipping...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var wc = new WebClient();
|
||||||
|
Logger.Debug($"Starting downloading of attachment {Id}...");
|
||||||
|
wc.DownloadFile(new Uri(DownloadSource), fileName);
|
||||||
|
Logger.Debug($"Downloaded attachment {Id}.");
|
||||||
|
|
||||||
|
|
||||||
|
Core.Scheduler.AttachmentsDownloaded++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue