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.

    save some information from a xml or json to a txt file

    Scheduled Pinned Locked Moved Requests
    25 Posts 5 Posters 20.6k Views 5 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.
    • G Offline
      gismo2006
      last edited by

      oh sorry i forgett, to describe. I have several bash scripts. For a example an bash script which display time an other one which display date. Now I have create a module for myself using the Hotword module by alexya. It works perfect. When i say “aktuellesDatum” (in englich actuellDate) my mirror says In german “Today is the 20. October 2016” for example. The same at the timescript.
      Then I have (I thing you as a good coder like to kill mke :-D ) a php script on my own webserver who runs at start up (I use cronjob) to read out the rss feed of the german website “Tageschschau” (A news site") and write the header in a txt file. Then the txst file is automaticly copied in in my own modulefolder. Then I write a module for myself again, using hotword detection again. If I now say “Aktuelle Nachrichten” (Actually News) My mirror reads out loudly the TXT file. (before it will read out my mirror create an acutell txt file ,whit the newest news)

      So I haven’t to look at the mirror, maybe durring take a shower in the morning. (Every day the same, I haven’t enough time in the morning :-D ) or something else i will inform about actually news, time or edate. I have tested several tts but the best for me was pico2wav. And a feature i added at this way too, was to read out loud the ip adress of the mirror. So I can simple control the pi if my monitor didn’t work. (save shut down for example).

      AND NOW. the thing i need this txt file I asked for. I like to do the same whit a little weather sentences about the actuall weather. In the json or xml file which I have was written Wolkig (cloudly) and in an other tag the temp. If I have finisched this I like to say “AktuellesWetter” (actuaklly weather) and my mirror says “10 °C, wolkig” So I will know very fast what clothes I need and (for example) I need a umbrella.

      Now you like to say maybe, “you write you read out the feeds of the Tagesschau feed. Why do you not use this for the txt you like too?” I will answer I have test it but it didn’t work.

      greets gismo

      LukeCodewalkerL 1 Reply Last reply Reply Quote 0
      • LukeCodewalkerL Offline
        LukeCodewalker Module Developer @gismo2006
        last edited by

        Hey @gismo2006,
        I think i can help you. As i started to develop MMM-DWD-WarnWeather, i used a little Pythonscript to explore the API. So if Python would be ok for you, just explain what Data should be written to your Text file and i can modify the Script for you.

        1 Reply Last reply Reply Quote 0
        • G Offline
          gismo2006
          last edited by

          @LukeCodewalker Hi, YEAH! THANK YOU! Python will be OK for me. I have tested a python script, that I found at the www. Lat days. But it didn’t work. Late today if I have some time I will post some more information OK?

          Greets gismo

          LukeCodewalkerL 1 Reply Last reply Reply Quote 0
          • LukeCodewalkerL Offline
            LukeCodewalker Module Developer @gismo2006
            last edited by

            @gismo2006 sure =)

            1 Reply Last reply Reply Quote 0
            • G Offline
              gismo2006
              last edited by

              @LukeCodewalker

              Here some more information:

              I use this link to use the api (It the same website which is use for the default Weather modul)

              http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=xml&appid=

              (I only cut out my api key at he end)

              This link create a xml file (You can change into json, simply erase xml in the link and put json there)

              This xml looks like this:

              DE

              And, I only like that your python script read out the line till and if it possible the line <temperature, too. And the save this in a txt file.

              That will be great.

              And if I can a have little bit more of your time it will be possible the txt file can use the following "Today it is and .

              It would be perfekt. But that I can took later by learning myself. If your python script can read out the xml file and save in a txt file it will be great.

              Greets gismo

              LukeCodewalkerL 1 Reply Last reply Reply Quote 0
              • LukeCodewalkerL Offline
                LukeCodewalker Module Developer @gismo2006
                last edited by

                @gismo2006 here it is. just place your app id and the path of the file, where the text should go.

                import json
                import subprocess
                
                APP_ID = ''
                FILE_PATH = 'test.txt'
                
                url = 'http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=json&appid=' + APP_ID
                curl_req = 'curl -L ' + '"' + url + '"'
                p = subprocess.Popen(curl_req, stdout=subprocess.PIPE, shell=True)
                (output, err) = p.communicate()  
                p_status = p.wait()
                
                try:
                    decoded = json.loads(output)
                    weather = decoded['weather'][0]['description']
                    temp = decoded['main']['temp']
                    f = open(FILE_PATH,'w')
                    f.write('Today is ' + weather + ' and ' + str(temp) + ' degrees.')
                    f.close()
                except (ValueError, KeyError, TypeError):
                    pass
                
                1 Reply Last reply Reply Quote 0
                • G Offline
                  gismo2006
                  last edited by

                  @LukeCodewalker

                  Hi, thank you for the code. But there was a little misstake I think. I have copied your code and save. put in my api and the right folder.

                  then the first try. All works fine the code generate a test.txt file. But then I oen the txt file it was empty.
                  Ok I think maybe I put in something wrong. But everything was ok.

                  At a second try I chnage the folder name, because I think maybe the was some folder Rights or something else. But the same. The txt file was empty.

                  At my next try I cut of the f.write line and there was only written f.write(‘Today is’)
                  And I see everything work like it should. In the txt file was written Today is. Now I put some more in this line again. Everything works fine till I put the word weather. If I put this the txt file will be empty.

                  I like to try something mor later today. But I want give you a feedback. And I like to say thank you.

                  greets gismo

                  1 Reply Last reply Reply Quote 0
                  • G Offline
                    gismo2006
                    last edited by

                    Sorry I forgett there was no error. Also Python displays no error if I try it a the Python IDLE.

                    LukeCodewalkerL 1 Reply Last reply Reply Quote 0
                    • LukeCodewalkerL Offline
                      LukeCodewalker Module Developer @gismo2006
                      last edited by

                      @gismo2006 I think I got the problem. The german description can contain letters like äöü etc. i will fix that later when i’m at home.

                      1 Reply Last reply Reply Quote 0
                      • LukeCodewalkerL Offline
                        LukeCodewalker Module Developer
                        last edited by

                        Now it should work

                        #!/usr/bin/python
                        # -*- coding: utf-8 -*-
                        import codecs
                        import json
                        import subprocess
                        
                        APP_ID = ''
                        FILE_PATH = 'test.txt'
                        
                        url = 'http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=json&appid=' + APP_ID
                        curl_req = 'curl -L ' + '"' + url + '"'
                        p = subprocess.Popen(curl_req, stdout=subprocess.PIPE, shell=True)
                        (output, err) = p.communicate()  
                        p_status = p.wait()
                        
                        try:
                            decoded = json.loads(output)
                            weather = decoded['weather'][0]['description']
                            temp = decoded['main']['temp']
                            f = codecs.open(FILE_PATH,'w', 'utf-8')
                            f.write('Today is ' + weather + ' and ' + str(temp) + ' degrees.')
                            f.close()
                        except (ValueError, KeyError, TypeError):
                            pass
                        
                        1 Reply Last reply Reply Quote 0
                        • G Offline
                          gismo2006
                          last edited by

                          Hi, a big thank you!!

                          Its work perfect!!!

                          Many Thanks! :-)

                          greets gismo

                          morozgrafixM 1 Reply Last reply Reply Quote 0
                          • LukeCodewalkerL Offline
                            LukeCodewalker Module Developer
                            last edited by

                            Welcome :)

                            S 1 Reply Last reply Reply Quote 0
                            • S Offline
                              Squirrel @LukeCodewalker
                              last edited by

                              @LukeCodewalker i want to make the same Module for Switzerland but i don`t know how to find the json data for my region…

                              Can you help me please? ;) I found your link in the node_helper.js file. From where do you got that?

                              I would like o work with http://alarm.meteocentrale.ch/

                              LukeCodewalkerL 2 Replies Last reply Reply Quote 0
                              • LukeCodewalkerL Offline
                                LukeCodewalker Module Developer @Squirrel
                                last edited by

                                @Squirrel
                                i did some reverse engineering of the dwd-website and found this json file.
                                i had a short look at your link and did not found something similar there. do you know if theres an app for ios where the data is shown? maybe i can help you then.

                                1 Reply Last reply Reply Quote 0
                                • LukeCodewalkerL Offline
                                  LukeCodewalker Module Developer @Squirrel
                                  last edited by

                                  @Squirrel
                                  maybe this link helps you:
                                  https://s3-eu-central-1.amazonaws.com/app-prod-static-fra.meteoswiss-app.ch/v1/warnings_with_outlook_with_naturalhazards_de.json

                                  1 Reply Last reply Reply Quote 0
                                  • S Offline
                                    Squirrel
                                    last edited by Squirrel

                                    @LukeCodewalker Thx for the answer. I will try it out. At the moment i can`t find my City. But maybe only because there is no warning at the moment? Otherwise on the alarm.meteocentrale.ch is still a warning for snow on the streets…?

                                    What about wetteralarm.ch/? This is a app for iPhone and Android! Maybe it can work with this site?

                                    LukeCodewalkerL 1 Reply Last reply Reply Quote 0
                                    • LukeCodewalkerL Offline
                                      LukeCodewalker Module Developer @Squirrel
                                      last edited by

                                      @Squirrel
                                      I can’t download this app in germany, so I can’t have a look at the data it uses.

                                      1 Reply Last reply Reply Quote 0
                                      • S Offline
                                        Squirrel
                                        last edited by

                                        Ok. Shit happens. Maybe you can tell me with which program/method u read the code of a app? The I can do it alone…

                                        LukeCodewalkerL 1 Reply Last reply Reply Quote 0
                                        • LukeCodewalkerL Offline
                                          LukeCodewalker Module Developer @Squirrel
                                          last edited by

                                          @Squirrel
                                          I use mitmproxy for viewing the http-requests of the apps.

                                          1 Reply Last reply Reply Quote 1
                                          • S Offline
                                            Squirrel
                                            last edited by

                                            Thx! After some little problems to install all the things to use mitmproxy it works now and i found some good json files. The only problem i have is that there is one json for region where i can`t choose a city. There are different json files for every city! Maybe i gonna make a module with regions for everybody but use the city json for my self!

                                            1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              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