KudaGo concerts and events in your mirror

I’ll tell you about how I made it possible to receive and display information from the public KudaGo API on your mirror. Of course, this is not about a simple, but about a “smart” mirror.



What is a smart mirror?



Most likely, many have heard about the MagicMirror² modular platform, which allows you to deploy an application on Raspberry Pi that can turn a mirror for an entrance hall or a bathroom into your personal assistant. You can implement this or this using the so-called "two-way" mirror, monitor, straight arms, and the aforementioned microcomputer.



Magic in MagicMirror is only in the name. If I understood correctly, all this was developed on Electron , which means that Node.js. is involved in the matter. The "Application" creates an html page that, when launched, opens in a browser window, and so-called modules are already displayed there, both pre-installed (Clock, Calendar, Current Weather, Weather Forecast, News Feed.), And developed by the community . And there is enough interesting (both useful and stubborn unusual), for example, here are just a few of more than a hundred modules:



Useful





Fancy





Background



About how I collected my mirror I will not paint in detail. If someone becomes interested then you can read here . If you want to put together your own, then a simple Google search will return dozens of examples \ guides \ manuals. I want to show how to make a module for such a mirror.



I am a big fan of attending various concerts and events of St. Petersburg. But sometimes it happens that (even taking into account the abundance of contextual advertising, spam and posters) I learn about some events almost a day or two before them. Therefore, I was always looking for a way to optimally filter the flow of information about them. Applications on a smartphone with such functionality did not linger for a long time (more precisely, I forgot about them). All posters required time and constant passage of the quest “choose 10 filters, and guess which one is right”. Social media feeds generally turned into advertising spam. And then the idea came up with the idea of ​​implementing a “parser” that would periodically select information on its own criteria and display it somewhere.



Development of your module



If someone immediately wants to see the result, or install the module on a “mirror”, then here is the link to the repository . There is also installation instructions.



As a source of information, the kudago service turned out to be the most adequate. No registration required. Sane methods. Concise data in the answer. It’s not strange coverage, it includes, not only “city curb” and “non-rubber”, but also 9 major Russian cities in addition.



Our developed module should be stored in the modules / modulename folder . To create a simple module (like mine) we need to create two files in this folder:





Having tinkered with a guide from the MagicMirror developer , as well as looking at other people's solutions, I uploaded a script to get entries from the kudago API using XMLHttpRequest. And ran into the problem of cross-domain queries. I'm not a javascript connoisseur at all (and not even a developer). All I understood was to send a request “running a script in a browser” and get a response from kudago, I couldn’t succeed. Here is what learn.javascript explained to me



Cross-domain requests undergo a special security control, the purpose of which is to prevent evil hackers from conquering the Internet.



Seriously. The developers of the standard provided all the barriers so that the “evil hacker” could not, using the new standard, do something fundamentally different from what he could have done before, and thus “break” some server running the old standard and not expecting anything fundamentally new.


Well, not really what I wanted. I had no desire to understand javascript problems. After all, at hand was the familiar and predictable Python. I will just get information from the requests library, writing the result in json. It remains only to solve the question of how to get Python code to be executed from js.



It turns out that node_helper.js is useful to me! When starting our module, you can use the capabilities of python-shell . Just specify the path to the Python interpreter, the path to the script, the arguments and their values. In this form, kudago kindly returned a successful answer to me.



Python script call code
kudagoGetData: function () { //call python script for collecting events from KudaGo api self = this; var options = { pythonPath: this.config.pythonPath, scriptPath: './modules/MMM-KudaGo', mode: 'json', args: [ "--location", this.config.location, "--days", this.config.days, "--categories", this.config.categories, "--tags", this.config.tags, ] }; pyshell.PythonShell.run('KudaGo.py', options, function (err) { if (err) throw err; }); }
      
      







In addition, only node_helper.js is able to read information from the json file (by default, the file will lie on the path kudago / events.json ) with the downloaded data from kudago. We implement in it the same reading from the file.



Code for reading information from a file
 readData: function () { //read a file with events fs.readFile("./modules/MMM-KudaGo/kudago/events.json", "utf8", (err, data) => { if (err) throw err; this.sendSocketNotification("DATA", data); }); }
      
      







As I already wrote, in MagicMirror all modules communicate through the socket system, so in order for our Python script to run at startup, add the appropriate code to node_helper.js



Code to start the module at startup
 socketNotificationReceived: function (notification, payload) { if (notification === "START") { this.config = payload; this.kudagoGetData(); setInterval(() => { this.kudagoGetData(); }, this.config.updateInterval); } }
      
      







All the parameters that are obtained from this.config are either the predefined variables in the main file of the module, or those specified in config.js - the general configuration file for all MagicMirror modules.



Example config for the created module
 { module: 'MMM-KudaGo', disabled: false, position: 'bottom_bar', config: { location: "spb", categories: "concert", tags: "  --,", days: 7 } }
      
      







Well, in the main file of our MMM-KudaGo.js module there are default parameters, functions for handling event data (taken from a json file), functions for drawing a plate (from which people who know will leak blood from my eyes, but I'm in a tank, the main thing for me is worked). Due to the fact that I took out work with the API in python files, .js files are not overloaded with code (unlike most other modules), nested (or what are they called here?) Functions, loops and other noodles.



Result



Screenshot of the module itself
image



Mirror screenshot
image



It turned out to implement a search on one or several categories of events, search by tags, search for a given interval of days, with updating at specified intervals (For a detailed list of categories and tags, see readme ). And display it all on the screen, indicating the venue, name, date and price, in a readable form, and without wasting time on an independent search.



Of course, you need to carefully watch several times what the applets returns to understand what tags you need to put so that he does not show concerts or theater performances, but for example yoga courses or free festivals in your city. But personally, my needs are fully satisfied!



I hope you also come up with and create a module useful for the community, which you share with others on a special page of the project. By the way, if you have good ideas, or need for something, then write in the comments. Perhaps push someone to the creative!



All Articles