2023-04-09 18:00:28 -04:00
|
|
|
using System.Diagnostics;
|
|
|
|
using ttrss_co_client.ttrss;
|
|
|
|
using ttrss_co_client.ttrss.datastructures;
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
namespace ttrss_co_client.tasks
|
|
|
|
{
|
|
|
|
///<summary>podcast, with attachment that is podcast</summary>
|
|
|
|
public class PodcastifyAttachment : Phase1Task
|
|
|
|
{
|
|
|
|
public override string TaskName => "podcastifyAttachment";
|
|
|
|
public override async Task<WorkOrder> ActOn(Headline headline, IEnumerable<Label> labelsWRTArticle)
|
|
|
|
{
|
|
|
|
Console.WriteLine($" podcast-is-attachment: {headline.link.ToString()}");
|
|
|
|
var myGuid = Guid.NewGuid().ToString();
|
|
|
|
var workingFolder = $"{Conf.WorkingDirectory}/{myGuid}";
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
if(!Path.Exists(workingFolder))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(workingFolder);
|
|
|
|
}
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var attachmentLink = headline.attachments.Select(a => a.content_url)?.FirstOrDefault().ToString();
|
|
|
|
var extensionUpstream = attachmentLink.Substring(attachmentLink.LastIndexOf('.'));
|
|
|
|
var downloadPath = Path.Combine(workingFolder, headline.title) + extensionUpstream;
|
|
|
|
var downloader = new HttpClient();
|
|
|
|
sw.Start();
|
2023-07-17 16:22:26 -04:00
|
|
|
var dlResult = (await downloader.GetAsync(attachmentLink));
|
|
|
|
File.WriteAllBytes(downloadPath, await dlResult.Content.ReadAsByteArrayAsync());
|
2023-04-09 18:00:28 -04:00
|
|
|
sw.Stop();
|
2023-07-17 16:22:26 -04:00
|
|
|
var outputStr = $"{(dlResult.IsSuccessStatusCode ? "Success" : "fail")} in {sw.Elapsed}";
|
|
|
|
if(!dlResult.IsSuccessStatusCode)
|
|
|
|
{
|
|
|
|
outputStr += $"\n\t{dlResult.StatusCode} - {dlResult.ReasonPhrase}";
|
|
|
|
}
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
Console.WriteLine($" {attachmentLink}\n{outputStr}");
|
|
|
|
await TtrssClient.SetArticleLabel(labelsWRTArticle.First(l => l.caption?.ToLower() == this.TriggerLabel.ToLower()).id, false, headline.id);
|
|
|
|
Console.WriteLine($" {headline.title}: download trigger label removed");
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
var podcastTitle = headline.feed_title;
|
|
|
|
var titlingLabel = labelsWRTArticle.FirstOrDefault(l => l.@checked && l.caption?.ToLower().StartsWith(Conf.PodcastTitlePrefix) == true);
|
|
|
|
if(titlingLabel != null)
|
|
|
|
{
|
|
|
|
await TtrssClient.SetArticleLabel(titlingLabel.id, false, headline.id);
|
|
|
|
Console.WriteLine($" {headline.title}: podcast titling label removed");
|
|
|
|
podcastTitle = titlingLabel.caption.Substring(Conf.PodcastTitlePrefix.Length);
|
|
|
|
}
|
|
|
|
Console.WriteLine($"article {headline.id} - podcastifying; {podcastTitle}");
|
2023-07-06 22:31:53 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - podcastify attachment (dl): {outputStr}", headline.id);
|
2023-07-17 16:22:26 -04:00
|
|
|
if (!dlResult.IsSuccessStatusCode)
|
2023-04-09 18:00:28 -04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine($"{attachmentLink} downloaded.");
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
var toReturn = new WorkOrder()
|
|
|
|
{
|
2023-04-10 23:20:07 -04:00
|
|
|
articleId = headline.id,
|
2023-04-09 18:00:28 -04:00
|
|
|
Phase2TaskList = new Dictionary<int, string>(),
|
|
|
|
data = new Dictionary<string, string>(),
|
|
|
|
guid = Guid.Parse(myGuid)
|
|
|
|
};
|
|
|
|
toReturn.data["path"] = downloadPath;
|
|
|
|
toReturn.Phase2TaskList[1] = "filemovePublish";
|
2023-04-10 23:20:07 -04:00
|
|
|
toReturn.data["publish-target"] = $"{Conf.OnDoneCopy}/Podcasts/{podcastTitle}/";
|
2023-04-09 18:00:28 -04:00
|
|
|
if(extensionUpstream != ".mp3")
|
|
|
|
{
|
|
|
|
Console.WriteLine($"{headline.title} needs conversion task.");
|
|
|
|
toReturn.Phase2TaskList[0] = "convert";
|
2023-04-10 23:20:07 -04:00
|
|
|
toReturn.data["conversion-target"] = ".mp3";
|
2023-04-09 18:00:28 -04:00
|
|
|
}
|
2023-04-07 22:24:31 -04:00
|
|
|
|
2023-07-06 22:31:53 -04:00
|
|
|
toReturn.Phase2TaskList[2] = "markread";
|
|
|
|
|
|
|
|
toReturn.Phase2TaskList[3] = "chatmessage";
|
|
|
|
toReturn.data["chatmessage"] = "new podcast from ";
|
|
|
|
|
2023-04-09 18:00:28 -04:00
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine($"fatal error in podcast attachment DL for {headline.link}");
|
|
|
|
Console.Error.WriteLine($"{e.ToString()}: {e.Message}.\n{e.StackTrace}");
|
|
|
|
await TtrssClient.UpdateArticleNote($"{headline.note}\n[{DateTime.Now.ToString("o")}] - fatal error {e.ToString()}: {e.Message}.\n{e.StackTrace}", headline.id);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|