Use constants for URL's, use Soup methods to build the request-string

openweathermap_3.6
Jens Lody 11 years ago
parent e12a050363
commit 2d0e21dab2
  1. 36
      src/extension.js
  2. 29
      src/prefs.js

@ -72,6 +72,11 @@ const WEATHER_REFRESH_INTERVAL_FORECAST = 'refresh-interval-forecast';
const WEATHER_CENTER_FORECAST_KEY = 'center-forecast';
const WEATHER_DAYS_FORECAST = 'days-forecast';
//URL
const WEATHER_URL_BASE = 'http://api.openweathermap.org/data/2.5/';
const WEATHER_URL_CURRENT = WEATHER_URL_BASE + 'weather';
const WEATHER_URL_FORECAST = WEATHER_URL_BASE + 'forecast/daily';
// Keep enums in sync with GSettings schemas
const WeatherUnits = {
CELSIUS: 0,
@ -626,7 +631,11 @@ const WeatherMenuButton = new Lang.Class({
for (let a in cities) {
if (!this.extractCity(cities[a])) {
this.load_json_async(encodeURI('http://api.openweathermap.org/data/2.5/weather?q=' + cities[a] + '&type=like'), function() {
let params = {
q: cities[a],
type: 'like'
};
this.load_json_async(WEATHER_URL_CURRENT, params, function() {
let city = arguments[0];
if (Number(city.cod) != 200)
@ -691,14 +700,6 @@ const WeatherMenuButton = new Lang.Class({
return '\u00B0\C';
},
get_weather_current_url: function() {
return encodeURI('http://api.openweathermap.org/data/2.5/weather?q=' + this.extractCity(this._city) + '&units=metric');
},
get_weather_forecast_url: function() {
return encodeURI('http://api.openweathermap.org/data/2.5/forecast/daily?q=' + this.extractCity(this._city) + '&units=metric&cnt=10');
},
get_weather_icon: function(code) {
// see http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
// fallback icons are: weather-clear-night weather-clear weather-few-clouds-night weather-few-clouds weather-fog weather-overcast weather-severe-alert weather-showers weather-showers-scattered weather-snow weather-storm
@ -1048,10 +1049,10 @@ weather-storm.png = weather-storm-symbolic.svg
return icon_name;
},
load_json_async: function(url, fun) {
load_json_async: function(url, params, fun) {
let here = this;
let message = Soup.Message.new('GET', url);
let message = Soup.form_request_new_from_hash('GET', url, params);
_httpSession.queue_message(message, function(_httpSession, message) {
if (!message.response_body.data) {
@ -1075,7 +1076,11 @@ weather-storm.png = weather-storm-symbolic.svg
this.updateCities();
return 0;
}
this.load_json_async(this.get_weather_current_url(), function(json) {
let params = {
q: this.extractCity(this._city),
units: 'metric'
};
this.load_json_async(WEATHER_URL_CURRENT, params, function(json) {
if (!json)
return 0;
@ -1324,7 +1329,12 @@ weather-storm.png = weather-storm-symbolic.svg
return 0;
}
this.load_json_async(this.get_weather_forecast_url(), function(json) {
let params = {
q: this.extractCity(this._city),
units: 'metric'
};
this.load_json_async(WEATHER_URL_FORECAST, params, function(json) {
if (!json)
return 0;

@ -58,6 +58,11 @@ const WEATHER_REFRESH_INTERVAL_FORECAST = 'refresh-interval-forecast';
const WEATHER_CENTER_FORECAST_KEY = 'center-forecast';
const WEATHER_DAYS_FORECAST = 'days-forecast';
//URL
const WEATHER_URL_BASE = 'http://api.openweathermap.org/data/2.5/';
const WEATHER_URL_CURRENT = WEATHER_URL_BASE + 'weather';
const WEATHER_URL_FIND = WEATHER_URL_BASE + 'find';
// Soup session (see https://bugzilla.gnome.org/show_bug.cgi?id=661323#c64) (Simon Legner)
const _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
@ -315,7 +320,9 @@ const WeatherPrefsWidget = new GObject.Class({
if (!id)
return 0;
that.loadJsonAsync(encodeURI('http://api.openweathermap.org/data/2.5/weather?id=' + id), function() {
that.loadJsonAsync(WEATHER_URL_CURRENT, {
id: id
}, function() {
d.sensitive = 0;
if (typeof arguments[0] == "undefined")
return 0;
@ -336,8 +343,15 @@ const WeatherPrefsWidget = new GObject.Class({
let searchLocation = function() {
let location = entry.get_text();
let params = {
cnt: '30',
sort: 'population',
type: 'like',
units: 'metric',
q: location
};
if (testLocation(location) == 0)
that.loadJsonAsync(encodeURI('http://api.openweathermap.org/data/2.5/find?cnt=30&sort=population&type=like&units=metric&q=' + location), function() {
that.loadJsonAsync(WEATHER_URL_FIND, params, function() {
if (!arguments[0])
return 0;
let city = arguments[0];
@ -396,7 +410,11 @@ const WeatherPrefsWidget = new GObject.Class({
if (!name)
return 0;
that.loadJsonAsync(encodeURI('http://api.openweathermap.org/data/2.5/weather?q=' + name + '&type=accurate'), function() {
let params = {
q: name,
type: 'accurate'
};
that.loadJsonAsync(WEATHER_URL_CURRENT, params, function() {
if (!arguments[0])
return 0;
let city = arguments[0];
@ -493,9 +511,10 @@ const WeatherPrefsWidget = new GObject.Class({
this.treeview.get_selection().select_path(path);
},
loadJsonAsync: function(url, fun, id) {
loadJsonAsync: function(url, params, fun, id) {
let here = this;
let message = Soup.Message.new('GET', url);
let message = Soup.form_request_new_from_hash('GET', url, params);
if (typeof this.asyncSession == "undefined")
this.asyncSession = {};

Loading…
Cancel
Save