Created
January 9, 2024 10:56
-
-
Save SinTeZWh1te/38dd43fcd2d35f9322bd337d1b750ea7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.test.bgbilling.kernel.scripts.global.tv; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import ru.bitel.bgbilling.common.BGException; | |
import ru.bitel.bgbilling.kernel.container.managed.ServerContext; | |
import ru.bitel.bgbilling.kernel.script.server.dev.GlobalScriptBase; | |
import ru.bitel.bgbilling.modules.tv.dyn.JsonClient; | |
import ru.bitel.bgbilling.modules.tv.dyn.tv24h.Tv24hConf; | |
import ru.bitel.bgbilling.server.util.Setup; | |
import ru.bitel.common.sql.ConnectionSet; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.sql.Connection; | |
import java.time.ZoneId; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.DateTimeParseException; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* @author sintezwh1te | |
*/ | |
public class SyncWithTV24 | |
extends GlobalScriptBase { | |
private static final Logger logger = LogManager.getLogger(); | |
private static final String dateTimePattern2 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; | |
private final DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern(dateTimePattern2).withZone(ZoneId.of("UTC")); | |
private static final String token = "TOKEN"; | |
JsonClient jsonClient = null; | |
@Override | |
public void execute(Setup setup, ConnectionSet connectionSet) | |
throws Exception { | |
Connection con = connectionSet.getConnection(); | |
ServerContext serverContext = ServerContext.get(); | |
Tv24hConf conf = serverContext.getSetup().getConfig(6, Tv24hConf.class); | |
final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")); | |
try { | |
jsonClient = new JsonClient(new URL(conf.providerURL), null, null); | |
int offset = 0; | |
while (true) { | |
JSONArray resp = invokeAndGetArray(JsonClient.Method.get, "/v2/users?subscriptions=active&limit=100&offset=" + offset, null); | |
if (resp != null && !resp.isEmpty()) { | |
for (int i = 0; i < resp.length(); i++) { | |
JSONObject account = resp.getJSONObject(i); | |
JSONArray subscriptions = account.optJSONArray("subscriptions"); | |
if (subscriptions != null) { | |
for (int j = 0; j < subscriptions.length(); j++) { | |
JSONObject subscription = subscriptions.getJSONObject(j); | |
ZonedDateTime endAt = parseTime(subscription.getString("end_at")); | |
if (now.isBefore(endAt)) { | |
print("account: " + account.optString("username") + "; packetId: " + subscription.getJSONObject("packet").getInt("id")); | |
} | |
} | |
} | |
} | |
offset = offset + 100; | |
} else { | |
break; | |
} | |
} | |
} finally { | |
if (jsonClient != null) { | |
jsonClient.disconnect(); | |
} | |
} | |
} | |
private JSONArray invokeAndGetArray(JsonClient.Method method, String resource, Object obj) | |
throws IOException, BGException, JSONException { | |
Map<String, String> requestOptions = new HashMap<>(); | |
requestOptions.put("Content-Type", "application/json"); | |
requestOptions.put("Accept", "application/json"); | |
if (!resource.contains("?")) { | |
resource += "?token=" + token; | |
} else { | |
resource += "&token=" + token; | |
} | |
try { | |
return jsonClient.invokeAndGetArray(method, requestOptions, resource, null, obj); | |
} catch (JsonClient.JsonClientException ex) { | |
String req = obj == null ? "null" : obj.toString(); | |
logger.error("INVOKE Error metod=>" + method.toString() + ",resource=>" + resource + ",req=>" | |
+ req + ", respose=>" + ex.getData()); | |
throw ex; | |
} | |
} | |
private ZonedDateTime parseTime(String time) { | |
try { | |
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(time, ZonedDateTime::from); | |
} catch (DateTimeParseException ex) { | |
try { | |
return dateTimeFormatter2.parse(time, ZonedDateTime::from); | |
} catch (DateTimeParseException ex2) { | |
try { | |
return dateTimeFormatter2.parse(time, ZonedDateTime::from); | |
} catch (DateTimeParseException ex3) { | |
throw ex3; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment