Created
May 2, 2012 23:57
-
-
Save fern4lvarez/2582025 to your computer and use it in GitHub Desktop.
example for getting ratings through Filmaffinity
This file contains hidden or 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
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.util.Map; | |
| import javax.xml.parsers.DocumentBuilder; | |
| import javax.xml.parsers.DocumentBuilderFactory; | |
| import javax.xml.parsers.ParserConfigurationException; | |
| import org.w3c.dom.Document; | |
| import org.xml.sax.SAXException; | |
| import org.xml.sax.XMLReader; | |
| import org.json.simple.*; | |
| import org.json.simple.parser.JSONParser; | |
| import org.json.simple.parser.ParseException; | |
| public class probar { | |
| private String getHTML(String urlToRead) { | |
| URL url; | |
| HttpURLConnection conn; | |
| BufferedReader rd; | |
| String line; | |
| String result = ""; | |
| try { | |
| url = new URL(urlToRead); | |
| conn = (HttpURLConnection) url.openConnection(); | |
| conn.setRequestMethod("GET"); | |
| rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
| while ((line = rd.readLine()) != null) { | |
| result += line; | |
| } | |
| rd.close(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return result; | |
| } | |
| private String remove1(String input) { | |
| // Cadena de caracteres original a sustituir. | |
| String original = "áàäéèëíìïóòöúùuñÁÀÄÉÈËÍÌÏÓÒÖÚÙÜÑçÇ"; | |
| // Cadena de caracteres ASCII que reemplazarán los originales. | |
| String ascii = "aaaeeeiiiooouuunAAAEEEIIIOOOUUUNcC"; | |
| String output = input; | |
| for (int i=0; i<original.length(); i++) { | |
| // Reemplazamos los caracteres especiales. | |
| output = output.replace(original.charAt(i), ascii.charAt(i)); | |
| }//for i | |
| return output.replace(' ', '+'); | |
| }//remove1 | |
| public static void main(String args[]) throws MalformedURLException, | |
| SAXException, IOException, ParserConfigurationException, | |
| ParseException { | |
| probar c = new probar(); | |
| String pelis[] = new String[10]; | |
| pelis[0] = "Contagio"; | |
| pelis[1] = "El sueño de Iván"; | |
| pelis[2] = "Enredados"; | |
| pelis[3] = "Fucsia, la pequeña bruja"; | |
| pelis[4] = "Johnny English returns"; | |
| pelis[5] = "La cosa (The thing)"; | |
| String peli; | |
| for (int i = 0; i<6; i++){ | |
| peli = c.remove1(pelis[i]); | |
| System.out.println("http://www.script.nixiweb.com/filmaffinity/?d1="+peli+"&json=1"); | |
| String response = c | |
| .getHTML("http://www.script.nixiweb.com/filmaffinity/?d1="+peli+"&json=1"); | |
| System.out.println(response); | |
| System.out.println("{\"criticas\":null}"); | |
| if (response.equals("{\"criticas\":null}")) | |
| System.out.println(5); | |
| else{ | |
| JSONParser parser = new JSONParser(); | |
| Object res = parser.parse(response); | |
| JSONObject jsonObject = (JSONObject) res; | |
| String name = (String) jsonObject.get("rating"); | |
| name = name.replace(',', '.'); | |
| double cal = Double.parseDouble(name); | |
| System.out.println(name); | |
| System.out.println(cal); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment