Scroll phat wunderground

Evening,

I’m having a play with the scroll phat (Well Scroll Bot really) and wanted to use the Wunderground weather example. However, wunderground seem to hae stopped offering the free api. Are there alternative examples that I could use in a school that will show the weather on the mighty bot?

ta

Alex

Looks like this might be a viable alternative api, but there’s no example for it yet: https://www.openweathermap.org/api

Yeah…I had a quick look at that but frankly have no idea what I am doing. Are there any simple to understand tutorials on dealing with API’s? So far the ones I’ve come across are so full of jargon it makes it impossible to know what is going on.

My guess is that the script goes and gets the data for a specific location and then returns it. the rest of the script organises it and then displays it

Good guess! It’s not that difficult if you take it step by step. Try doing a search for ‘simple python requests json tutorial’

Here’s one that came up from my quick search:

And another one over the requests library:
http://docs.python-requests.org/en/master/user/quickstart/

Hope this helps!

I was contemplating making writing this example my Twitch Stream theme for this afternoon- showing how I might put it together step-by-step as @Blanchot suggests.

Thanks for the pointers. I’m going to have a play about. Whats the worst that could happen ;-)

So I’ve had a play this evening and come a bit unstuck. It might be worth moving this thread but I’ll post anyway. One of the other projects I’m involved with is one called SatNOGS (its a series of networked ground stations that listen for satellites using RPi’s and rtl_sdr dongles…its pretty cool). One of my ground stations is numbered 33 so I thought perhaps I could query the api and return the next observation then display some of the basic data like the satellite name (or norad_id) and the transmitter.

I’ve managed to set up th eurl query and return the full set of data but parsing it is where I’m falling down a bit. I thought I’d return a few observation id’s (called ‘id’) with the hope of producing a list of id’s to start with.

code so far is:

import urllib.parse
import requests

url = 'https://network.satnogs.org/api/observations/?format=json&ground_station=33'

json_data = requests.get(url).json()
#print(json_data) - just used this to prove I was getting the right data

observation_id = json_data['0'][0]['id']
print ('obs_id: '+observation_id)

The error returned is:

Traceback (most recent call last):
File "python", line 9, in <module>
TypeError: list indices must be integers or slices, not str

I’m not sure I fully understand the error. So a clue to that would be handy. I’ve found returning the data is relatively easy, its selecting data that I find tricky. From what I can understand is that the data is contained within a dictionary that has a few sub ‘parts’. For simplicity I tried to return the one right at the top under the sub heading ‘0’.

Can anyone put the right code in line line and explain what I’ve done wrong?

After that I’m gong to need to work out how to return the ‘next’ observation as it doesn’t sit at the top of the page but I guess is dynamic and based on the number of scheduled observations.

After that I’m going to need to query one of the other available api’s to return the satellite name from the norad_id

I think I’ve bitten off way more than I can chew but am really keen to see this one through as it’s cool

cheers

Alex

The “list indices must be integers or slices, not str” error is a little obtuse sounding, but it actually quite simply if you know what’s going on. In python a list looks like this:

my_list = ['a', 'b', 'c']

It has an implicit order to its items, and each one receives an index 0, 1, 2, but the items are not named in any way so they can only be referred to either by their index or a “slice” which is a way of expressing a group of indexes. IE this will work:

>>> my_list = ['a', 'b', 'c']
>>> my_list[0]
'a'

But this will not:

>>> my_list = ['a', 'b', 'c']
>>> my_list['0']

And neither will this:

>>> my_list = ['a', 'b', 'c']
>>> my_list['blahblah']

Because ‘0’ and ‘blahblah’ are both strings. You might just as well be asking “get me number b” which makes no sense!

This is not to be confused with dictionaries, which have a “key” you can reference by name:

>>> my_list = {'0': 'monkey', 'blahblah': 'badger'}
>>> my_list['0']
'monkey'
>>> my_list['blahblah']
'badger'

So, TLDR: The error is just a really obtuse way of saying “you need a number to get the value of something form a list”

No doubt your json_data['0'] should read json_data[0]

It’s always good practise to do this kind of stuff in a Python repl (just run Python at the command line) so you can step through your code piece by piece and play with the json_data object to see what it contains.

In your repl you might:

>>> import urllib.parse
>>> import requests
>>> data = requests.get('your HTTP url').json()
>>> print(json)
... shows what's returned
>>> json['0']
... returns an error
>>> json[0]
... shows the first element
>>> dir(json)
... returns a list of methods on `json`

This makes it easy to prod and poke at something exploratively, and while you’ll still see errors raised, they wont abort the repl so you can continue prodding and poking until you learn what’s what, and then go and bake that into your .py file.

Thanks for that. It’s starting to make sense now and I’ve managed to extract the first part of what I want. All told JSON is a very useful tool to get stuff from somewhere else. I can see this taking me a while to get it ‘just right’. Mechanical engineers and coding don’t tend to mix well ;-).

Once I’ve got it all sorted and working I’ll put it somewhere useful in case someone else takes an interest in satellites, Raspberry Pi’s, rtl_sdr and damp string.

Alex