Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
Getting there, but the basic stuff works now.
  • Loading branch information
kekonn committed Aug 16, 2019
1 parent aefcd40 commit b633066
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 29 deletions.
105 changes: 86 additions & 19 deletions Commands/Pull.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
}
59 changes: 50 additions & 9 deletions Configuration/Configuration.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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()
{
Expand All @@ -118,7 +158,7 @@ private string GetPBApiKey()
return apiKey;
}

private string GetDeviceId()
private string GetTargetDeviceId()
{
var deviceId = ApplicationConfig[DeviceEnvironmentKey];

Expand All @@ -134,6 +174,7 @@ private string GetDeviceId()

return deviceId;
}

#endregion

#region Transmission
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion Transmission.PushbulletImport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="PushBulletSharp" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Transmission.API.RPC" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="PushbulletSharp, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\PushbulletSharp\PushbulletSharp\bin\Release\netstandard20\PushbulletSharp.dll</HintPath>
</Reference>
</ItemGroup>

</Project>

0 comments on commit b633066

Please sign in to comment.