Page 1 of 1

C# for post-download scripts?

Posted: October 11th, 2023, 12:29 pm
by dochoss
Hey all, I've been using Sabnzbd for a long time, but I've never taken a look at writing custom download scripts. The examples I can find are in Python but I'm a .Net/C# dev. Modern versions of .Net run fine in Linux (my server runs Ubuntu 22.04), so I'm wondering if I can get Sab to run a C# file for a post-download script. I've searched around but can't seem to find any examples. Anyone know if this is possible, and if so, where I might find some guidance or examples?

Re: C# for post-download scripts?

Posted: October 11th, 2023, 1:25 pm
by safihre
I don't think anyone has done it, especially since Python is such a simple language to learn for anyone, especially those with programming experience.

From the top of my head I would suggest to either write a wrapper shell file that calls your program or just call it directly. SABnzbd will call anything you want with the parameters as described, as long as it has the excutable bit set.

Re: C# for post-download scripts?

Posted: October 11th, 2023, 2:10 pm
by dochoss
Yeah, certainly not complaining about Python, it's definitely uncomplicated. But if I already have the skills in C#, might as well try to use that if I can :) Did I miss documentation for how scripts are executed? I saw the wiki page about post-processing scripts but I don't see it saying anything about how to choose between bash scripts, python, etc. Writing a bash script to call a dotnet console app is pretty straightforward, so I might do that. But if I can get Sab to execute it directly, that would be the optimal approach.

Appreciate the reply!

Re: C# for post-download scripts?

Posted: October 11th, 2023, 3:35 pm
by safihre
We just call them (on Linux), just as you would through the command line.
With the parameters as described in the wiki.
Just set the Scripts Directory to the folder that holds your application and select it in the Queue or for your category in the config.

Re: C# for post-download scripts?

Posted: October 11th, 2023, 3:37 pm
by sander
If you can write a C# program that works like this

Code: Select all

C:\ mycsharpprogram.exe This That
with output

Code: Select all

The first input is This
The second input is that
... then I'm quite sure you can write a post processing script in C#.

Re: C# for post-download scripts?

Posted: January 23rd, 2024, 12:57 am
by M77
I have written one in VB NET, so it should be possible in C#.

The basic info you need is at - I can't post the link as new user, so you'll have to search the wiki for "Post-processing scripts"

You have to make it in C#, but in VB NET I started with a command-line template.

Here's some VB NET code. If you can't translate it yourself, just use a VB NET to C# converter and that should get you far enough to start converting it to proper C#.

Code: Select all

  Dim clArgs() As String
  clArgs = Environment.GetCommandLineArgs()
It'll get those as per above URL:
Position Description
1 The final directory of the job (full path)
2 The original name of the NZB file
3 Clean version of the job name (no path info and ".nzb" removed)
4 Indexer's report number (if supported)
5 User-defined category
6 Group that the NZB was posted
7 Status of post processing.
0 = OK
1 = Failed verification
2 = Failed unpack
3 = 1+2
-1 = Failed post processing
...

Writing to the command-line will be captured by sab and logged/emailed:

Code: Select all

Console.WriteLine("The final directory of the job (full path) " + clArgs(1))
Console.WriteLine("The original name of the NZB file " + clArgs(2))
Console.WriteLine("User-defined category " + clArgs(5))
Console.WriteLine("Status of post processing " + clArgs(7))
Console.WriteLine("SAB_COMPLETE_DIR: " + Environment.GetEnvironmentVariable("SAB_COMPLETE_DIR"))
Console.WriteLine("SAB_FINAL_NAME: " + Environment.GetEnvironmentVariable("SAB_FINAL_NAME"))
Console.WriteLine("SAB_FAIL_MSG: " + Environment.GetEnvironmentVariable("SAB_FAIL_MSG"))
Console.WriteLine("SAB_CAT: " + Environment.GetEnvironmentVariable("SAB_CAT"))
And then I do some checking like:

Code: Select all

        If clArgs(7) <> 0 Then
            Console.WriteLine("Failed download - nothing to do here")
            Environment.ExitCode = 1
            End
        End If
And if everything works right, I set

Code: Select all

Environment.ExitCode = 0