LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2014, 10:15 PM   #1
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Rep: Reputation: Disabled
Node.js - writing hex digest to file


I've written a bot to retrieve all of a user's images. I noticed today that for some reason I'm receiving blank image files and I can't seem to understand why. I've provided a scenario that contains image files that it receives within the array pics. What could be causing this?

Code:
#!/usr/bin/env node

var request = require('request');
var crypto = require('crypto');
var fs = require('fs');

var md5 = function (pic) {
  var hash = crypto.createHash('md5');
  hash.update(pic);
  return hash.digest('hex');
};

var pics = ['http://37.media.tumblr.com/eea272630e7c0749241b1594b1b911b4/tumblr_n1t63oOzF81rt268so1_1280.jpg',
           'http://38.media.tumblr.com/9b40422b9129745292b174ffd683e8f3/tumblr_mvhhcuOQn41rt268so1_1280.jpg',    
           'http://37.media.tumblr.com/7f96f676610efa9beb2f322c4f6eeb36/tumblr_mudrsmtKtl1r4lu5ao1_1280.jpg'
];

for (var i = 0; i < pics.length; i++) {
  var hexDigest = md5(pics[i]) + pics[i].slice(-4);
  request(pics[i]).pipe(fs.createWriteStream(process.cwd() + '/' + hexDigest));
}

Last edited by Lucien Lachance; 07-17-2014 at 10:17 PM.
 
Old 01-12-2015, 11:22 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I recommend making better use of error/status reporting provided by 'request' (https://github.com/request/request). Try something like:

Code:
var fs = require('fs'),
    request = require('request'),
    uri = 'http://cartoonsimages.com/sites/default/files/field/image/' +
    'hello-kitty-para-el-facebook-15781.jpg',
    outFile = 'kitty.jpg';

request
  .get(uri)
  .on('response', function(res) {
    console.log('Response: HTTP ' + res.statusCode);
  })
  .on('error', function(err) {
    console.log('Error: ' + err);
  })
  .pipe(fs.createWriteStream(outFile));
You should be receiving HTTP 200 response codes, and no errors logged to the console. If that's not the case, needs investigation..
 
  


Reply

Tags
javascript, md5, node



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passable nouveau kernel driver bug (MMIO read of [hex l] FAULT at [hex l]) marbangens Linux - General 1 05-24-2013 01:35 AM
Convert a file of binery values to hex file voda87 Programming 1 12-18-2012 08:05 AM
How to transfer the services from node 1 to node 2 ,if node 1 is directly turned off sankarg304 Linux - Server 1 12-12-2012 10:06 AM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration