• 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.

Need help creating a module.

Scheduled Pinned Locked Moved Solved Requests
6 Posts 2 Posters 500 Views 2 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.
  • S Offline
    sdmydbr9
    last edited by Feb 9, 2023, 1:12 PM

    Being from one of the remotest part of the world, it’s not easy to get the local news from the convenient sources to stay updated, having no options I was using the default newsfeed modules. My only source of local news is the local YouTube news channel, therefore I was looking for ideas to implement something similar to display the titles of the latest YouTube videos on the mirror. I used the MMM-Pythonprint module for doing that, unfortunately I couldn’t figure out a way to rotate the titles one at a time since my python script prints the title of latest 10 videos using YouTube api.
    I then stumbled upon chatgpt, I asked it to create a module for me to use this functionality.
    It did create the module, it gave me the following codes but due to some reasons I could not get it to work, Iam still a learner and I have zero knowledge about codes. Im hoping someone will stumble upon this and figure out a way

    This is the main.js

    
    Module.register("MMM-LocalNews", {
        // Default module config.
        defaults: {
            channelId: "UCX6OQ3DkcsbYNE6H8uQQuVA",
            apiKey: "YOUR_API_KEY",
            updateInterval: 60 * 60 * 1000, // Every hour.
            animationSpeed: 2 * 1000,
            initialLoadDelay: 0,
            retryDelay: 2500,
            maxTitles: 10,
            reverse: false
        },
    
        // Define required scripts.
        getScripts: function() {
            return [];
        },
    
        // Define required styles.
        getStyles: function() {
            return [
                "MMM-LocalNews.css"
            ];
        },
    
        // Define start sequence.
        start: function() {
            Log.info("Starting module: " + this.name);
    
            // Set locale.
            moment.locale(config.language);
    
            this.videoTitles = [];
            this.scheduleUpdate();
        },
    
        // Override the getDom method.
        getDom: function() {
            var wrapper = document.createElement("div");
            wrapper.className = "video-titles";
    
            if (!this.videoTitles.length) {
                wrapper.innerHTML = "Loading video titles...";
                wrapper.classList.add("bright", "light");
                return wrapper;
            }
    
            if (this.config.reverse) {
                this.videoTitles.reverse();
            }
    
            this.videoTitles.forEach(function(videoTitle) {
                var title = document.createElement("div");
                title.className = "video-title";
                title.innerHTML = videoTitle;
                wrapper.appendChild(title);
            });
    
            return wrapper;
        },
    
        // Request video titles from the node helper.
        scheduleUpdate: function() {
            setTimeout(function() {
                this.sendSocketNotification("GET_VIDEO_TITLES", {
                    channelId: this.config.channelId,
                    apiKey: this.config.apiKey
                });
            }.bind(this), this.config.initialLoadDelay);
    
            setInterval(function() {
                this.sendSocketNotification("GET_VIDEO_TITLES", {
                    channelId: this.config.channelId,
                    apiKey: this.config.apiKey
                });
            }.bind(this), this.config.updateInterval);
        },
    
        // Handle the video titles received from the node helper.
        socketNotificationReceived: function(notification, payload) {
            if (notification === "VIDEO_TITLES") {
                this.videoTitles = payload.titles.slice(0, this.config.maxTitles);
                this.updateDom(this.config.animationSpeed);
            }
        }
    });
    
    

    And here is the node_helper.js

    
    var https = require("https");
    
    // Replace YOUR_API_KEY with your actual API key.
    var apiKey = "YOUR_API_KEY";
    var channelId = "UC_CHANNEL_ID";
    
    // Build the API request URL.
    var url = "https://www.googleapis.com/youtube/v3/search?key=" + apiKey + "&channelId=" + channelId + "&part=snippet,id&order=date&maxResults=10";
    
    https.get(url, function(res) {
        var body = "";
    
        res.on("data", function(chunk) {
            body += chunk;
        });
    
        res.on("end", function() {
            var response = JSON.parse(body);
            var titles = [];
    
            for (var i = 0; i < response.items.length; i++) {
                titles.push(response.items[i].snippet.title);
            }
    
            console.log(titles);
        });
    }).on("error", function(error) {
        console.log("Error: " + error.message);
    });
    
    

    I have also created a GitHub repository for the same
    link to repo

    Can anyone provide me another alternative to do this because this module doesn’t work, or can someone fix this module to make it work. Thanks

    M 1 Reply Last reply Feb 9, 2023, 8:14 PM Reply Quote 0
    • S Offline
      sdmydbr9 @sdmydbr9
      last edited by Feb 10, 2023, 3:14 PM

      @sdmydbr9
      Module has been created and working version has been published in GitHub. Feel free to Check out

      M 1 Reply Last reply Feb 10, 2023, 3:54 PM Reply Quote 0
      • M Offline
        mumblebaj Module Developer @sdmydbr9
        last edited by Feb 9, 2023, 8:14 PM

        @sdmydbr9 A few things are missing. Your MMM-LocalNews.js sends a Socket Notification which the node_helper is supposed to listening for and act upon. The node_helper need a socketNotificationReceived in order to start its work. When it is done doing it’s work it needs to send the data back to the MMM-LocalNews.js (sendSocketNotifcation). Have a look at this sample module https://github.com/sdetweil/SampleModule.

        Check out my modules at: https://github.com/mumblebaj?tab=repositories

        S 2 Replies Last reply Feb 10, 2023, 6:35 AM Reply Quote 1
        • S Offline
          sdmydbr9 @mumblebaj
          last edited by Feb 10, 2023, 6:35 AM

          @mumblebaj so to sum up, the main.is is correct and I need to edit the nodehelper.js script?

          1 Reply Last reply Reply Quote 0
          • S Offline
            sdmydbr9 @mumblebaj
            last edited by Feb 10, 2023, 6:47 AM

            @mumblebaj
            Here is the updated nodehelper

            
            var https = require("https");
            
            module.exports = NodeHelper.create({
            
              // Override start method.
              start: function() {
                console.log("Starting node helper for: " + this.name);
              },
            
              // Handle the GET_VIDEO_TITLES socket notification.
              socketNotificationReceived: function(notification, payload) {
                if (notification === "GET_VIDEO_TITLES") {
                  var apiKey = payload.apiKey;
                  var channelId = payload.channelId;
            
                  var url = "https://www.googleapis.com/youtube/v3/search?key=" + apiKey + "&channelId=" + channelId + "&part=snippet,id&order=date&maxResults=10";
            
                  https.get(url, function(res) {
                    var body = "";
            
                    res.on("data", function(chunk) {
                      body += chunk;
                    });
            
                    res.on("end", function() {
                      var response = JSON.parse(body);
                      var titles = [];
            
                      for (var i = 0; i < response.items.length; i++) {
                        titles.push(response.items[i].snippet.title);
                      }
            
                      // Send the video titles back to the module.
                      this.sendSocketNotification("VIDEO_TITLES", { titles: titles });
                    }.bind(this));
                  }.bind(this)).on("error", function(error) {
                    console.log("Error: " + error.message);
                  });
                }
              }
            });
            
            
            S 1 Reply Last reply Feb 10, 2023, 3:14 PM Reply Quote 0
            • S Offline
              sdmydbr9 @sdmydbr9
              last edited by Feb 10, 2023, 3:14 PM

              @sdmydbr9
              Module has been created and working version has been published in GitHub. Feel free to Check out

              M 1 Reply Last reply Feb 10, 2023, 3:54 PM Reply Quote 0
              • M Offline
                mumblebaj Module Developer @sdmydbr9
                last edited by Feb 10, 2023, 3:54 PM

                @sdmydbr9 Awesome. Glad we could help

                Check out my modules at: https://github.com/mumblebaj?tab=repositories

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/6
                  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