Any way of retrieving a client's timezone? (JTS3ServerMod/JTS3ServerQuery)

Started by Sno, March 04, 2013, 09:32:06 AM

Previous topic - Next topic

Sno

Howdy,
I've heavily extended  JTS3ServerMod with a bunch of  features. One of  them  reports clan battle schedules for the game "World of tanks," and I was wondering if it's possible to find out what timezone a client is in? So when the clients send it a !battles command it can adjust the battle times to match their time zone in the response. It also pushes notifications to clients when the battle schedule changes and it'd be nice to have the notifications be adjusted to each client's timezone.

I've figured out how  to get what country a client is in by using eventInfo.get("invokerid") to lookup a client in a list created by:Vector<HashMap<String,String>> clientList = queryLib.getList(JTS3ServerQuery.LISTMODE_CLIENTLIST, "-uid,-country");But can't find anything about the timezone they're in.  ???

If timezone info isn't available in teamspeak, how do I retrieve a client's ip address using JTS3ServerMod/JTS3ServerQuery? I know that info is available in a normal TS client by right clicking a user and hitting "connection info." If I can get their ip address i can find thier timezone using http://ipinfodb.com/ip_location_api.php

Cheers,
8)

Stefan1200

The TS3 server don't know the timezone. Country really just knows the country, which might be also useful for you, if you have a list of country => timezone.

The  client_ip will be displayed at the client information, if the TS3 bot has the permission to see the client ip. Currently I don't know if there is also a way to see the IP at the client list.

Sno

Quote from: Stefan1200 on March 06, 2013, 08:46:00 PM
The TS3 server don't know the timezone. Country really just knows the country, which might be also useful for you, if you have a list of country => timezone.
Most of the clients on the server I run this on are in North America, so using country for timezone won't work
Quote from: Stefan1200 on March 06, 2013, 08:46:00 PM
The  client_ip will be displayed at the client information, if the TS3 bot has the permission to see the client ip. Currently I don't know if there is also a way to see the IP at the client list.
Awesome, figured it out, now just need to make a class that retrieves timezones from geo locations obtained from an ip lookup

thanks, I keep figuring out ways  to use JTS3ServerMod/JTS3ServerQuery to automate some seriously cool stuff

btw i've figured out how to add things like a dice roller, joke teller, and a bunch of other stuff. it's almost as functional as a good mIrc bot (almost got trivia working). With plans i have it won't be long before i'll need it to utilize an sqlite or mysql db

Sno

In case you need it:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;
import java.util.Vector;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import de.stefan1200.jts3serverquery.JTS3ServerQuery;

public class JTS3TimeZone {
private boolean DEBUG;
JTS3ServerQuery queryLib;
HashMap<String, String> clientInfo;
HashMap<Integer, TimeZone> clientZone;
// If you don't have an API_KEY yet, you can get one for free by registering at http://www.ipinfodb.com/register.php.
private final String apiKey = ;

public JTS3TimeZone(
JTS3ServerQuery queryLib) {
clientZone = new HashMap<Integer, TimeZone>();
this.queryLib = queryLib;
// DEBUG = true;
}

public TimeZone getTimeZone(Integer clientID) {
TimeZone result;
if (clientZone.containsKey(clientID)) {
result = clientZone.get(clientID);
} else {
result = getTimeZone(getIpAddress(clientID));
clientZone.put(clientID, result);
}
return getTimeZone(getIpAddress(clientID));
}

public TimeZone getTimeZone(String ipAddress) {
TimeZone result = null;
URL localURL;
try {
localURL = new URL("http://api.ipinfodb.com/v3/ip-city/?format=json&key=" + apiKey + "&ip=" + ipAddress);
URLConnection localURLConnection = localURL.openConnection();
localURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
localURLConnection.setRequestProperty("Accept-Language", "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3");
localURLConnection.setRequestProperty("Accept-Encoding", "paco");
localURLConnection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
localURLConnection.setRequestProperty("Connection", "close");
localURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
localURLConnection.setConnectTimeout(10000);

BufferedReader clanBufferedReader = null;
StringBuilder data = new StringBuilder(3000);
try {
clanBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream(), "UTF8"));
for (String line; (line = clanBufferedReader.readLine()) != null; data.append(line.replace(" ", "").replace("\t", "")))
;
} finally {
if (clanBufferedReader != null)
clanBufferedReader.close();
}
if (DEBUG)
System.out.println(data);
JsonParser jsonParser = new JsonParser();
JsonObject json = jsonParser.parse(data.toString()).getAsJsonObject();

if (!"ok".equalsIgnoreCase(json.get("statusCode").getAsString())) {
throw new Exception("Error in API status: " + json.get("statusMessage").getAsString());
}

result = TimeZone.getTimeZone("GMT" + json.get("timeZone").getAsString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

private String getIpAddress(int clientID) {
return queryLib.getInfo(JTS3ServerQuery.INFOMODE_CLIENTINFO, clientID).get("connection_client_ip");
}
}