11
Ro
rotator / files
rotator / rotator.js
4.7 KB
1/* 2 * Author: Sean Caetano Martin 3 * Description: script that reads all the files in a directory and 4 * sets them as a random wallpaper. If you intend to use this, please 5 * make sure you only have IMAGE files in the specified directory. 6 * once the script is started you can set the walpaper to the rotator.png 7 * that was just created in the pictures folder 8 * 9 * USAGE: node rotator.js ['/path/to/pictures_folder'] [interval=30] 10 * 11 * The path is relative to the root (/). 12 * If you don't supply a folder, it will look for: 13 * cwd + '/Pictures' (default) 14 * If you don't supply an interval it will default to 30 min 15 */ 16 17// dependencies 18var fs = require('fs'), 19 http = require('http'), 20 xml2js = require('xml2js'), 21 url = require('url'), 22 exec = require('child_process').exec; 23 24// locals 25var args = process.argv, folder, interval, web; 26 27// parse command line args 28if (args.length > 2) 29 folder = args[2]; 30else // default folder 31 folder = process.cwd() +'/Pictures'; 32 33if (args.length > 3 && args[3] !== 0) 34 interval = args[3]; 35else // default interval 36 interval = 30; 37 38function rotate () { 39 // locals 40 var file = '', index = 0; 41 42 // update the file directory 43 fs.readdir(folder, function (err, files) { 44 // validate the folder throw errors 45 if (err || files.length === 0) { 46 if (files.length === 0) { 47 console.log('Bad folder, no files. Next try in '+interval+'min(s)'); 48 return ; 49 } 50 else 51 throw err; 52 } 53 54 do { // any picture but rotator.png or 55 // generate random number to choose next wallpaper 56 index = Math.floor(Math.random() * files.length) 57 } while (files[index] === 'rotator.png' || files[index] === file); 58 59 // save the file so that we don't repeat it 60 file = files[index]; 61 62 // declare link function 63 function link () { 64 fs.link(folder +'/'+files[index], folder +'/rotator.png') 65 exec('touch '+ folder +'/rotator.png'); // needed to force gnome to refresh 66 67 console.log('Linked new file '+files[index]+' '+new Date().toString()); 68 } 69 70 // remove existing link (if there is one) 71 fs.lstat(folder +'/rotator.png', function (err, stats) { 72 if (stats && stats.isFile()) 73 fs.unlink(folder +'/rotator.png', link); 74 else 75 link(); 76 }); 77 }); 78} 79 80// web version (i hope) 81// uses www.simpledesktops.com rss feed 82// it should be too hard to adapt it to other feeds 83function rss_rotate () { 84 // locals 85 var parser = {}, xml = '',items = [], item = {}, 86 title = '', link = '', index = 0; 87 88 // GET request wrapper 89 // params: host path callback(body) 90 function get (host, path, callback) { 91 var client = {}, req = {}, body = ''; 92 93 client = http.createClient(80, host); 94 req = client.request(path, { 'host': host }); 95 req.end(); 96 97 req.on('response', function (res) { 98 res.on('data', function (chunk) { 99 body += chunk; 100 }); 101 res.on('end', function () { 102 callback(body) 103 }); 104 }); 105 } 106 107 function get_write (host, path, dir) { 108 var client = {}, req = {}, body = '', stream = {}; 109 110 stream = fs.createWriteStream(dir, { 'flags': 'w' }); 111 stream.on('open', function (fd) { 112 113 client = http.createClient(80, host); 114 req = client.request(path, { 'host': host }); 115 req.end(); 116 117 req.on('response', function (res) { 118 res.on('data', function (chunk) { 119 stream.write(chunk); 120 }); 121 }); 122 }); 123 } 124 125 // fetch the feed 126 get('feeds.feedburner.com', '/simpledesktops', function (xml) { 127 // parse the xml 128 parser = new xml2js.Parser(); 129 parser.addListener('end', function (xml_js) { 130 // save it 131 items = xml_js.channel.item; 132 index = Math.floor(Math.random() * items.length) 133 item = xml_js.channel.item[index]; 134 135 // set it if its new 136 if (item.title !== title) { 137 title = item.title; 138 139 // this would be easier if the 140 // rss feed linked to the static file 141 link = item.description.split(' ')[2]; 142 link = link.substring(5, link.indexOf('.png') + 4); 143 link = url.parse(link); 144 145 // save the file to the disk 146 get_write(link.hostname, link.pathname, folder +'/rotator.png'); 147 console.log('Linked '+ link.hostname + link.pathname +' '+ new Date().toString()); 148 } 149 }); 150 parser.parseString(xml); 151 }); 152} 153 154// self executing recursive function 155(function action () { 156 try { 157 rss_rotate(); 158 } 159 catch (err) { 160 console.log('error: '+ err +'\nfalling back to local images'); 161 rotate(); 162 } 163 164 setTimeout(action, interval * 60 * 1000); 165}) ();