RS TV Show Tracker

Extensibility

The software was designed to be extensible since day 1, however external assemblies were not searched and loaded until November 2011. This version is able to load any .NET assembly or Python script, and use their classes.

Supported languages

Any .NET language

It's really easy to write a plugin using a .NET language:

Before starting, take a look at the already implemented classes in the repository. You can download a C# file from there, and only modify the names and XPaths, since those are already internal plugins.

IronPython 2.7+

Python scripts are officially supported as a way to implement plugins without having to compile the code to a .NET assembly.

It's easy to subclass a .NET type using IronPython. Here's a working line-by-line port of TvTorrentsRo.cs:

import clr

clr.AddReference("System")
clr.AddReferenceToFile("RSTVShowTracker.exe")

from System import *
from System.Collections.Generic import *
from System.Text.RegularExpressions import *
from RoliSoft.TVShowTracker import *
from RoliSoft.TVShowTracker.Parsers.Downloads import *

class TvTorrentsPy(DownloadSearchEngine):
    def get_Name(self):
        return "Tv Torrents Ro w/ IPy"

    def get_Site(self):
        return "http://freshon.tv/"

    def get_Icon(self):
        return "http://static.freshon.tv/favicon.ico"

    def get_Developer(self):
        return "RoliSoft"

    def get_Version(self):
        return Utils.DateTimeToVersion("2011-11-06")

    def get_Private(self):
        return True

    def get_RequiredCookies(self):
        return Array[str](["uid", "pass"])

    def get_CanLogin(self):
        return True

    def get_LoginURL(self):
        return self.get_Site() + "login.php?action=makelogin"

    def get_LoginFields(self):
        return Dictionary[str, object](dictionary = {"username": LoginFieldTypes.UserName, "password": LoginFieldTypes.Password})

    def get_Type(self):
        return Types.Torrent

    def Login(self, username, password):
        return self.GazelleTrackerLogin(username, password)

    def Search(self, query):
        html = Utils.GetHTML(self.Site + "browse.php?search=" + Uri.EscapeUriString(query), cookies = self.Cookies)

        if self.GazelleTrackerLoginRequired(html.DocumentNode):
            raise Security.Authentication.AuthenticationException()

        links = html.DocumentNode.SelectNodes("//table/tr/td/div[1]/a")

        if links is None:
            return

        for node in links:
            link = Link(self)

            link.Release = node.GetAttributeValue("title", None)
            link.InfoURL = self.Site.rstrip("/") + node.GetAttributeValue("href", None)
            link.FileURL = self.Site + "download.php?id=" + \
                           Regex.Replace(node.GetAttributeValue("href", None), "[^0-9]+", str.Empty) + "&type=torrent"
            link.Size    = node.GetHtmlValue("../../../td[@class='table_size']").strip().replace("<br>", " ")
            link.Quality = FileNames.Parser.ParseQuality(link.Release)
            link.Infos   = Link.SeedLeechFormat.FormatWith(node.GetTextValue("../../../td[@class='table_seeders']").Trim(), \
                           node.GetTextValue("../../../td[@class='table_leechers']").Trim())

            if node.GetHtmlValue("../..//img[@alt='50% Free']") is not None:
                link.Infos += ", 50% Free"

            if node.GetHtmlValue("../..//img[@alt='100% Free']") is not None:
                link.Infos += ", 100% Free"

            yield link

        return

To run it, save the above code as TvTorrentsPy.Plugin.py and place it near the application. The name of the file has to be exactly the name of the class you want to load plus .Plugin.py. This means you can only extend 1 class/file.

If something isn't working out for you, you can contact me via email or by using the "favicon Send feedback" menu option from within the software.

Adding a custom interpreter

This is possible by subclassing the ScriptingPlugin class. The newly derived class will register a new file extension, for example .js, and when the software finds a file named like anything.Plugin.js it'll call the LoadScript(file) method of your class. This method will return a list of exposed types in the form of ExternalType objects. When the software is ready to use your plugin, it'll call the CreateInstance(exposedType) method.

For more informations on how to do this, read source of the base type, ScriptingPlugin.cs, and the source of a working implementation, IronPython.cs.

Quick tips:

public override T CreateInstance<T>(ExternalType type)
{
    dynamic obj = IronWhatNotEngine.CreateDynamicInstance(type);
    // 'obj' is now a dynamic object, on which if you call a method it'll forward it to the interpreter
    // but we need a native CLR object reference for the software, so we'll need to generate a proxy class
    // which subclasses from our 'T' type and forwards any calls to 'obj'
    // this is *EXACTLY* how you do it in impromptu with 'obj':
    return obj.ActLike<T>();
}

Injecting code at startup

It is possible to load a plugin when the software starts. This lets you to do a variety of things, such as registering background tasks or modify the UI. To do this, all you have to do is to subclass the StartupPlugin class. These plugins will be called in RoliSoft.TVShowTracker.MainWindow.WindowLoaded.

An example code in Python, which manipulates the tray icon at startup by changing its icon to favicon and appending to its title:

import clr

clr.AddReference("System")
clr.AddReference("System.Drawing")
clr.AddReference("PresentationFramework")
clr.AddReferenceToFile("RSTVShowTracker.exe")

from System import *
from System.Drawing import Icon, Bitmap
from System.Windows import Application
from RoliSoft.TVShowTracker import *

class StartupTest(StartupPlugin):
    def get_Name(self):
        return "Startup Test"

    def get_Developer(self):
        return "RoliSoft"

    def get_Version(self):
        return Utils.DateTimeToVersion("2011-12-30")

    def Run(self):
        icon = Application.GetResourceStream(Uri("pack://application:,,,/RSTVShowTracker;component/Images/disc.png")).Stream
        MainWindow.NotifyIcon.Text += " - now with 140% more awesomeness!"
        MainWindow.NotifyIcon.Icon  = Icon.FromHandle(Bitmap(icon).GetHicon())

Experimental languages

The following languages are not officially supported as a way to write plugins, however I've played around with them in my free time and published my findings here. More are coming soon.

Java or any JVM language

There are multiple ways to do .Net/Java interoperability.

IKVM.NET

This is probably the easiest way to do it and the most mature project to do it with. According to Mono-Project.com, IKVM.NET can run the whole Eclipse IDE under Mono.1

<configuration>
   <startup>
      <supportedRuntime version="v4.0.30319"/>
   </startup>
</configuration>
ikvmstub -r:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationFramework.dll \
         -r:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationCore.dll \
         -namespace:RoliSoft.TVShowTracker \
         "<em>FULL_PATH_TO_YOUR_INSTALLATION</em>\RSTVShowTracker.exe"
ikvmc <em>anything</em>.Plugin.jar -r:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationFramework.dll \
                          -r:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationCore.dll \
                          -r:"<em>FULL_PATH_TO_YOUR_INSTALLATION</em>\RSTVShowTracker.exe" \
                          -r:"<em>FULL_PATH_TO_YOUR_INSTALLATION</em>\*.dll"
JNI Bridging

There are various solutions that offer interoperability between the two VMs by using an automatically generated internal webservice or something smarter. Most of these solutions are commercial, but you'll be able to find free and open-source solutions as well. Please avoid using commercial solutions if you want to publish your plugin: first off, their license may not permit the redistribution of their libraries in open-source softwares, and secondly, if you abandon the plugin it will be a lot harder or legally impossible for others to maintain your plugin.

An interesting project I found while researching this method was jni4net. Unfortunately I was not able to successfully generate a proxy using the provided proxygen.exe tool. Maybe if it listed errors, I could've done something to fix them, but the generator terminates without printing any error messages or codes. On the mailing lists, the developer claims that the 0.8.x branch will never be able to generate proxies for complicated classes.2 While the project itself looks promising, it's still in an early stage of development.

