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.
    • 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
                                  • S Offline
                                    Squirrel
                                    last edited by

                                    Hmm since i changed some things in node_helper.js my MagicMirror is no more working and i get the following ERROR:

                                    npm ERR! Linux 4.4.34-v7+
                                    npm ERR! argv “/usr/local/bin/node” “/usr/local/bin/npm” “start”
                                    npm ERR! node v0.12.6
                                    npm ERR! npm v2.11.2
                                    npm ERR! code ELIFECYCLE
                                    npm ERR! magicmirror@2.0.0 start: electron js/electron.js
                                    npm ERR! Exit status 1
                                    npm ERR!
                                    npm ERR! Failed at the magicmirror@2.0.0 start script ‘electron js/electron.js’.
                                    npm ERR! This is most likely a problem with the magicmirror package,
                                    npm ERR! not with npm itself.
                                    npm ERR! Tell the author that this fails on your system:
                                    npm ERR! electron js/electron.js
                                    npm ERR! You can get their info via:
                                    npm ERR! npm owner ls magicmirror
                                    npm ERR! There is likely additional logging output above.

                                    npm ERR! Please include the following file with any support request:
                                    npm ERR! /home/pi/MagicMirror/npm-debug.log

                                    This is what i did in config.js:

                                    module: 'MMM-DWD-WarnWeather',
                                    position: 'bottom_right',
                                    header: 'Wetterwarnungen',
                                    config: {
                                        region: 'Seeland/Bielersee',					// Kreis Lörrach
                                        changeColor: true,
                                        interval: 10 * 60 * 1000, // every 10 minutes
                                        loadingText: 'Warnungen werden geladen...',
                                        noWarningText: 'Keine Warnungen'
                                    }
                                    

                                    },

                                    And here what i did in node_helper.js :

                                    getWarningData: function (region) {
                                    	var self = this;
                                    
                                    	var timestamp = Date.now().toString();
                                    	var url = 'http://my.wetter.ch/v6/alarms/meteo/with-regions.json' + timestamp;			// http://www.dwd.de/DWD/warnungen/warnapp_landkreise/json/warnings.json?jsonp=loadWarnings
                                    
                                    
                                    
                                    	request({
                                    		url: url,
                                    		method: 'GET'
                                    	}, function (error, response, body) {
                                    
                                    		var result = JSON.parse(body.substr(24).slice(0, -2));
                                    		var warningData = [];
                                    		for (var regionId in result['warnings']) {
                                    			if (result['warnings'][regionId][0]['name_de'] == region) {			// name_de = regionName 
                                    				var warnings = [];
                                    				for (var warning in result['warnings'][regionId]) {
                                    					warnings.push(result['warnings'][regionId][warning]);
                                    				}
                                    				if (warnings.length > 0) {
                                    					warningData = warnings;
                                    				}
                                    			}
                                    		}
                                    		self.sendSocketNotification('WARNINGS_DATA', {warnings: warningData, region: region});
                                    
                                    	});
                                    

                                    I changed the link and i changed regionName to name_de… Any idea? Hope this is gonna be my last Question ;)

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

                                      @Squirrel
                                      So first of all i would suggest you to open a new topic in the development area of this forum.
                                      Second, it looks like you have not much experience http-requests and data-structures =D but that’s no problem =) it’s a good point to start for you now =)
                                      To help you i need the request which you have monitored with mitmproxy.
                                      To do that, open the request in mitmproxy and press E to export it. Then choose curl command as output-format and choose a file where you save this command.
                                      With this command i can explain you how to go on. So please post the content of this file in the new topic which you will create now =)

                                      1 Reply Last reply Reply Quote 0
                                      • morozgrafixM Offline
                                        morozgrafix Moderator @gismo2006
                                        last edited by

                                        @gismo2006 I know this thread is solved and a few months old. There is also another one liner solution with use of jq JSON parser from command line. Here is an example that would get result that you need (or at least get you close to it)

                                        curl -s "http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=json&appid=YOUR_OWM_API_KEY" | jq -r '"Today is " + .weather[].description + " and " + (.main["temp"]|tostring) + " degrees."' > filename.txt
                                        

                                        would result in Today is überwiegend bewölkt and -6.12 degrees. put into filename.txt

                                        1 Reply Last reply Reply Quote 1

                                        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
                                        • 2 / 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