First hackish attempt to use nominatim to get coordinates of locations

multiprovider
None 10 years ago
parent ae4bac972c
commit fa8a54911c
  1. 134
      src/prefs.js

@ -37,6 +37,7 @@ const _ = Gettext.gettext;
const Soup = imports.gi.Soup; const Soup = imports.gi.Soup;
const Lang = imports.lang; const Lang = imports.lang;
const Mainloop = imports.mainloop;
const ExtensionUtils = imports.misc.extensionUtils; const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(); const Me = ExtensionUtils.getCurrentExtension();
const Config = imports.misc.config; const Config = imports.misc.config;
@ -65,9 +66,9 @@ const WEATHER_DECIMAL_PLACES = 'decimal-places';
const WEATHER_OWM_API_KEY = 'appid'; const WEATHER_OWM_API_KEY = 'appid';
//URL //URL
const WEATHER_URL_BASE = 'http://api.openweathermap.org/data/2.5/'; const WEATHER_URL_BASE = 'https://open.mapquestapi.com/nominatim/v1/';
const WEATHER_URL_CURRENT = WEATHER_URL_BASE + 'weather'; const WEATHER_URL_FIND = WEATHER_URL_BASE + 'search.php';
const WEATHER_URL_FIND = WEATHER_URL_BASE + 'find'; const WEATHER_URL_REVERSE = WEATHER_URL_BASE + 'reverse.php';
let _httpSession; let _httpSession;
@ -342,15 +343,18 @@ const WeatherPrefsWidget = new GObject.Class({
entry.activates_default = true; entry.activates_default = true;
let testLocation = Lang.bind(this, function(location) { let testLocation = Lang.bind(this, function(location) {
log(new Error().lineNumber + ' : location = ' + location);
if (location.search(/\[/) == -1 || location.search(/\]/) == -1) if (location.search(/\[/) == -1 || location.search(/\]/) == -1)
return 0; return 0;
let id = location.split(/\[/)[1].split(/\]/)[0]; let coord = location.split(/\[/)[1].split(/\]/)[0];
if (!id) if (!coord)
return 0; return 0;
log(new Error().lineNumber + ' : coord = ' + coord);
this.loadJsonAsync(WEATHER_URL_CURRENT, { this.loadJsonAsync(WEATHER_URL_REVERSE, {
id: id lat: coord.split(/,/)[0],
lon: coord.split(/,/)[1]
}, function() { }, function() {
d.sensitive = 0; d.sensitive = 0;
if (arguments[0] === undefined) if (arguments[0] === undefined)
@ -358,10 +362,7 @@ const WeatherPrefsWidget = new GObject.Class({
let city = arguments[0]; let city = arguments[0];
if (Number(city.cod) != 200) if (Number(city.length) < 1)
return 0;
if (Number(city.count) === 0)
return 0; return 0;
d.sensitive = 1; d.sensitive = 1;
@ -370,60 +371,59 @@ const WeatherPrefsWidget = new GObject.Class({
return 0; return 0;
}); });
this.changeTimeout = undefined;
let searchLocation = Lang.bind(this, function() { let searchLocation = Lang.bind(this, function() {
let location = entry.get_text(); if (this.changeTimeout !== undefined)
let params = { Mainloop.source_remove(this.changeTimeout);
cnt: '30',
sort: 'population',
type: 'like',
units: 'metric',
q: location
};
if (this.appid)
params.APPID = this.appid;
if (testLocation(location) === 0)
this.loadJsonAsync(WEATHER_URL_FIND, params, function() {
if (!arguments[0])
return 0;
let city = arguments[0];
if (Number(city.cod) != 200) this.changeTimeout = Mainloop.timeout_add(2000,
return 0; Lang.bind(
this,
function() {
if (Number(city.count) > 0) let location = entry.get_text();
city = city.list; let params = {
else format: 'json',
return 0; addressdetails: '1',
q: location
};
completionModel.clear(); if (testLocation(location) === 0) {
this.loadJsonAsync(WEATHER_URL_FIND, params, function() {
if (!arguments[0])
return 0;
let city = arguments[0];
let current = this.liststore.get_iter_first(); if (Number(city.length) < 1)
return 0;
var m = {}; completionModel.clear();
for (var i in city) {
current = completionModel.append(); let current = this.liststore.get_iter_first();
let cityText = city[i].name; var m = {};
for (var i in city) {
if (city[i].sys) current = completionModel.append();
cityText += ", " + city[i].sys.country;
if (city[i].id) let cityText = city[i].display_name;
cityText += " [" + city[i].id + "]"; let cityCoord = "[" + city[i].lat + "," + city[i].lon + "]";
if (m[cityText]) if (m[cityCoord])
continue; continue;
else else
m[cityText] = 1; m[cityCoord] = 1;
completionModel.set_value(current, 0, cityText); completionModel.set_value(current, 0, cityText + " " + cityCoord);
} }
completion.complete(); completion.complete();
return 0; return 0;
}, "getInfo"); }, "getInfo");
return 0; }
return 0;
}));
}); });
entry.connect("changed", searchLocation); entry.connect("changed", searchLocation);
@ -436,39 +436,31 @@ const WeatherPrefsWidget = new GObject.Class({
if (entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1) if (entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1)
return 0; return 0;
let id = entry.get_text().split(/\[/)[1].split(/\]/)[0]; let coord = entry.get_text().split(/\[/)[1].split(/\]/)[0];
if (!id) if (!coord)
return 0; return 0;
let params = { let params = {
id: id, format: 'json',
type: 'accurate' lat: coord.split(/,/)[0],
lon: coord.split(/,/)[1]
}; };
if (this.appid) this.loadJsonAsync(WEATHER_URL_REVERSE, params, Lang.bind(this, function() {
params.APPID = this.appid;
this.loadJsonAsync(WEATHER_URL_CURRENT, params, Lang.bind(this, function() {
if (!arguments[0]) if (!arguments[0])
return 0; return 0;
let city = arguments[0]; let city = arguments[0];
if (Number(city.cod) != 200) if (Number(city.length) < 1)
return 0; return 0;
if (!id) let cityText = entry.get_text().split(/\[/)[0];
return 0;
if (id != city.id)
return 0;
let cityText = entry.get_text().split(/,/)[0];
if (city.sys)
cityText += " (" + city.sys.country + ")";
if (this.city) if (this.city)
this.city = this.city + " && " + city.id + ">" + cityText; this.city = this.city + " && " + coord + ">" + cityText;
else else
this.city = city.id + ">" + cityText; this.city = coord + ">" + cityText;
return 0; return 0;
}), "lastTest"); }), "lastTest");
@ -485,7 +477,7 @@ const WeatherPrefsWidget = new GObject.Class({
if (!city.length) if (!city.length)
return 0; return 0;
let ac = this.actual_city; let ac = this.actual_city;
let textDialog = _("Remove %s ?").replace("%s", this.extractLocation(city[ac])); let textDialog = _("Remove %s ?").format(this.extractLocation(city[ac]));
let dialog = new Gtk.Dialog({ let dialog = new Gtk.Dialog({
title: "" title: ""
}); });

Loading…
Cancel
Save