Another interesting project is Espresso, which in turn is an extended version of Caffeine. However, I didn't play around with these since the original project is from 2004 and the updated one is from 2006. The most likely outcome of my experimentation would be that they're incompatible with .Net 4.0 and/or JRE 7.

Direct *.Plugin.jar support

In the "Adding a custom interpreter" section I've talked about a theoretical way to dynamically invoke methods from other languages. It would be possible to use one of the first two methods and automate proxy generation and bridging in a ScriptingPlugin class.

Phalanger 2.1+

The software can be extended using files compiled by phpc.exe in pure mode. The resulting library file will expose all the exported classes and the software will be able to cast it back to ParserEngine and its derived classes.

<?
import namespace System;
import namespace System:::Collections:::Generic;
import namespace RoliSoft:::TVShowTracker;
import namespace RoliSoft:::TVShowTracker:::Parsers:::Downloads;

namespace RoliSoft:::TVShowTracker:::Parsers:::Downloads:::Engines:::Experimental
{
    public class TestEngine extends DownloadSearchEngine
    {
        function get_Name()
        {
            return "Test Engine";
        }

        function get_Site()
        {
            return "http://lab.rolisoft.net/";
        }

        function get_Type()
        {
            return Types::Torrent;
        }

        function get_Private()
        {
            return true;
        }

        function get_RequiredCookies()
        {
            return array("uid", "pass");
        }

        function get_CanLogin()
        {
            return true;
        }

        function get_LoginURL()
        {
            return get_Site() + "login.php";
        }

        function get_LoginFields()
        {
            return array(
                "username" => LoginFieldTypes::UserName,
                "password" => LoginFieldTypes::Password
            );
        }

        function Login($username, $password)
        {
            return GazelleTrackerLogin($username, $password);
        }

        function Search($query)
        {
            $list = new i'List'<:Link:>;

            // TODO

            return $list;
        }
    }
}
?>

To compile this PHP file into a .NET library, you'll have to issue the following command:

phpc /pure /target:dll /r:RSTVShowTracker.exe /out:TestEngine.Plugin.dll TestEngine.php

The compiled assembly's name doesn't have to match its classes name and can contain multiple exported classes. They will all load, once you place the compiled library near the application and restart it.

To redistribute your plugin, you'll have to include at least four additional assemblies:

If you want to use native extensions, you'll also have to include:

They are all located in your Phalanger installation's Bin and Wrappers directory. (C:\Program Files (x86)\Phalanger 2.1\Bin)

PerlNET (Perl Dev Kit 9.1+)

Although it is possible to compile Perl scripts, it has some drawbacks:

Regardless, if you want to do it, here's a sample Perl script to get you started:

package RoliSoft::TVShowTracker::Parsers::Downloads::Engines::Experimental;

use strict;
use PerlNET qw(with AUTOCALL);

use namespace "System";
use namespace "System.Collections.Generic";
use namespace "RoliSoft.TVShowTracker";
use namespace "RoliSoft.TVShowTracker.Parsers.Downloads";

# define the exported interface

=for interface
    [extends: DownloadSearchEngine]
    string get_Name();
    string get_Site();
    string get_Icon();
    bool get_Private();
    string[] get_RequiredCookies();
    bool get_CanLogin();
    string get_LoginURL();
    Types get_Type();
    any Search(string query);
    string Login(string username, string password);
=cut

# and now start implementing the functions above

sub get_Name {
    return "TestEngine";
}

# ...

sub Login {
    my($this, $self, $username, $password) = @_;
    return $this->GazelleTrackerLogin($username, $password);
}

When you're done, to compile this Perl script, you'll have to issue the following command:

plc --reference RSTVShowTracker.exe --norunlib --target library --out TestEngine.Plugin.dll TestEngine.pl

You should know, that when I was playing around with PerlNET, it didn't support .NET 4, so I had to recompile the software for .NET 3.5, which meant cutting out some crucial parts of the software. If you find a way to get around this please send me an email with the instructions, so I can update this document.

To redistribute your plugin, you'll have to include two additional assemblies: