2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
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.
10 goog
.provide('armadillo.TVRenamer');
12 goog
.require('goog.Disposable');
13 goog
.require('goog.net.XhrIo');
16 * Creates a helper to rename a file in a pretty format for TV episodes.
17 * @extends {goog.Disposable}
20 armadillo
.TVRenamer
= function(file
) {
25 * @type {armadillo.File}
29 goog
.inherits(armadillo
.TVRenamer
, goog
.Disposable
);
34 armadillo
.TVRenamer
.prototype.disposeInternal
= function() {
35 goog
.base(this, 'disposeInternal');
40 * Performs the information lookup and renames the file if the lookup is
43 armadillo
.TVRenamer
.prototype.run
= function() {
44 console
.log('running for ' + this.file_
.getName());
45 var data
= this.parseName_(this.file_
.getName());
47 app
.showError('Could not parse episode information for ' + this.file_
.getName());
50 var url
= this.buildURL_(data
[0], data
[1], data
[2]);
51 console
.log('url = ' + url
);
52 goog
.net
.XhrIo
.send('/proxy?url=' + encodeURIComponent(url
),
53 goog
.bind(this.lookupHandler_
, this));
57 * Callback for when the network data is received.
61 armadillo
.TVRenamer
.prototype.lookupHandler_
= function(e
) {
62 var response
= e
.target
.getResponseText();
64 goog
.array
.forEach(response
.split('\n'), function (line
) {
65 if (line
.length
> 0) {
66 var parts
= line
.split('@', 2);
67 tags
[parts
[0]] = parts
[1];
71 if (tags
['Show Name'] && tags
['Episode Info']) {
72 var episode
= tags
['Episode Info'].split('^');
73 // Strip off leading zeros from the season number.
74 while (episode
[0].charAt(0) == '0') {
75 episode
[0] = episode
[0].substr(1);
77 var name
= tags
['Show Name'] + ' - ' + episode
[0] + ' - ' + episode
[1];
83 * Parses the TV episode data out of the name.
84 * @param {string!} name The current file name.
85 * @returns {Tuple|null} Returns a tuple (show,season,episode) on success,
89 armadillo
.TVRenamer
.prototype.parseName_
= function(name
) {
90 var pattern
= /^(\d
+_
)?(.+) S
?(\d
+)(x
|E
)(\d
+)/i
;
91 var matches
= name
.match(pattern
);
92 if (!matches
|| matches
.length
< 5)
94 return [matches
[2], parseInt(matches
[3], 10), parseInt(matches
[5], 10)];
98 * Builds the query URL.
99 * @param {string!} show Show name
100 * @param {number!} season Season number
101 * @param {number!} episode Episode number
105 armadillo
.TVRenamer
.prototype.buildURL_
= function(show
, season
, episode
) {
106 return "http://services.tvrage.com/tools/quickinfo.php?show=" +
107 encodeURIComponent(show
) + "&ep=" + season
+ "x" + episode
;
111 * Performs the actual rename of the current file to the |newName|.
112 * @param {string!} newName
115 armadillo
.TVRenamer
.prototype.rename_
= function(newName
) {
116 var path
= app
.joinPath(this.file_
.getParentPath(), newName
);
117 this.file_
.move(path
);