Home > Uncategorized > Using the @OpenSubtitles API in C#

Using the @OpenSubtitles API in C#

Open Subtitles is a great online resource for finding subtitles for movies online, and you can also interact with it programmatically using their XML-RPC API – but it’s quite complex, so it’s best to start with a pre-built library.

I went for this library on SourceForge – https://sourceforge.net/projects/opensubdotnet/

it had a demo console app that works out of the box, but you should register your own user agent with OpenSubtitles, so they know who you are.

I needed a two step process for my app – which you can download on iTunes here:

https://itunes.apple.com/gb/app/open-subtitles/id522825951?mt=8

The first was to make a search for movies by name; which you can do using the opensubdotnet library below;

 

 

OSDotNetSession session = OSDotNetSession.LogIn(“”, “”, “en”, “**YOUR USER AGENT HERE**”);

List<SearchSubtitleResult> List = session.SearchByQuery(Request.QueryString[“film”]);

 

Then, once you get that list of films, with language variations, you can ask the user to select one, you can get the download link at this stage, however, if you need to do any server processing of the subtitles, then you can’t just download this and unzip it, since OpenSubtitles will block your server for not being a human (a captcha)

But, I get the IMDB ID and Subtitle File at this stage, and pass it through to the next step

OSDotNetSession session = OSDotNetSession.LogIn(“”, “”, “en”, “**YOUR USER AGENT HERE**”);
List<SearchSubtitleResult> List = session.SearchByImdbId(strImdb);
SearchSubtitleResult selected = List.First(f => f.IDSubtitleFile == strSubtitleID);
MemoryStream mem = session.DownloadSubtitle(selected);
string strLang = selected.ISO639.ToLower();
Encoding enc = Encoding.GetEncoding(“iso-8859-1″);
if (strLang==”he”) enc = Encoding.GetEncoding(“iso-8859-8″);
if (strLang==”el”) enc = Encoding.GetEncoding(“iso-8859-7″);
if (strLang==”ar”) enc = Encoding.GetEncoding(“iso-8859-6”);
StreamReader sr = new StreamReader(mem,enc);
string strText = sr.ReadToEnd();

 

Note that you need to be careful with text encodings here. The text is not necessarily going to be in the latin (i.e. english) alphabet. So I have made exceptions here for Hebrew (he), Greek (el) and Arabic (ar). This should be extended for Chinese, Korean, Japanese, Russian, Thai, Hindu, Georgian etc., but if anyone wants to complete that list, please comment below.

 

 

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: