You are not logged in.

#1 2019-07-02 14:21:18

Strykar
Member
Registered: 2018-02-17
Posts: 50

Migrate an existing Sopel IRC bot python module to a new JSON feed

I know some shell, but no Python, and I need help to figure this out.
I'm trying to get an existing python module for the Sopel IRC bot to use a new JSON feed for current weather data from the Insight rover on Mars. See https://mars.nasa.gov/insight/weather/

I thought it wouldn't be that hard to migrate this plugin:

# -*- coding: utf-8 -*-

import sopel.module
import json
import urllib2
from sopel.module import commands, example

degree_sign= u'\N{DEGREE SIGN}'

@commands('weathermars')
@example('.weathermars')
def weathermars(bot, trigger):
    weather_url = 'http://marsweather.ingenology.com/v1/latest/?format=json'
    weather_data = urllib2.urlopen(weather_url)
    data_json = json.load(weather_data)
    min_temp = data_json['report']['min_temp']
    max_temp = data_json['report']['max_temp']
    pressure = data_json['report']['pressure']/1000
    climate = data_json['report']['atmo_opacity']
    bot.say('Il fait un peu froid sur Mars aujourd\'hui: minimum %.1f %sC, maximum %.1f %sC' % (min_temp, degree_sign, max_temp, degree_sign))
    bot.say('La pression est de %.3f kPa, et les conditions climatiques sont %s' % (pressure, climate))

From the old deprecated Curiosity rover MAAS API JSON feed:

{
    "report": {
        "terrestrial_date": "2013-05-01", 
        "sol": 261, 
        "ls": 310.5, 
        "min_temp": -69.75, 
        "min_temp_fahrenheit": -93.55, 
        "max_temp": -4.48, 
        "max_temp_fahrenheit": 23.94, 
        "pressure": 868.05, 
        "pressure_string": "Higher", 
        "abs_humidity": null, 
        "wind_speed": null, 
        "wind_direction": "--", 
        "atmo_opacity": "Sunny", 
        "season": "Month 11", 
        "sunrise": "2013-05-01T11:00:00Z", 
        "sunset": "2013-05-01T22:00:00Z"
    }
}

To the Insight rover's Mars weather data from APSS (Auxiliary Payload Subsystem) instrument API feed:

{
  "200": {
    "AT": {
      "av": -70.076, 
      "ct": 88776, 
      "mn": -104.347, 
      "mx": -24.617
    }, 
    "First_UTC": "2019-06-19T17:08:19Z", 
    "HWS": {
      "av": 4.153, 
      "ct": 40197, 
      "mn": 0.129, 
      "mx": 15.554
    }, 
    "Last_UTC": "2019-06-20T17:47:54Z", 
    "PRE": {
      "av": 760.183, 
      "ct": 887772, 
      "mn": 738.173, 
      "mx": 777.275
    }, 
    "Season": "winter", 
    "WD": {
      "1": {
        "compass_degrees": 22.5, 
        "compass_point": "NNE", 
        "compass_right": 0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 1
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 7033
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 5029
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 7928
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 161
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 13
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 1
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 5
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 623
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 403
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 3855
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 9081
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 3276
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 2788
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 9081
      }
    }
  }, 
  "201": {
    "AT": {
      "av": -69.671, 
      "ct": 88776, 
      "mn": -103.263, 
      "mx": -24.02
    }, 
    "First_UTC": "2019-06-20T17:47:54Z", 
    "HWS": {
      "av": 4.235, 
      "ct": 39361, 
      "mn": 0.13, 
      "mx": 16.105999999999998
    }, 
    "Last_UTC": "2019-06-21T18:27:30Z", 
    "PRE": {
      "av": 759.734, 
      "ct": 887771, 
      "mn": 738.14, 
      "mx": 775.2860000000001
    }, 
    "Season": "winter", 
    "WD": {
      "0": {
        "compass_degrees": 0.0, 
        "compass_point": "N", 
        "compass_right": 0.0, 
        "compass_up": 1.0, 
        "ct": 14
      }, 
      "1": {
        "compass_degrees": 22.5, 
        "compass_point": "NNE", 
        "compass_right": 0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 1
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 5694
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 4271
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 5942
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 402
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 21
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 16
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 6
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 468
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 853
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 4882
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 8617
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 3666
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 4508
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 8617
      }
    }
  }, 
  "202": {
    "AT": {
      "av": -69.574, 
      "ct": 88776, 
      "mn": -103.326, 
      "mx": -24.353
    }, 
    "First_UTC": "2019-06-21T18:27:30Z", 
    "HWS": {
      "av": 4.398, 
      "ct": 39878, 
      "mn": 0.13, 
      "mx": 15.364
    }, 
    "Last_UTC": "2019-06-22T19:07:05Z", 
    "PRE": {
      "av": 760.242, 
      "ct": 887771, 
      "mn": 739.211, 
      "mx": 775.072
    }, 
    "Season": "winter", 
    "WD": {
      "0": {
        "compass_degrees": 0.0, 
        "compass_point": "N", 
        "compass_right": 0.0, 
        "compass_up": 1.0, 
        "ct": 1
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 6637
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 5495
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 4824
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 20
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 10
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 6
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 1
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 195
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 971
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 6208
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 7986
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 2410
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 5114
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 7986
      }
    }
  }, 
  "203": {
    "AT": {
      "av": -72.872, 
      "ct": 79626, 
      "mn": -103.98, 
      "mx": -25.698
    }, 
    "First_UTC": "2019-06-22T19:07:05Z", 
    "HWS": {
      "av": 3.973, 
      "ct": 35995, 
      "mn": 0.13, 
      "mx": 14.555
    }, 
    "Last_UTC": "2019-06-23T19:46:40Z", 
    "PRE": {
      "av": 761.877, 
      "ct": 791486, 
      "mn": 739.393, 
      "mx": 775.812
    }, 
    "Season": "winter", 
    "WD": {
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 5129
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 6356
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 5631
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 36
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 19
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 7
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 5
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 494
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 582
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 3922
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 6771
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 2996
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 4047
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 6771
      }
    }
  }, 
  "204": {
    "AT": {
      "av": -69.722, 
      "ct": 88776, 
      "mn": -104.145, 
      "mx": -25.339
    }, 
    "First_UTC": "2019-06-23T19:46:40Z", 
    "HWS": {
      "av": 4.282, 
      "ct": 39630, 
      "mn": 0.131, 
      "mx": 15.263
    }, 
    "Last_UTC": "2019-06-24T20:26:15Z", 
    "PRE": {
      "av": 761.392, 
      "ct": 887700, 
      "mn": 739.709, 
      "mx": 775.7639999999999
    }, 
    "Season": "winter", 
    "WD": {
      "0": {
        "compass_degrees": 0.0, 
        "compass_point": "N", 
        "compass_right": 0.0, 
        "compass_up": 1.0, 
        "ct": 6
      }, 
      "1": {
        "compass_degrees": 22.5, 
        "compass_point": "NNE", 
        "compass_right": 0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 15
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 3342
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 6113
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 10002
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 279
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 15
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 7
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 28
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 672
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 995
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 5125
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 8033
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 3642
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 1356
      }, 
      "most_common": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 10002
      }
    }
  }, 
  "205": {
    "AT": {
      "av": -69.64, 
      "ct": 88777, 
      "mn": -103.918, 
      "mx": -24.91
    }, 
    "First_UTC": "2019-06-24T20:26:15Z", 
    "HWS": {
      "av": 4.323, 
      "ct": 40161, 
      "mn": 0.131, 
      "mx": 15.89
    }, 
    "Last_UTC": "2019-06-25T21:05:51Z", 
    "PRE": {
      "av": 760.643, 
      "ct": 887771, 
      "mn": 738.951, 
      "mx": 775.332
    }, 
    "Season": "winter", 
    "WD": {
      "0": {
        "compass_degrees": 0.0, 
        "compass_point": "N", 
        "compass_right": 0.0, 
        "compass_up": 1.0, 
        "ct": 3
      }, 
      "1": {
        "compass_degrees": 22.5, 
        "compass_point": "NNE", 
        "compass_right": 0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 6
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 6642
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 5105
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 6779
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 552
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 43
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 10
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 29
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 429
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 582
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 5553
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 8491
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 3723
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 2214
      }, 
      "most_common": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 8491
      }
    }
  }, 
  "206": {
    "AT": {
      "av": -69.373, 
      "ct": 88777, 
      "mn": -103.47, 
      "mx": -25.303
    }, 
    "First_UTC": "2019-06-25T21:05:51Z", 
    "HWS": {
      "av": 4.277, 
      "ct": 39973, 
      "mn": 0.13, 
      "mx": 15.56
    }, 
    "Last_UTC": "2019-06-26T21:45:26Z", 
    "PRE": {
      "av": 761.348, 
      "ct": 887771, 
      "mn": 740.0989999999999, 
      "mx": 775.436
    }, 
    "Season": "winter", 
    "WD": {
      "0": {
        "compass_degrees": 0.0, 
        "compass_point": "N", 
        "compass_right": 0.0, 
        "compass_up": 1.0, 
        "ct": 1
      }, 
      "10": {
        "compass_degrees": 225.0, 
        "compass_point": "SW", 
        "compass_right": -0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 6915
      }, 
      "11": {
        "compass_degrees": 247.5, 
        "compass_point": "WSW", 
        "compass_right": -0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 5044
      }, 
      "12": {
        "compass_degrees": 270.0, 
        "compass_point": "W", 
        "compass_right": -1.0, 
        "compass_up": -0.0, 
        "ct": 3763
      }, 
      "13": {
        "compass_degrees": 292.5, 
        "compass_point": "WNW", 
        "compass_right": -0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 33
      }, 
      "14": {
        "compass_degrees": 315.0, 
        "compass_point": "NW", 
        "compass_right": -0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 25
      }, 
      "15": {
        "compass_degrees": 337.5, 
        "compass_point": "NNW", 
        "compass_right": -0.382683432365, 
        "compass_up": 0.923879532511, 
        "ct": 19
      }, 
      "2": {
        "compass_degrees": 45.0, 
        "compass_point": "NE", 
        "compass_right": 0.707106781187, 
        "compass_up": 0.707106781187, 
        "ct": 1
      }, 
      "3": {
        "compass_degrees": 67.5, 
        "compass_point": "ENE", 
        "compass_right": 0.923879532511, 
        "compass_up": 0.382683432365, 
        "ct": 215
      }, 
      "5": {
        "compass_degrees": 112.5, 
        "compass_point": "ESE", 
        "compass_right": 0.923879532511, 
        "compass_up": -0.382683432365, 
        "ct": 1504
      }, 
      "6": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 7735
      }, 
      "7": {
        "compass_degrees": 157.5, 
        "compass_point": "SSE", 
        "compass_right": 0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 6947
      }, 
      "8": {
        "compass_degrees": 180.0, 
        "compass_point": "S", 
        "compass_right": 0.0, 
        "compass_up": -1.0, 
        "ct": 1952
      }, 
      "9": {
        "compass_degrees": 202.5, 
        "compass_point": "SSW", 
        "compass_right": -0.382683432365, 
        "compass_up": -0.923879532511, 
        "ct": 5819
      }, 
      "most_common": {
        "compass_degrees": 135.0, 
        "compass_point": "SE", 
        "compass_right": 0.707106781187, 
        "compass_up": -0.707106781187, 
        "ct": 7735
      }
    }
  }, 
  "sol_keys": [
    "200", 
    "201", 
    "202", 
    "203", 
    "204", 
    "205", 
    "206"
  ], 
  "validity_checks": {
    "200": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "201": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "202": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "203": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "204": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "205": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "206": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7, 
          8, 
          9, 
          10, 
          11, 
          12, 
          13, 
          14, 
          15, 
          16, 
          17, 
          18, 
          19, 
          20, 
          21, 
          22, 
          23
        ], 
        "valid": true
      }
    }, 
    "207": {
      "AT": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7
        ], 
        "valid": false
      }, 
      "HWS": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7
        ], 
        "valid": false
      }, 
      "PRE": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7
        ], 
        "valid": false
      }, 
      "WD": {
        "sol_hours_with_data": [
          0, 
          1, 
          2, 
          3, 
          4, 
          5, 
          6, 
          7
        ], 
        "valid": false
      }
    }, 
    "sol_hours_required": 18, 
    "sols_checked": [
      "200", 
      "201", 
      "202", 
      "203", 
      "204", 
      "205", 
      "206", 
      "207"
    ]
  }
}

I looked at this NPM project to decipher the feed as it's undocumented for now - https://github.com/gablau/mars-insight-weather-node
But the feed appears to be an array of the last 7 sols and this is where I'm stuck as I don't know how to process this data, as opposed to the simple MAAS feed.

I would appreciate any pointers to get me started.

Offline

#2 2019-07-02 14:34:41

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

They are not normal arrays, they are associative arrays [aka dictionaries or hash table]

Assuming you read this structure using the json library into x
You could get to the first day's compass_degrees for the first WD (Whatever that is) with:

x['200']['WD']['1']['compass_degrees']

Note that the indices are all strings for this structure.   So, if you want to iterate over the 15 elements in WD of day 200, you either need to count with an integer, and convert that to a string, or create a list of strings from '1' to '15' and iterate over the list.

Last edited by ewaller (2019-07-02 17:40:01)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2019-07-02 17:20:29

Strykar
Member
Registered: 2018-02-17
Posts: 50

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

Cheers ewaller! This is what I came up with so far:

# -*- coding: utf-8 -*-

import sopel.module
import json
import urllib.request  as urllib2
from sopel.module import commands, example

degree_sign= u'\N{DEGREE SIGN}'

@commands('weathermars')
@example('.weathermars')
def weathermars(bot, trigger):
    weather_url = 'https://mars.nasa.gov/rss/api/?feed=weather&category=insight&feedtype=json&ver=1.0'
    weather_data = urllib2.urlopen(weather_url)
    data_json = json.load(weather_data)
    min_temp = data_json['200']['AT']['mn']
    max_temp = data_json['200']['AT']['mx']
    avg_temp = data_json['200']['AT']['av']
    min_speed = data_json['200']['HWS']['mn']
    max_speed = data_json['200']['HWS']['mx']
    avg_speed = data_json['200']['HWS']['av']
    wind_direction1 = data_json['200']['WD']['1']['compass_degrees']
    wind_direction2 = data_json['200']['WD']['1']['compass_point']
    bot.say('Temperature: Avg %.1f %sC Min %.1f %sC, Max %.1f %sC' % (avg_temp, degree_sign, min_temp, degree_sign, max_temp, degree_sign))
    bot.say('Horizontal Wind Speed: Avg %.1f %%m/s Min %.1f %%m/s, Max %.1f %%m/s' % (avg_speed, min_speed, max_speed))
    bot.say('NASA InSight Rover at Elysium Planitia, Mars - Wind Direction: %.f%s %s' % (wind_direction1, degree_sign, wind_direction2))

The output in an IRC client looks like:

<Strykar> .weathermars
<bot> Temperature: Avg -70.1 °C Min -104.3 °C, Max -24.6 °C
<bot> Horizontal Wind Speed: Avg 4.2 %m/s Min 0.1 %m/s, Max 15.6 %m/s
<bot> NASA InSight Rover at Elysium Planitia, Mars - Wind Direction: 22° NNE

How do I display the Wind Speed without a % in front of m/s and all of the bot.say statements on one line?
I think import urllib.request as urllib2 is hackish for Python3?

Last edited by Strykar (2019-07-02 17:21:29)

Offline

#4 2019-07-02 17:32:19

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

Sorry, I am not at all familiar with the sobel module.  I assume the decorators on weathermars come from that module.  It follows I have no idea about the syntax for talking to whatever it is that is defined by bot.

But, I note that in  both places you preceded "m/s" with two % symbols.  I assume one is part of the %.1f% (which I assume is asking to express the number as a float with one significant digit).  What happens if you delete the '%' right before 'm/s'  ?

Edit:  To put them all on one line, I suggest you create one big control string rather than three say statements.   You can do that like this:

theString = ("The time has come, "
                           "the walrus said, "
                            "to talk of many things")

The result will be "The time has come, the walrus said, to talk of many things"
Note that it will not be a tupple -- in this case Python figures out that that this is a series of continuation line.  I like this better than the '\' method

Edit2:  Sorry the formating of that code segment is wrong.  They are supposed to all line up.

Last edited by ewaller (2019-07-02 17:37:59)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2019-07-02 17:46:27

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,740

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

Arg!  I am not paying attention.  Those are just regular format strings, so what I said above about removing the % will work just fine.   

I use the format method for strings instead.  I believe the '%' format has been deprecated
https://docs.python.org/2/library/strin … -functions.

Edit:
Finally, if you are interested, here is a program I wrote that does something amazingly similar to what you are doing.  It gathers weather from openweathermap.org.  If you want to try it, you will need to get an API key from them.  For more information, run the program with --help

#! /usr/bin/python
""" Obtain cuurent weather from Openweather.com

Copyright (c) 2019 Eric Waller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


"""


from urllib.request import urlopen
import simplejson as json
import os
import logging
import time

PersistentStrorageFilename = os.environ['HOME'] + '/.openweathermap'


