From b633066f08de3a2129a6cf60608fc3c07f7ddf1c Mon Sep 17 00:00:00 2001 From: Jonas Van der Aa Date: Fri, 16 Aug 2019 16:36:34 +0200 Subject: [PATCH] v1 Getting there, but the basic stuff works now. --- Commands/Pull.cs | 105 ++++++++++++++++++++++----- Configuration/Configuration.cs | 59 ++++++++++++--- Transmission.PushbulletImport.csproj | 8 +- 3 files changed, 143 insertions(+), 29 deletions(-) diff --git a/Commands/Pull.cs b/Commands/Pull.cs index 3697ec2..dc27bf6 100644 --- a/Commands/Pull.cs +++ b/Commands/Pull.cs @@ -1,8 +1,11 @@ using System; using System.Linq; +using System.Net; using CommandLine; using PushbulletSharp.Filters; +using PushbulletSharp.Models.Requests; using PushbulletSharp.Models.Responses; +using Transmission.API.RPC.Entity; using CFG = Transmission.PushbulletImport.Configuration; namespace Transmission.PushbulletImport.Commands @@ -20,43 +23,107 @@ public static int Execute(PullOptions options) { var pbClient = CFG.Configuration.Default.PBClient; - var pushFilter = new PushResponseFilter() + var prFilter = new PushResponseFilter() { + Active = true, + IncludeTypes = new []{PushResponseType.Link, PushResponseType.Note}, ModifiedDate = options.Since, - IncludeTypes = new[] { PushResponseType.File, PushResponseType.Link} }; - - var pushes = pbClient.GetPushes(pushFilter).Pushes; - pushes.ForEach(ProcessPush); + var pushes = pbClient.GetPushes(prFilter).Pushes + .Where(p => p.TargetDeviceIden != null + && p.TargetDeviceIden.Equals(CFG.Configuration.Default.PBServerDevice.Iden)).ToArray(); + foreach (var push in pushes) + { + switch (push.Type) + { + case PushResponseType.Note: + DetectNoteContent(push.Body); + continue; + case PushResponseType.Link when push.Url.StartsWith("magnet:"): + ProcessMagnetLink(push); + continue; + case PushResponseType.Link when push.Url.EndsWith(".torrent"): + ProcessTorrentUrl(push.Url); + continue; + default: + // this shouldn't happen, but it's dirty to not have it + continue; + } + } + return Program.ExitNormal; } - private static void ProcessPush(PushResponse push) + private static void DetectNoteContent(string noteBody) { - switch (push.Type) + if (noteBody.StartsWith("magnet:")) { - case PushResponseType.File: - ProcessTorrentFile(push); - return; - case PushResponseType.Link: - ProcessMagnetLink(push); - return; - default: - // we shouldn't even be here, because of the push filter, so just ignore this - return; + ProcessMagnetLink(noteBody); } } + private static void ProcessTorrentUrl(string torrentUrl) + { + var webClient = new WebClient(); + var fileContents = webClient.DownloadData(torrentUrl); + AddBase64Torrent(Convert.ToBase64String(fileContents)); + } + + private static void ProcessMagnetLink(string magnetUrl) + { + var torrent = new NewTorrent + { + Filename = magnetUrl + }; + + AddTorrent(torrent); + } + private static void ProcessMagnetLink(PushResponse push) { - throw new NotImplementedException(); + var magnetUrl = push.Url; + if (string.IsNullOrEmpty(magnetUrl) || magnetUrl.StartsWith("magnet:") == false) + { + // we don't do those links + return; + } + + ProcessMagnetLink(magnetUrl); + } + + private static void AddBase64Torrent(string base64Torrent) + { + var torrent = new NewTorrent() + { + Metainfo = base64Torrent + }; + + AddTorrent(torrent); } - private static void ProcessTorrentFile(PushResponse push) + private static void AddTorrent(NewTorrent torrent) { - throw new NotImplementedException(); + var client = CFG.Configuration.Default.TransmissionClient; + + var torrentInfo = client.TorrentAdd(torrent); + + ReportBack($"Torrent {torrentInfo.Name} was added to Transmission."); + } + + private static void ReportBack(string message) + { + var client = CFG.Configuration.Default.PBClient; + + var pushNote = new PushNoteRequest() + { + DeviceIden = CFG.Configuration.Default.PBTargetDeviceId, + Body = message, + Title = "Torrent added successfully" + }; + + client.PushNote(pushNote); } } } \ No newline at end of file diff --git a/Configuration/Configuration.cs b/Configuration/Configuration.cs index 7325a2b..b805da7 100644 --- a/Configuration/Configuration.cs +++ b/Configuration/Configuration.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.Linq; using Microsoft.Extensions.Configuration; using PushbulletSharp; +using PushbulletSharp.Models.Requests; using PushbulletSharp.Models.Responses; namespace Transmission.PushbulletImport.Configuration @@ -31,9 +33,11 @@ public PushbulletClient PBClient } } public string PBTargetDeviceId { get; private set; } + public Device PBServerDevice { get; private set; } + private const string PBDeviceName = "Transmisson Server"; - private const string ApiEnvironmentKey = EnvironmentConfigPrefix + "PBAPI"; - private const string DeviceEnvironmentKey = EnvironmentConfigPrefix + "PBDEVICE"; + private const string ApiEnvironmentKey = "PBAPI"; + private const string DeviceEnvironmentKey = "PBDEVICE"; private const string PBSectionKey = "Pushbullet"; private const string ApiKeyConfigKey = "Pushbullet API key"; @@ -59,7 +63,7 @@ private IConfigurationSection PushbulletConfigSection private const string TransmissionSectionKey = "Transmission"; private const string TransmissionHostKey = "Host"; - private const string TransmissionHostEnvironmentKey = EnvironmentConfigPrefix + "THOST"; + private const string TransmissionHostEnvironmentKey = "THOST"; private API.RPC.Client _tClient; public API.RPC.Client TransmissionClient @@ -86,7 +90,7 @@ private Configuration(string settingsFile) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile(ConfigFileName) + .AddJsonFile(ConfigFileName, optional: true) .AddEnvironmentVariables(prefix: EnvironmentConfigPrefix); ApplicationConfig = config.Build(); @@ -97,10 +101,46 @@ private void PushbulletSetup() { var apiKey = GetPBApiKey(); - _pbClient = new PushbulletClient(apiKey, TimeZoneInfo.Local); - PBTargetDeviceId = GetDeviceId(); + _pbClient = new PushbulletClient(accessToken:apiKey, TimeZoneInfo.Local); + PBTargetDeviceId = GetTargetDeviceId(); + PBServerDeviceSetup(); + } + + private void PBServerDeviceSetup() + { + PBServerDevice = DoesPBDeviceAlreadyExist(PBDeviceName) + ? GetPBDevice(PBDeviceName) + : CreatePBDevice(PBDeviceName); + } + + private bool DoesPBDeviceAlreadyExist(string nickname = null, string deviceId = null) + { + return PBClient.CurrentUsersDevices(true).Devices + .Any(d => d.Nickname.Equals(nickname) || d.Iden.Equals(deviceId)); + } + + private Device GetPBDevice(string nickname = null, string deviceId = null, bool showActiveOnly = true) + { + return PBClient.CurrentUsersDevices(showActiveOnly).Devices + .First(d => d.Nickname.Equals(nickname) || d.Iden.Equals(deviceId)); + } + + private Device CreatePBDevice(string nickname, string model = null, string manufacturer = null, int? appVersion = null) + { + var newDevice = new Device() + { + Nickname = nickname, + Model = model, + Manufacturer = manufacturer + }; + + if (appVersion.HasValue) + { + newDevice.AppVersion = appVersion.Value; + } + + return PBClient.CreateDevice(newDevice); } - private string GetPBApiKey() { @@ -118,7 +158,7 @@ private string GetPBApiKey() return apiKey; } - private string GetDeviceId() + private string GetTargetDeviceId() { var deviceId = ApplicationConfig[DeviceEnvironmentKey]; @@ -134,6 +174,7 @@ private string GetDeviceId() return deviceId; } + #endregion #region Transmission @@ -147,7 +188,7 @@ private void TorrentClientSetup() private string GetTransmissionHostname() { - var hostname = ApplicationConfig[ApiEnvironmentKey]; + var hostname = ApplicationConfig[TransmissionHostEnvironmentKey]; if (string.IsNullOrWhiteSpace(hostname)) { var configSection = ApplicationConfig.GetSection(TransmissionSectionKey); diff --git a/Transmission.PushbulletImport.csproj b/Transmission.PushbulletImport.csproj index 411ce37..1a565b8 100644 --- a/Transmission.PushbulletImport.csproj +++ b/Transmission.PushbulletImport.csproj @@ -10,8 +10,14 @@ - + + + + ..\PushbulletSharp\PushbulletSharp\bin\Release\netstandard20\PushbulletSharp.dll + + +