• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

folder monitor/ file creation...

Scheduled Pinned Locked Moved Requests
10 Posts 2 Posters 930 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    banbutcher
    last edited by Apr 12, 2020, 3:10 AM

    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

    S 1 Reply Last reply Apr 12, 2020, 3:26 AM Reply Quote 0
    • S Away
      sdetweil @banbutcher
      last edited by sdetweil Apr 12, 2020, 4:10 AM Apr 12, 2020, 3:26 AM

      @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

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      1 Reply Last reply Reply Quote 0
      • B Offline
        banbutcher
        last edited by Apr 12, 2020, 11:26 AM

        @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

        S 1 Reply Last reply Apr 12, 2020, 11:53 AM Reply Quote 0
        • S Away
          sdetweil @banbutcher
          last edited by Apr 12, 2020, 11:53 AM

          @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) {
                      }
                  }
          )
          

          Sam

          How to add modules

          learning how to use browser developers window for css changes

          1 Reply Last reply Reply Quote 0
          • B Offline
            banbutcher
            last edited by Apr 13, 2020, 10:44 AM

            @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

            S 1 Reply Last reply Apr 13, 2020, 11:00 AM Reply Quote 0
            • S Away
              sdetweil @banbutcher
              last edited by Apr 13, 2020, 11:00 AM

              @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

              Sam

              How to add modules

              learning how to use browser developers window for css changes

              1 Reply Last reply Reply Quote 0
              • B Offline
                banbutcher
                last edited by Apr 13, 2020, 11:18 AM

                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…

                B S 2 Replies Last reply Apr 13, 2020, 11:26 AM Reply Quote 0
                • B Offline
                  banbutcher @banbutcher
                  last edited by banbutcher Apr 13, 2020, 11:28 AM Apr 13, 2020, 11:26 AM

                  @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! :)

                  S 1 Reply Last reply Apr 13, 2020, 11:29 AM Reply Quote 0
                  • S Away
                    sdetweil @banbutcher
                    last edited by Apr 13, 2020, 11:29 AM

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

                    ill be asking more silly questions

                    no such thing… ask away…

                    Sam

                    How to add modules

                    learning how to use browser developers window for css changes

                    1 Reply Last reply Reply Quote 1
                    • S Away
                      sdetweil @banbutcher
                      last edited by Apr 13, 2020, 11:31 AM

                      @banbutcher you can also convert the buffer to a string

                      buffer_object.toString()

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        8/10
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy