Parse the response from the info server and generate the correct file name.
[armadillo.git] / web_frontend / tv_renamer.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
8 //
9
10 goog.provide('armadillo.TVRenamer');
11
12 goog.require('goog.Disposable');
13 goog.require('goog.net.XhrIo');
14
15 /**
16 * Creates a helper to rename a file in a pretty format for TV episodes.
17 * @extends {goog.Disposable}
18 * @constructor
19 */
20 armadillo.TVRenamer = function(file) {
21 goog.base(this);
22
23 /**
24 * The file object
25 * @type {armadillo.File}
26 */
27 this.file_ = file;
28 }
29 goog.inherits(armadillo.TVRenamer, goog.Disposable);
30
31 /**
32 * @inheritDoc
33 */
34 armadillo.TVRenamer.prototype.disposeInternal = function() {
35 goog.base(this);
36 this.file_ = null;
37 };
38
39 /**
40 * Performs the information lookup and renames the file if the lookup is
41 * successful.
42 */
43 armadillo.TVRenamer.prototype.run = function() {
44 console.log('running for ' + this.file_.getName());
45 var data = this.parseName_(this.file_.getName());
46 var url = this.buildURL_(data[0], data[1], data[2]);
47 console.log('url = ' + url);
48 goog.net.XhrIo.send('/proxy?url=' + encodeURIComponent(url),
49 goog.bind(this.lookupHandler_, this));
50 };
51
52 /**
53 * Callback for when the network data is received.
54 * @param {object} response
55 * @private
56 */
57 armadillo.TVRenamer.prototype.lookupHandler_ = function(e) {
58 var response = e.target.getResponseText();
59 var tags = {};
60 goog.array.forEach(response.split('\n'), function (line) {
61 if (line.length > 0) {
62 var parts = line.split('@', 2);
63 tags[parts[0]] = parts[1];
64 }
65 });
66
67 if (tags['Show Name'] && tags['Episode Info']) {
68 var episode = tags['Episode Info'].split('^');
69 var name = tags['Show Name'] + ' - ' + episode[0] + ' - ' + episode[1];
70 console.log('final name = ' + name);
71 }
72 };
73
74 /**
75 * Parses the TV episode data out of the name.
76 * @param {string!} name The current file name.
77 * @returns {Tuple|null} Returns a tuple (show,season,episode) on success,
78 NULL on failure
79 * @private
80 */
81 armadillo.TVRenamer.prototype.parseName_ = function(name) {
82 var pattern = /^(\d+_)?(.+) S?(\d+)(x|E)(\d+)/;
83 var matches = name.match(pattern);
84 if (!matches || matches.length < 5)
85 return null;
86 return [matches[2], parseInt(matches[3]), parseInt(matches[5])];
87 };
88
89 /**
90 * Builds the query URL.
91 * @param {string!} show Show name
92 * @param {number!} season Season number
93 * @param {number!} episode Episode number
94 * @returns {string}
95 * @private
96 */
97 armadillo.TVRenamer.prototype.buildURL_ = function(show, season, episode) {
98 return "http://services.tvrage.com/tools/quickinfo.php?show=" + show +
99 "&ep=" + season + "x" + episode;
100 };