Android Open Source - VirginMobileMinutesChecker Website Scraper






From Project

Back to project page VirginMobileMinutesChecker.

License

The source code is released under:

Copyright 2011 Jay Goel. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Red...

If you think the Android project VirginMobileMinutesChecker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jaygoel.virginminuteschecker;
//w  w  w  . j a va 2 s  . co  m
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

public class WebsiteScraper {

   public static String fetchScreen(String username, String password) {
      String line = "";

      try {
         TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
               public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return null;
               }
               public void checkClientTrusted(
                     java.security.cert.X509Certificate[] certs, String authType) {
               }
               public void checkServerTrusted(
                     java.security.cert.X509Certificate[] certs, String authType) {
               }
            }
         };

         String url = "https://www1.virginmobileusa.com/login/login.do";    
         //String url = "https://www1.virginmobileusa.com/login/login.do";    
         //   String url = "https://www1.virginmobileusa.com/myaccount/home.do";



         try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
         } catch (Exception e) {
            e.getMessage();
         }

         //HttpsURLConnection.setFollowRedirects(true);

         HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
         ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());

         //connection.setFollowRedirects(true);


         connection.setDoOutput(true);

         // try {
         //Thread.sleep(5000);
         OutputStreamWriter out = new OutputStreamWriter(
               connection.getOutputStream());
         out.write("loginRoutingInfo=&min=" + username + "&vkey=" + password + "&submit=submit");
         out.close();
         //} catch (IOException e) {
         //   e.printStackTrace();
         // }


         //connection.connect();

         InputStreamReader in = new InputStreamReader((InputStream) connection.getContent());

         BufferedReader buff = new BufferedReader(in);

         StringBuilder sb = new StringBuilder();

         while ((line = buff.readLine()) != null) {
            sb.append(line);
         }

         int mainContentIndex = sb.indexOf("id=\"mainContent\"");
         if (mainContentIndex == -1) {
            line = "";
         } else {
            line = sb.substring(mainContentIndex);
         }

         connection.disconnect();
      } catch (Exception e) {
         e.printStackTrace();
         //System.err.println("exception 83");
         //System.err.println(e.getMessage());
         //System.err.println(line);
         return line;
         //rc.put("isValid", "FALSE");
      }
      //line = null;
      if (line == null) {
         line = "";
      }
      //System.err.println(line);
      return line;
   }


   public static Map<String, String> parseInfo(String line) {
      Map<String, String> rc = new HashMap<String, String>();

      if (line == null) {
         rc.put("isValid", "FALSE");
         return rc;
      }


      String srch;
      int start;
      int end;

      srch = "<p class=\"tel\">";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      if (start < 0) {
         rc.put("isValid", "FALSE");
         return rc;
      } else {
         rc.put("isValid", "TRUE");
      }

      //         virginInfo.append("Phone Number: ");
      //         virginInfo.append(line.substring(start + srch.length(), end));
      //         virginInfo.append("\n");

      rc.put("Phone Number", line.substring(start + srch.length(), end));

      srch = "<h3>Monthly Charge</h3><p>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      //         virginInfo.append("Monthly Charge: ");          
      //         virginInfo.append(line.substring(start + srch.length(), end));
      //         virginInfo.append("\n");

      rc.put("Monthly Charge", line.substring(start + srch.length(), end));

      srch = "<h3>Current Balance</h3><p>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      //         virginInfo.append("Current Balance: ");
      //         virginInfo.append(line.substring(start + srch.length(), end));
      //         virginInfo.append("\n");

      rc.put("Current Balance", line.substring(start + srch.length(), end));

      srch = "<h3>Min. Amount Due</h3><p>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      //virginInfo.append("Amount Due: ");
      //virginInfo.append(line.substring(start + srch.length(), end));
      //virginInfo.append("\n");

      if ((start > 0) && (end > 0)) {
         rc.put("Amount Due", line.substring(start + srch.length(), end));
      }
      // srch = "<h3>Date Due</h3><p>";
      // start = line.indexOf(srch);
      // end = line.indexOf("</p>", start);

      //         virginInfo.append("Due Date: ");
      //         virginInfo.append(line.substring(start + srch.length(), end));
      //         virginInfo.append("\n");

      //if ((start > 0) && (end > 0)) {
      //       rc.put("Date Due", line.substring(start + srch.length(), end));
      // }

      srch = "<h3>Charge Will be deducted on</h3><p>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      //         virginInfo.append("Due Date: ");
      //         virginInfo.append(line.substring(start + srch.length(), end));
      //         virginInfo.append("\n");

      if ((start > 0) && (end > 0)) {
         rc.put("Charge Deducted", line.substring(start + srch.length(), end));
      }

      srch = "<h3>You will be charged on</h3><p>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      if ((start > 0) && (end > 0)) {
         rc.put("Charged on", line.substring(start + srch.length(), end));
      }

      //     rc.put("Charged on", "02/05/11");


      srch = "<p id=\"remaining_minutes\"><strong>";
      start = line.indexOf(srch);
      end = line.indexOf("</p>", start);

      //         virginInfo.append("Minutes Used: ");
      //         virginInfo.append(line.substring(start + srch.length(), end).replaceFirst("</strong>", ""));
      //         virginInfo.append("\n");

      rc.put("Minutes Used", line.substring(start + srch.length(), end).replaceFirst("</strong>", ""));

      // rc.put("info", virginInfo.toString());
      return rc;
   }

   public static Map<String, String> getInfo(String username, String password) {

      String line = fetchScreen(username, password);
      // Log.d("DEBUG", "Line: "+line);


      return parseInfo(line);

   }

}




Java Source Code List

.MinutesBarGraph.java
com.baker.vm.PreferencesUtil.java
com.baker.vm.ScraperUtil.java
com.baker.vm.UsernamePassword.java
com.baker.vm.VMAccount.java
com.baker.vm.service.NotifyRemainingMinutes.java
com.baker.vm.ui.AboutActivity.java
com.baker.vm.ui.FetchAccountTask.java
com.baker.vm.ui.MinutesGraphDrawable.java
com.baker.vm.ui.MinutesPieGraphDrawable.java
com.baker.vm.ui.MultipleAccountsActivity.java
com.baker.vm.widget.PieGraphWidget.java
com.baker.vm.widget.Simple2x1Widget.java
com.baker.vm.widget.WidgetUtil.java
com.drfloob.VirginMobileMinutesChecker.call_trigger.CallEventReceiver.java
com.drfloob.VirginMobileMinutesChecker.call_trigger.MinutesService.java
com.jaygoel.virginminuteschecker.IVMCScraper.java
com.jaygoel.virginminuteschecker.MinutesChecker.java
com.jaygoel.virginminuteschecker.Preferences.java
com.jaygoel.virginminuteschecker.ReferenceScraper.java
com.jaygoel.virginminuteschecker.UpdateService.java
com.jaygoel.virginminuteschecker.ViewMinutes.java
com.jaygoel.virginminuteschecker.WebsiteScraper.java
com.jaygoel.virginminuteschecker.Widget.java