Mercurial > hg > STI-GWT
diff src/econnect/wp3_3/server/FlickrServiceImpl.java @ 3:cf06b77a8bbd
Committed branch of the e4D repos sti-gwt branch 16384.
git-svn-id: http://dev.dariah.eu/svn/repos/eu.dariah.de/ap1/sti-gwt-dariah-geobrowser@36 f2b5be40-def6-11e0-8a09-b3c1cc336c6b
author | StefanFunk <StefanFunk@f2b5be40-def6-11e0-8a09-b3c1cc336c6b> |
---|---|
date | Tue, 17 Jul 2012 13:34:40 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/econnect/wp3_3/server/FlickrServiceImpl.java Tue Jul 17 13:34:40 2012 +0000 @@ -0,0 +1,118 @@ +package econnect.wp3_3.server; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.net.URLConnection; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; + +import javax.xml.parsers.*; +import org.xml.sax.InputSource; +import org.w3c.dom.*; +import java.io.*; + +import com.google.gwt.user.server.rpc.RemoteServiceServlet; + +import econnect.wp3_3.client.services.FlickrService; +import econnect.wp3_3.shared.DataElement; + +public class FlickrServiceImpl extends RemoteServiceServlet implements FlickrService { + + private String key="3f0e901a712e4a1e4143a3be6fa8131e"; + private String server="http://api.flickr.com/services/rest/"; + private String searchMethod = "flickr.photos.search"; + private String placeIdMethod = "flickr.places.getInfo"; + + private Document getXMLFromConnection(String connection){ + try { + URLConnection uc = new URL(connection).openConnection(); + ByteArrayOutputStream result = new ByteArrayOutputStream(); + InputStream input = uc.getInputStream(); + byte[] buffer = new byte[10000]; + int amount = 0; + while(amount != -1){ + result.write(buffer, 0, amount); + amount = input.read(buffer); + } + + String xmlString = result.toString(); + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader(xmlString)); + + return db.parse(is); + } + catch(Exception e){ + } + return null; + } + + @Override + public DataElement[] getResultsAsKmlString(String term) { + + String searchConnection = server+"?method="+searchMethod; + searchConnection += "&api_key="+key; + searchConnection += "&tags="; + String[] tags = term.split(" "); + for( int i=0; i<tags.length; i++ ){ + searchConnection += tags[i]; + if( i+1 < tags.length ){ + searchConnection += ","; + } + } + searchConnection += "&has_geo=true"; + searchConnection += "&tag_mode=all"; + searchConnection += "&extras=geo,url_t,date_taken"; + + String placeConnection = server+"?method="+placeIdMethod+"&api_key="+key; + + DataElement[] elements = new DataElement[4000]; + + for( int j=0; j<16; j++ ){ + System.out.println(j); + try { + String c = searchConnection + "&page="+(j+1); + Document doc = getXMLFromConnection(c); + NodeList nodes = doc.getElementsByTagName("photo"); + + for (int i = 0; i < nodes.getLength(); i++) { + try { + Element element = (Element) nodes.item(i); + String name = element.getAttribute("title"); + double latitude = (new Double(element.getAttribute("latitude"))).doubleValue(); + double longitude = (new Double(element.getAttribute("longitude"))).doubleValue(); + int granularity = 0; + + String place = ""; + String description = "<a target='_blank' href='http://flickr.com/photos/"+element.getAttribute("owner")+"/"+element.getAttribute("id")+"'><img src='"+element.getAttribute("url_t")+"'/></a>"; + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date d = sdf.parse(element.getAttribute("datetaken")); + long millies = d.getTime(); + String date = (new Long(millies)).toString(); +/* + String time = element.getAttribute("datetaken"); + long d = Long.parseLong(time)*1000; + String date = (new Long(d)).toString(); +*/ + DataElement e = new DataElement(); + e.setValues(name, place, latitude, longitude, granularity, date, description); + elements[i+250*j] = e; + } + catch( Exception e ){ + } + } + } + catch(Exception e){ + } + + } + return elements; + + } + +}