KARPACH

WEB DEVELOPER BLOG

AutoHotkey DVDFab script for volume label conversion

From time to time I use free DVDFab HD Decrypter to rip rented movies, so I can watch them later using my Popcorn Hour A210 media streamer. DVDFab is kind enough to pull a volume label from a DVD disk. However, this label has all uppercase letters and words are concatenated by underscores. For example:

THE_LONG_GOODBYE

DVDFab vs AutoHotkey

I rename those labels to something like this: The Long Goodbye (1973), where a year I lookup on IMDB web site. I don’t like tedious repetitive tasks, so one day I decided to write a script for AutoHotkey to automate this process.

It is easy to replace underscores and correct letter case, but a year should be retrieved from some kind of database. Google pointed me in a direction of IMDB API.

It might be a little bit challenging to do IMDB API access using just AutoHotkey script engine. Luckily AutoHotkey scripts can load DLL libraries and with the magic of COM.ahk and CLR.ahk you can load even .NET libraries. However those magic scripts can’t properly execute .NET 3.5/4.0 libraries, so I had to use .NET 2.0. As you probably know .NET 2.0 doesn’t have native support for JSON deserialization, so I had to use Newtonsoft.Json for this purpose. I also could use XML output from IMDB API, but I prefer to use JSON, since a response is smaller in a size. Here is my code for .NET part of it:

public class Server
{
    public string GetMovieTitleWithYear(string volumeLablel)
    {
        var request = WebRequest.Create(new Uri(string.Format("http://www.imdbapi.com/?t={0}", 
            volumeLablel.Replace('_', '+'))));
        request.Method = "GET";
        try
        {
 
            var response = request.GetResponse();                               
            var responseStream = response.GetResponseStream();
            if (responseStream != null)
            {
                var reader = new StreamReader(responseStream);
                var responseString = reader.ReadToEnd();
                var movie = JsonConvert.DeserializeObject<Movie>(responseString);
                if (movie != null && !string.IsNullOrEmpty(movie.Title))
                {
                    return string.Format("{0} ({1})", movie.Title, movie.Year);
                }
 
            }
        }
        catch
        { 
        }            
        return UppercaseWords(volumeLablel.Replace('_', ' ').ToLower());
    }               
 
    static string UppercaseWords(string value)
    {
        char[] array = value.ToCharArray();
        // Handle the first letter in the string.
        if (array.Length >= 1)
        {
            if (char.IsLower(array[0]))
            {
                array[0] = char.ToUpper(array[0]);
            }
        }
        // Scan through the letters, checking for spaces.
        // ... Uppercase the lowercase letters following spaces.
        for (int i = 1; i < array.Length; i++)
        {
            if (array[i - 1] == ' ')
            {
                if (char.IsLower(array[i]))
                {
                    array[i] = char.ToUpper(array[i]);
                }
            }
        }
        return new string(array);
    }
}

Then using JSON 2 C# Service you get the following:

public class Movie
{ 
    public string Title { get; set; }     
    public int Year { get; set; }
    public string Rated { get; set; }
    public string Released { get; set; }
    public string Genre { get; set; }
    public string Director { get; set; }
    public string Writer { get; set; }
    public string Actors { get; set; }
    public string Plot { get; set; }
    public string Poster { get; set; }
    public string Runtime { get; set; }
    public float Rating { get; set; }
    public int Votes { get; set; }
    public string ID { get; set; }
    public string Response { get; set; }        
}

The library is ready. Let’s plug it into AutoHotkey:

#include c:\Program Files (x86)\Autohotkey\Extras\Scripts\CLR.ahk
#include c:\Program Files (x86)\Autohotkey\Extras\Scripts\COM.ahk

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return

#IfWinActive ahk_class QWidget
~Lbutton::
MouseGetPos, xpos,ypos
if (xpos>306 and ypos>474 and xpos<605 and ypos<495)
{
	ToolTip, Double click in text box below`nto get IMDB title and year,305,430
	SetTimer, RemoveToolTip, 5000
}
If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < 500) 
{
    CLR_Start()
	imdb := CLR_LoadLibrary("c:\Program Files (x86)\Autohotkey\Extras\Scripts\Karpach.IMDB.dll")
	;Type names must be fully qualified.
	server:= CLR_CreateObject(imdb,"Karpach.IMDB.Server")
	Send ^a
	Sleep 100
	clipboard =   
	Send ^c
	ClipWait
	title:=COM_Invoke(server,"GetMovieTitleWithYear",clipboard)
	COM_Release(imdb)
	COM_Release(server)
	clipboard = %title%
	Send ^v
	return  
}
return

#IfWinActive

Now, whenever you double click in volume label text box content would be converted into a proper movie title.

AutoHotkey scripts and DLLs

IMDB API .NET library source code

Update:

If you are using AutoHotkey_L then you don’t need COM.ahk and you can call GetMovieTitleWithYear method directly:

title:= server.GetMovieTitleWithYear(clipboard)	

AutoHotkey_L scripts and DLLs

Second Update:

It is better to use a new URL for IMDB api: http://www.omdbapi.com/.

Posted on December 25, 2011 by