You are on page 1of 4

package com.bean.

gamefaqs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.text.Html;
import android.util.Log;
public class GamefaqsParser {
final static String SITE_URI = "http://www.gamefaqs.com/";
final static String SEARCH_URI = "search/index.html?game=";
final static String SEARCH_REGEXP = "<a href=\"/(.*\\d+.*)\"\\s+>{1}\\s?
(.*)</a>";
//(.*)\\s+</td>\\s+<td>\\s+<a href=\"/(.*\\d+.*)\">{1}\\s+(.*)</a>
final static String FAQ_REGEXP = "<a href=\"/([\\w|\\d]+/[\\w|\\d|-]+/fa
qs/\\d+)\"\\s?>([\\w|/|\\s|\\(|\\)]+)</a>(.+</span>)?</td><td>([\\d{1,2}/]+)</td
><td><a href=\"[/\\w?]+feature[/\\w?]+.html\">([\\w|/|\\s|\\(|\\)]+)</a>";
final static String DOWNLOAD_REGEXP = "a href=\".*(http.*)\".*View";
final static String TOP_REGEXP = "\\d\\d\\s<a href=\"/(.*\\d+.*)\">(.*)<
/a>";
// final static String FAQ_REGEXP =
// "<a href=\"([/\\w?]+file[/\\w?]+)\"\\s?>([\\w|/|\\s]+)</a>";
static DefaultHttpClient httpClient = new DefaultHttpClient();
static HttpContext localContext = new BasicHttpContext();
static private boolean abort;
static private String ret;
static HttpResponse response = null;
static HttpGet httpGet = null;
public static void main(String[] args) {
Vector<GamefaqGame> gameList = searchForGame("Monster Hunter");
GamefaqGame game = gameList.elementAt(2);
System.out.println(game.getName());
listFaqs(game);
}
public static Vector<GamefaqGame> topGames() {
Vector<GamefaqGame> gameList = new Vector<GamefaqGame>();
final String htmlResponse = fileGetContents(SITE_URI);
if (htmlResponse==null) return gameList;
Pattern pattern = Pattern.compile(TOP_REGEXP);
Matcher matcher = pattern.matcher(htmlResponse);
int i = 0;
while (matcher.find()) {
i++;
if (i==10) break; //no more than 10
try {
gameList.add(new GamefaqGame(matcher.group(1) +
"/faqs", matcher.group(2),""));
} finally {
}
}
return gameList;
}
public static Vector<GamefaqGame> searchForGame(String searchString) {
Vector<GamefaqGame> gameList = new Vector<GamefaqGame>();
final String htmlResponse = fileGetContents(SITE_URI + SEARCH_UR
I
+ URLEncoder.encode(searchString));
if (htmlResponse==null) return gameList;
Pattern pattern = Pattern.compile(SEARCH_REGEXP);
Matcher matcher = pattern.matcher(htmlResponse);
while (matcher.find()) {
try {
String url = matcher.group(1);
String console = url.substring(0,url.indexOf("/"
)).toUpperCase();
gameList.add(new GamefaqGame(matcher.group(1) +
"/faqs", Html.fromHtml(matcher.group(2)).toString(),console));
} finally {
}
}
return gameList;
}
public static Vector<GamefaqFaq> listFaqs(GamefaqGame game) {
Vector<GamefaqFaq> faqList = new Vector<GamefaqFaq>();
final String htmlResponse = fileGetContents(SITE_URI + game.getU
rl());
Pattern pattern = Pattern.compile(FAQ_REGEXP);
Matcher matcher = pattern.matcher(htmlResponse);
while (matcher.find()) {
try {
faqList.add(new GamefaqFaq(matcher.group(1), mat
cher.group(2),
matcher.group(4), matcher.group(
5)));
} finally {
}
}
return faqList;
}
public static String getDownloadLink(String pageContent) {
String downloadLink = null;
Pattern pattern = Pattern.compile(DOWNLOAD_REGEXP);
Matcher matcher = pattern.matcher(pageContent);
while(matcher.find()) {
try {
downloadLink = URLDecoder.decode(matcher.group(1
));
} finally {
}
}
return downloadLink;
}
public static String downloadImage(String uri) {
String returnString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
returnString = httpclient.execute(httpget, responseHandl
er);
} catch (ClientProtocolException e) {
Log.e("GAMEFAQS", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("GAMEFAQS", e.getMessage());
e.printStackTrace();
}
String returnImage = null;
if (returnString.length()<4000) returnImage = getDownloadLink(re
turnString);
if (returnImage!=null) return returnImage;
return returnString;
}

public static String fileGetContents(String uri) {


String returnString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
httpget.setHeader("User-Agent","Mozilla/5.0 (Linux; U; Android 1.0; en-u
s; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/52
3.12.2");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
returnString = httpclient.execute(httpget, responseHandl
er);
} catch (ClientProtocolException e) {
Log.e("GAMEFAQS", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("GAMEFAQS", e.getMessage());
e.printStackTrace();
}
return returnString;
}
}

You might also like