MagicMirror² v2.12.0 is available! For more information about this release, check out this topic.

folder monitor/ file creation...



  • Hi folks,

    i was just wondering if its possible to have a module that monitors a folder for new files/downloads and prints the last say 5 or 10 results on the mirror?

    many thanks,
    Richie



  • @banbutcher sure in the node_helper you woudl use the fs.watch() function to watch for files in a directory
    and it will call the routine when files are added or deleted

    i use this i one of my modules

    	// watch for a file to appear in the folder
    					fs.watch(self.config.detectionDir, (eventType, filename) => {
    						if (filename) {
                                                    }
                                             }
    

    then when the file arrives, you would sendsocketNotification up to the module.js to display it

    you could use my sample module
    https://github.com/sdetweil/SampleModule

    and use the code above instead of checking for the configured message and have your idea running a few minutes



  • @sdetweil thanks for the reply, unfortunately all that is a little bit above my head atm, any chance you’d have a link or some info on how to implement the code?

    Richie



  • @banbutcher sorry, nothing more than what I have already given…

    look up fs.watch() (its just a google search away)

    and then u have to understand how modules work, read the doc and look at my sample.

    i could clarify the code from before

    fs.watch(folder_name, 
            callback_function(eventType, filename) {
    	    if (filename) {
                }
            }
    )
    


  • @sdetweil

    i have it partly working… i have the contents displaying on my mirror but its all showing up on one line, how can i display it with each line break as in txt file?
    deluge.js:

    Module.register("deluge",{
    	defaults: {
    		updateInterval: 30 * 60 * 1000 //reads the file every 30 mins
    	},
    
    	start: function(){
    		this.sendSocketNotification("START", this.config);
    	},
    
    	socketNotificationReceived: function(notification, payload) {
    		if(notification === "DATA"){
    			this.dataFile = payload;
    			this.updateDom();
    		}
    	},
    
    	getDom: function(){
    		var wrapper = document.createElement("div");
    		if(this.dataFile){
    			wrapper.innerHTML = this.dataFile;
    		} else {
    			wrapper.innerHTML = "No data";
    		}
    		return wrapper;
    	}
    });
    

    and my node helper.js

    const NodeHelper = require("node_helper");
    const fs= require("fs");
    
    module.exports = NodeHelper.create({
    //here comes the part of the nodehelper after the 3 dots as posted above
    
    	socketNotificationReceived: function(notification, payload) {
    		if(notification === "START"){
    			this.config = payload;
    			this.readData();
        			setInterval(() => {
            			this.readData();
        			}, this.config.updateInterval);
    		}
    	},
    
    	readData: function(){
    		//to read a file to do the following
    		fs.readFile("new-tv-list.txt", "utf8", (err, data) => {
    			if (err) throw err;
    			this.sendSocketNotification("DATA", data);
    		});
    	}
    });
    

    thanks



  • @banbutcher said in folder monitor/ file creation...:

      if(this.dataFile){
      	wrapper.innerHTML = this.dataFile;
      } else {
      	wrapper.innerHTML = "No data";
      }
    

    if the content you are injecting in NOT html, then you should use innerText…
    it might recognize the line ends as such… they are not line ends in html

    if the innerText doesn’t work, then u will have to convert the line ends t html line breaks
    replace '\n\ with ‘

    note that html is NOT UTF 8



  • here is the script that im using to retreve file names…

    #!/bin/bash
    
    TARGET=/media/TheVault/UnWatched/
    PROCESSED=/media/TheVault/Torrentopia/Torrent-Files/
    
    inotifywait -m -e create -e moved_to -r --format "%f" $TARGET \
            | while read FILENAME
                    do
                            echo Detected $FILENAME, Adding to list...
                            echo $FILENAME | cat - $PROCESSED/new-tv-list.txt > temp && mv temp $PROCESSED/new-tv-list.txt
    #                        echo $FILENAME `date` >> $PROCESSED/new-tv-list.txt
    			cp $PROCESSED/new-tv-list.txt /home/pi/MagicMirror/new-tv-list.txt
    
                    done
    

    if i take out the utf8 part i get an [object array buffer] instead of my file names…



  • @banbutcher

    so i put back in the utf8 and then added <br /> after i echo the filename and now its working fine now, i need to tweek it more to suit my needs but so far so good! thanks for your help @sdetweil im sure ill be asking more silly questions again soon! 🙂



  • @banbutcher said in folder monitor/ file creation...:

    ill be asking more silly questions

    no such thing… ask away…



  • @banbutcher you can also convert the buffer to a string

    buffer_object.toString()


Log in to reply