class Weather:

    """ Retrieve weather data from openweather.com

    Implements a class that requests weather data from openweather.com
    and print those data to stdout.

    Args:
        theLocation : a string representing the geolocation
        theKey      : a string representing the openweather API key

    """
    baseURL = ('http://api.openweathermap.org/data/2.5/weather?q='
               '{location:s}&appid={apikey:s}')

    # Definition of the fields of interest.  Each field is a tuple of
    #  five items --
    #    (0) a tuple that defines a path to the field,
    #    (1) a string prefix that is printed before the field value,
    #    (2) a string that is printed after the field value and
    #    (3) a flag that, if not None, is a string that define whether the
    #        field is valid for imperial or metric units.
    #    (4) a function to format the result into a string.

    # The tuple that defines the key path (first item in the field)
    # defines the path (tree) of keys for the field of interest.
    # The size of the tuples  for the keys are variable in length as
    # this json scheme uses multiple layers of hierarchical dictionaries.
    # All of the entries are keys that point to dictionaries with the
    # exception of the last, which is a key for something that is not a
    # dictionary (string, int, float,etc)

    fields = (
        (('name',),
         'Location : ', '; ', None, lambda x: x),
        (('coord', 'lat'),
         'lat: ', ', ', None, lambda x: "{:0.2f}".format(x)),
        (('coord', 'lon'),
         'lon: ', '\n', None, lambda x: "{:0.2f}".format(x)),
        (('dt',),
         'Observarion: ', '\n', None, lambda x: time.ctime(x)),
        (('main', 'temp'),
         'Temperature :', '°F ', None, lambda x: "{:0.1f}"
         .format(float(x - 273) * 9 / 5 + 32)),
        (('main', 'temp'),
         '(', '°C ) ;', None, lambda x: "{:0.1f}".format(x - 273)),
        (('weather',),
         ' ','\n', None, lambda x: x[0]['main']),
        (('main', 'humidity'),
         'Humidity: ', '% / ', None, lambda x: "{:0.1f}".format(x)),
        (('main', 'pressure'),
         'Pressure :', ' mb.\n', None, lambda x: "{:0.2f}".format(x)),
        (('visibility',),
         'Visibility ', ' km\n', None, lambda x: "{:0.1f}".format(x/1000)),
        (('wind', 'deg'),
         'Wind: ', '', None, lambda x: "{:0.0f}".format(x)),
        (('wind', 'speed'),
         ' @ ', ' kph\n', None, lambda x: "{:0.1f}".format(x)),
        (('rain', '1h'),
         'precip/hr: ', ' mm\n', None, lambda x: "{:0.2f}".format(x)),
        (('sys', 'sunrise'),
         'Sunrise ', '\n', None, lambda x: time.ctime(x)),
        (('sys', 'sunset'),
         'Sunset ', '\n', None, lambda x: time.ctime(x)),
    )

    def __init__(self, theLocation, theKey, imperial):

        self.location = theLocation
        self.key = theKey
        self.imperial = imperial
        logging.info("location set to {}".format(self.location))
        logging.info("Units are {}"
                     .format(("imperial" if self.imperial else "metric")))
        logging.info("Fetching weather data using API key {}".format(self.key))

    def GetWeather(self):
        """ Get the json information for the current location

        Returns:  A dictionary of items in the json file
        """
        url = self.baseURL.format(apikey=self.key,
                                  location=self.location)

        try:
            logging.info("Retrieving weather report from {}".format(url))
            response = urlopen(url)
        except Exception as e:
            logging.warning("Error occurred during processing.  Check"
                            " your API key")
            logging.warning(e)
            #traceback.print_exc()
            exit(1)

        theString = response.read().decode('utf-8')
        theDict = json.loads(theString)
        logging.debug("json response received:\n{}"
                      .format(
                          json.dumps(json.loads(theString),
                                     sort_keys=True,
                                     indent=4 * ' ')))

        if 'cod' in theDict and theDict['cod'] != 200:
            print("Error, openweathermap reports: {}"
                  .format(theDict['cod']))
            return(False)
        return (theDict)

    def PrintReport(self, theWeather):
        """Generate  a report of the current weather using 'interesting' json fields

        theWeather : a dictionary of weather items (derived from the
        json report)

        Args:
            theWeather: A dictionary containing a JSON representation
            of the current weather.
        Returns: None

        """

        logging.info("Generating report")
        for x in self.fields:
            theThing = theWeather
            for y in x[0]:
                theThing = (None if not theThing or y not in theThing
                            else theThing[y])
            theThing = x[4](theThing) if theThing else "N/A"
            if theThing:
                if (not x[3] or
                    ((self.imperial and (x[3] == 'imperial')) or
                     ((not self.imperial) and (x[3] == 'metric')))):
                    print(x[1] + '{}'.format(theThing), end=x[2])
        print("Weather data by OpenWeatherMap.com\n"
              "(https://openweathermap.com)")


def main():
    from argparse import ArgumentParser
    logFormat = ('%(relativeCreated)6dmS (%(threadName)s) %'
                 '(levelname)s : %(message)s')

    # variable theParameters defines the command line options, where
    # and how their data are stored, and define the relation
    # of the command line parameters to things that are stored in
    # persistent storage.
    #
    #    Element 0 is the member name in the ArgumentPaser object,
    #    element 1 is the action,
    #    element 2 is the short option name,
    #    element 3 is the long option name,
    #    element 4 is the key name for storage in theShelve
    #              (None implies the value is not stored)
    #    element 5 is the help string, and
    #    element 6 is the error message if the element is not set
    #              (None implies it is not required)

    theParameters = (
          ('verbose', 'store_true', '-v', '--verbose', None,
           "Generate information", None),
          ('debug', 'store_true', '-d', '--debug', None,
           "Generate debugging information", None),
          ('api_key', 'store', '-k', '--key', 'APIkey',
           "Set and store the API key", "API key not set"),
          ('location', 'store', '-l', '--location', 'location',
           'Set and store the location', "Location not set"),
          ('units', 'store_true', '-i', '--imperial', 'units',
           "Set and store choice of Imperial units",
           "Units not set (Imperial/Metric)"),
          ('units', 'store_false', '-m', '--metric', 'units',
           "Set and store choice of Metric units",
           "Units not set (Imperial/Metric)"),
        )

    # open the persistent storage and create any missing keys
    db = {}
    try:
        with open(PersistentStrorageFilename) as data_file:
            db = json.load(data_file)
    except:
        pass
    for x in theParameters:
        if x[4] and not x[4] in db:
            db[x[4]] = None

    # Handle all the command line nonsense.  There are six options --
    # one is to set the location, one is to set the API key, two to
    # set the unit system that is desired, and two to set verbose and
    # debug level reporting.  Defaults come from persistent storage

    description = "Fetch weather from openweathermap"
    parser = ArgumentParser(description=description)

    [parser.add_argument(x[2],
                         x[3],
                         action=x[1],
                         dest=x[0],
                         help=x[5],
                         default=db[x[4]] if x[4] else False)
     for x in theParameters]

    args = parser.parse_args()

    # Set up the log function and enable the output if the user wants it

    if (args.debug):
        logging.basicConfig(level=logging.DEBUG, format=logFormat)
    elif (args.verbose):
        logging.basicConfig(level=logging.INFO, format=logFormat)
    else:
        logging.basicConfig(level=logging.WARN, format=logFormat)

    # If anything needs to be updated in persistent storage, then do so.

    update = False
    for x in theParameters:
        if x[4] is not None and (db[x[4]] != getattr(args, x[0])):
            db[x[4]] = getattr(args, x[0])
            result = db[x[4]]
            if type(result) != str:
                result = result.__str__()
            logging.info('Persistent storage updated: %s set to %s' %
                         (x[4], result))
            update = True
    if update:
        with open(PersistentStrorageFilename, mode='w') as data_file:
            json.dump(db, data_file)

    # If we have all the data we need, then proceed, die otherwise

    [parser.error(x[6]) for x in theParameters
     if x[4] if db[x[4]] is None]

    # Here is where the magic happens.

    weather = Weather(db['location'],
                      db['APIkey'],
                      db['units'])
    theWeather = weather.GetWeather()
    if theWeather:
        weather.PrintReport(theWeather)
    logging.info("Done")


if __name__ == "__main__":
    main()

Last edited by ewaller (2019-07-02 18:36:52)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2019-07-07 17:02:59

Strykar
Member
Registered: 2018-02-17
Posts: 50

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

Thanks, I am still working on this.
This is harder than I thought, the "200" gets incremented by 1 approximately every day (Mars sol). So the current script won't work after 24.4-ish hours.
If this will need an iterator, it's out of my league, still playing with converting it to a tuple and then being able to pull the day/sol, as Mars has 687 earth days.

Thanks for your help!

Offline

#7 2019-07-07 17:54:34

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: Migrate an existing Sopel IRC bot python module to a new JSON feed

You can get the latest available sol from the sol_keys array:

def getLatestSol(data_json):
    return data_json['sol_keys'][-1]

data=data_json[getLatestSol(data_json)]

Last edited by progandy (2019-07-07 17:58:01)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

Board footer

Powered by FluxBB