Permalink
Browse files

Merge branch 'master' of http://github.com/pfleidi/magnets

  • Loading branch information...
Felix Richter Felix Richter
Felix Richter authored and Felix Richter committed Oct 25, 2010
2 parents 9c14356 + 7afc948 commit fe93a58ebabba8543360b222a98cdb080c8ca73e
Showing with 32 additions and 54 deletions.
  1. +25 −52 lib/magnetlib.js
  2. +7 −2 magnets.js
View
@@ -12,6 +12,7 @@ Sys = require('sys'),
Url = require('url'),
Crypto = require('crypto'),
Log4js = require('log4js'),
+HttpClient = require('node-wwwdude'),
Appender = require('./colorappender');
Log4js.addAppender(Appender.consoleAppender());
@@ -30,7 +31,7 @@ log.setLevel('INFO');
*/
var Content = exports.Content = function Content(contentUrl) {
this.url = contentUrl;
- this.data = "";
+ this.data = '';
};
/**
@@ -46,31 +47,7 @@ Content.prototype.hash = function () {
*/
Content.prototype.toString = function () {
return 'url: ' + this.url +
- ' port: ' + this.urlPort() +
- ' hash: ' + this.hash();
-};
-
-/**
- * @return {URL}
- */
-Content.prototype.parsedUrl = function () {
- return Url.parse(this.url);
-};
-
-/**
- * @return {Int}
- */
-Content.prototype.urlPort = function () {
- var cUri = this.parsedUrl();
- if (cUri.port) {
- return cUri.port;
- } else {
- if (cUri.protocol === 'http:') {
- return 80;
- } else if (cUri.protocol === 'https:') {
- return 443;
- }
- }
+ ' hash: ' + this.hash();
};
var Image = exports.Image = function Image(imageUrl) {
@@ -95,36 +72,32 @@ var httpGet = exports.httpGet = function httpGet(content, callback, encoding) {
log.debug('httpGet: ' + content.url);
- var parsedUrl = content.parsedUrl(),
- connection = Http.createClient(content.urlPort(), parsedUrl.host),
- queryparms = parsedUrl.query ? '?' + parsedUrl.query : '',
- getPath = parsedUrl.pathname + queryparms,
- request = connection.request('GET', getPath, {
- "host": parsedUrl.host,
- "User-Agent": "fucking magnets"
+ var client = HttpClient.createClient({
+ logger: log,
+ headers: { 'User-Agent': 'fucking magnets' },
+ encoding: encoding
});
- request.addListener("response", function (response) {
- response.setEncoding(encoding);
-
+ client.get(content.url)
+ .addListener('error', function (data, resp) {
+ log.error('Error for: ' + content.url + ' code: ' + resp.statusCode);
+ })
+ .addListener('redirect', function (data, resp) {
+ log.info('Redirecting to: ' + resp.headers['location']);
+ })
+ .addListener('success', function (data, resp) {
var image = new Image(content.url);
-
- response.addListener("data", function (chunk) {
- image.data += chunk;
- });
-
- response.addListener("end", function () {
- callback(image);
- });
- });
- request.end();
+ image.data = data;
+ log.debug('Got image: ' + image);
+ callback(image);
+ }).send();
};
-/**
+/**
* appends arbitrary data to given file
*
* @author makefu
- * @date 2010-08-21
+ * @date 2010-08-21
* @param filename
* @return data to be written
*/
@@ -159,7 +132,7 @@ function writeFile(image, fname) {
}
var saveImage = exports.saveImage = function saveImage(image) {
- log.debug('writing file:' + image);
+ log.debug('writing file:' + image);
var fname = imageFolder + image.hash() + "." + image.fileType();
Fs.readFile(fname, function (err, data) {
@@ -184,20 +157,20 @@ exports.downloadImages = function downloadImages(imageUrls) {
});
};
-/**
+/**
* convenience function which exectues a regular expression and saves
* every match in a table
*
* @author makefu
- * @date 2010-10-11
+ * @date 2010-10-11
* @param regex the regular expression object
* @param content the string to validate the regex against
* @return an array of all matches
*/
exports.getMatches = function getMatches(regex, content) {
var matches = [];
- var match;
+ var match;
while (match = regex.exec(content)) {
if (match) {
log.debug("found: " + match[0]);
View
@@ -17,15 +17,15 @@ Appender = require('./lib/colorappender.js');
var modules = [],
PLUGIN_FOLDER = __dirname + "/plugins/",
-LOGFILE = __dirname + '/log/mediengewitter.log',
+LOGFILE = __dirname + '/log/magnets.log',
DEFAULT_TIMEOUT = 10000,
TIMEOUT = DEFAULT_TIMEOUT;
//Log4js.addAppender(Appender.consoleAppender());
Log4js.addAppender(Log4js.fileAppender(LOGFILE), 'magnets');
var log = Log4js.getLogger('magnets');
-log.setLevel('INFO');
+log.setLevel('ERROR');
/* Process Logging */
@@ -34,6 +34,11 @@ process.on('SIGINT', function () {
process.exit(0);
});
+process.on('uncaughtException', function(err) {
+ log.fatal('RUNTIME ERROR! :' + err.stack);
+});
+
+
/**
* @param {String} fileName
* @return {String}

0 comments on commit fe93a58

Please sign in to comment.