Fetches the remaining minutes and other information via command line
make PHONE_NUMBER=2065550199
all: | |
@javac $(shell find -name "*.java") | |
@java TracfoneInfo $(PHONE_NUMBER) |
import java.util.List; | |
import java.net.URL; | |
import java.net.HttpURLConnection; | |
import java.io.InputStreamReader; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.PrintStream; | |
import java.io.FileOutputStream; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashMap; | |
public class TracfoneInfo { | |
public static void main(String[] args) throws Exception { | |
if (args.length != 1) { | |
System.out.println("Phone number missing"); | |
return; | |
} | |
System.out.println(getAllInfo(args[0])); | |
String html = saveHTML("https://www.tracfone.com/direct/controller.block", | |
getCookies("https://www.tracfone.com/direct/CheckBalance") + ";TFOpinionlab=||"); | |
saveString("output.html", html); | |
if (html.contains("<strong>Service End Date :</strong>")) { | |
String expiration = html.split("<strong>Service End Date :</strong>")[1].split("</p>")[0]; | |
System.out.println("Expiration: " + expiration); | |
} | |
} | |
public static HttpURLConnection connect(String url) throws Exception { | |
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); | |
conn.setUseCaches(false); | |
return conn; | |
} | |
private static String getTransactionID(String phoneNumber) throws Exception { | |
HttpURLConnection conn = connect("https://www.fastactportal.com/static/balanceReq"); | |
appendData(conn, "min=" + phoneNumber); | |
String html = getResponse(conn); | |
if (html.contains("transactionId: ")) { | |
return html.split("transactionId: ")[1].split(" ")[0].trim(); | |
} | |
return ""; | |
} | |
public static int daysBetween(Date date1, Date date2) { | |
return (int) ((date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24)); | |
} | |
public static HashMap<String, Object> getAllInfo(String phoneNumber) throws Exception { | |
String cookieString = getCookies("https://www.fastactportal.com/static/balanceReq"); | |
HttpURLConnection conn = connect("https://www.fastactportal.com/static/balanceP"); | |
conn.setRequestProperty("Cookie", cookieString); | |
appendData(conn, "min=" + phoneNumber + "&transactionId=" + getTransactionID(phoneNumber)); | |
String raw = getResponse(conn); | |
HashMap<String, Object> response = new HashMap<String, Object>(); | |
boolean success = raw.contains("NOK"); | |
response.put("success", success); | |
if (!success) | |
return response; | |
String[] parts = raw.split(","); | |
response.put("minutes", Integer.parseInt(parts[1])); | |
response.put("messages", Integer.parseInt(parts[2])); | |
response.put("kb", Integer.parseInt(parts[3])); | |
Date expiration = new SimpleDateFormat("MM-dd-yyy").parse(parts[4]); | |
response.put("expiration", expiration); | |
response.put("daysleft", daysBetween(new Date(), expiration)); | |
return response; | |
} | |
public static void appendData(HttpURLConnection conn, String data) throws Exception { | |
conn.setDoOutput(true); | |
DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); | |
wr.writeBytes(data); | |
wr.close(); | |
} | |
public static String getResponse(HttpURLConnection conn) throws Exception { | |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
StringBuilder response = new StringBuilder(); | |
String line; | |
while ((line = rd.readLine()) != null) { | |
response.append(line); | |
response.append('\r'); | |
} | |
rd.close(); | |
return response.toString(); | |
} | |
public static void saveString(String filename, String data) { | |
try (PrintStream out = new PrintStream(new FileOutputStream(filename))) { | |
out.print(data); | |
} catch (Exception ex) { | |
} | |
} | |
public static String getCookies(String url) throws Exception { | |
String cookies = ""; | |
HttpURLConnection conn = connect(url); | |
List<String> fullCookies = conn.getHeaderFields().get("Set-Cookie"); | |
for (int i = 0; i < fullCookies.size(); i++) { | |
cookies += fullCookies.get(i).split(";")[0] + ";"; | |
} | |
return cookies.substring(0, cookies.length() - 1); | |
} | |
public static String saveHTML(String url, String cookies) throws Exception { | |
HttpURLConnection conn = connect(url); | |
conn.setRequestProperty("Cookie", cookies); | |
appendData(conn, | |
"__blockname=direct.balance.landing&__returnTo=&facebookemail=&emailSelected=&input_phone=3302121759&input_esn=5464&emailModal=&passwordModal=&balance_inquiry_button_selection=submit"); | |
return getResponse(conn); | |
} | |
} |