2016-02-15 15:28:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-09 13:47:49 +00:00
|
|
|
"fmt"
|
2016-02-15 15:28:41 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-04-03 10:07:06 +00:00
|
|
|
"github.com/hamcha/tg"
|
2016-02-15 15:28:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const viaggiurl = "http://free.rome2rio.com/api/1.2/json/Search?key=X5JMLHNc&languageCode=IT¤cyCode=EUR"
|
|
|
|
|
|
|
|
var reg *regexp.Regexp
|
|
|
|
|
2018-05-24 15:48:35 +00:00
|
|
|
func viaggi_init() {
|
2016-02-15 15:28:41 +00:00
|
|
|
reg = regexp.MustCompile("([^-]+) -> (.+)")
|
|
|
|
}
|
|
|
|
|
2018-05-24 15:48:35 +00:00
|
|
|
func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
|
2016-02-15 15:28:41 +00:00
|
|
|
if isCommand(update, "viaggi") {
|
|
|
|
parts := strings.SplitN(*(update.Text), " ", 2)
|
|
|
|
if len(parts) < 2 {
|
2018-06-18 17:26:22 +00:00
|
|
|
viaggi_help(broker, update)
|
2016-02-15 15:28:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
text := parts[1]
|
|
|
|
msgs := reg.FindStringSubmatch(text)
|
|
|
|
if len(msgs) <= 2 {
|
2018-06-18 17:26:22 +00:00
|
|
|
viaggi_help(broker, update)
|
2016-02-15 15:28:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:48:14 +00:00
|
|
|
msgs[1] = strings.Replace(msgs[1], ",", "", -1)
|
|
|
|
msgs[1] = strings.Replace(msgs[1], " ", "-", -1)
|
|
|
|
msgs[2] = strings.Replace(msgs[2], ",", "", -1)
|
|
|
|
msgs[2] = strings.Replace(msgs[2], " ", "-", -1)
|
2016-02-15 15:28:41 +00:00
|
|
|
|
2017-05-09 13:47:49 +00:00
|
|
|
out, err := viaggiAPI(msgs[1], msgs[2])
|
2016-02-15 15:28:41 +00:00
|
|
|
if err != nil {
|
2017-05-09 13:47:49 +00:00
|
|
|
log.Println("[viaggi] " + err.Error())
|
2016-02-15 15:28:41 +00:00
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &update.MessageID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:47:49 +00:00
|
|
|
broker.SendTextMessage(update.Chat, out, &update.MessageID)
|
2016-02-15 15:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:26:22 +00:00
|
|
|
func viaggi_help(broker *tg.Broker, update tg.APIMessage) {
|
|
|
|
broker.SendTextMessage(update.Chat, "Formato: /viaggi <i><PARTENZA></i> -> <i><DESTINAZIONE></i>", &update.MessageID)
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:28:41 +00:00
|
|
|
func parseData(route Romeroute) string {
|
|
|
|
// Get time
|
|
|
|
minutes := int(route.Duration)
|
|
|
|
hours := minutes / 60
|
|
|
|
minutes -= hours * 60
|
|
|
|
days := hours / 24
|
|
|
|
hours -= days * 24
|
|
|
|
timestamp := ""
|
|
|
|
if days > 0 {
|
|
|
|
timestamp += strconv.Itoa(days) + "d "
|
|
|
|
}
|
|
|
|
if hours > 0 {
|
|
|
|
timestamp += strconv.Itoa(hours) + "h "
|
|
|
|
}
|
|
|
|
if minutes > 0 {
|
|
|
|
timestamp += strconv.Itoa(minutes) + "m"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Itoa(int(route.IndicativePrice.Price)) + " " + route.IndicativePrice.Currency + " - " + strconv.Itoa(int(route.Distance)) + " Km - " + timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
type Romeplace struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Romeprice struct {
|
|
|
|
Price float64
|
|
|
|
Currency string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Romeroute struct {
|
|
|
|
Name string
|
|
|
|
Distance float64
|
|
|
|
Duration float64
|
|
|
|
IndicativePrice Romeprice
|
|
|
|
}
|
|
|
|
|
|
|
|
type Romejson struct {
|
|
|
|
Places []Romeplace
|
|
|
|
Routes []Romeroute
|
|
|
|
}
|
2017-05-09 13:47:49 +00:00
|
|
|
|
|
|
|
func viaggiAPI(from string, to string) (string, error) {
|
|
|
|
src := url.QueryEscape(from)
|
|
|
|
dst := url.QueryEscape(to)
|
|
|
|
url := viaggiurl + "&oName=" + src + "&dName=" + dst
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("GET error: %s", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var outjson Romejson
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&outjson)
|
|
|
|
if err != nil {
|
|
|
|
return "Hm, Rome2rio non ha trovato nulla, mi spiace :(\nForse non hai scritto bene uno dei due posti?", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var moreeco Romeroute
|
|
|
|
var lesstim Romeroute
|
|
|
|
if len(outjson.Routes) < 1 {
|
|
|
|
// Should never happen
|
|
|
|
return "", fmt.Errorf("No routes found (??)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate cheapest and fastest
|
|
|
|
moreeco = outjson.Routes[0]
|
|
|
|
lesstim = outjson.Routes[0]
|
|
|
|
for _, v := range outjson.Routes {
|
|
|
|
if v.IndicativePrice.Price < moreeco.IndicativePrice.Price {
|
|
|
|
moreeco = v
|
|
|
|
}
|
|
|
|
if v.Duration < lesstim.Duration {
|
|
|
|
lesstim = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Viaggio da <b>%s</b> a <b>%s</b>\n\nPiu economico: <b>%s</b> (%s)\nPiu veloce: <b>%s</b> (%s)\n\n<a href=\"http://www.rome2rio.com/it/s/%s/%s\">Maggiori informazioni</a>", outjson.Places[0].Name, outjson.Places[1].Name, moreeco.Name, parseData(moreeco), lesstim.Name, parseData(lesstim), src, dst), nil
|
|
|
|
}
|