comparison 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
comparison
equal deleted inserted replaced
2:2897af43ccc6 3:cf06b77a8bbd
1 package econnect.wp3_3.server;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5 import java.net.URLConnection;
6 import java.net.URL;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.Locale;
11
12 import javax.xml.parsers.*;
13 import org.xml.sax.InputSource;
14 import org.w3c.dom.*;
15 import java.io.*;
16
17 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
18
19 import econnect.wp3_3.client.services.FlickrService;
20 import econnect.wp3_3.shared.DataElement;
21
22 public class FlickrServiceImpl extends RemoteServiceServlet implements FlickrService {
23
24 private String key="3f0e901a712e4a1e4143a3be6fa8131e";
25 private String server="http://api.flickr.com/services/rest/";
26 private String searchMethod = "flickr.photos.search";
27 private String placeIdMethod = "flickr.places.getInfo";
28
29 private Document getXMLFromConnection(String connection){
30 try {
31 URLConnection uc = new URL(connection).openConnection();
32 ByteArrayOutputStream result = new ByteArrayOutputStream();
33 InputStream input = uc.getInputStream();
34 byte[] buffer = new byte[10000];
35 int amount = 0;
36 while(amount != -1){
37 result.write(buffer, 0, amount);
38 amount = input.read(buffer);
39 }
40
41 String xmlString = result.toString();
42 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
43 DocumentBuilder db = dbf.newDocumentBuilder();
44 InputSource is = new InputSource();
45 is.setCharacterStream(new StringReader(xmlString));
46
47 return db.parse(is);
48 }
49 catch(Exception e){
50 }
51 return null;
52 }
53
54 @Override
55 public DataElement[] getResultsAsKmlString(String term) {
56
57 String searchConnection = server+"?method="+searchMethod;
58 searchConnection += "&api_key="+key;
59 searchConnection += "&tags=";
60 String[] tags = term.split(" ");
61 for( int i=0; i<tags.length; i++ ){
62 searchConnection += tags[i];
63 if( i+1 < tags.length ){
64 searchConnection += ",";
65 }
66 }
67 searchConnection += "&has_geo=true";
68 searchConnection += "&tag_mode=all";
69 searchConnection += "&extras=geo,url_t,date_taken";
70
71 String placeConnection = server+"?method="+placeIdMethod+"&api_key="+key;
72
73 DataElement[] elements = new DataElement[4000];
74
75 for( int j=0; j<16; j++ ){
76 System.out.println(j);
77 try {
78 String c = searchConnection + "&page="+(j+1);
79 Document doc = getXMLFromConnection(c);
80 NodeList nodes = doc.getElementsByTagName("photo");
81
82 for (int i = 0; i < nodes.getLength(); i++) {
83 try {
84 Element element = (Element) nodes.item(i);
85 String name = element.getAttribute("title");
86 double latitude = (new Double(element.getAttribute("latitude"))).doubleValue();
87 double longitude = (new Double(element.getAttribute("longitude"))).doubleValue();
88 int granularity = 0;
89
90 String place = "";
91 String description = "<a target='_blank' href='http://flickr.com/photos/"+element.getAttribute("owner")+"/"+element.getAttribute("id")+"'><img src='"+element.getAttribute("url_t")+"'/></a>";
92
93 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
94 Date d = sdf.parse(element.getAttribute("datetaken"));
95 long millies = d.getTime();
96 String date = (new Long(millies)).toString();
97 /*
98 String time = element.getAttribute("datetaken");
99 long d = Long.parseLong(time)*1000;
100 String date = (new Long(d)).toString();
101 */
102 DataElement e = new DataElement();
103 e.setValues(name, place, latitude, longitude, granularity, date, description);
104 elements[i+250*j] = e;
105 }
106 catch( Exception e ){
107 }
108 }
109 }
110 catch(Exception e){
111 }
112
113 }
114 return elements;
115
116 }
117
118 }