From c7c0da84ab5c9619551653ba6aa0ebafb32a104c Mon Sep 17 00:00:00 2001 From: Yusuke Yamamoto Date: Tue, 23 Nov 2010 23:04:46 +0800 Subject: [PATCH] TFJ-417 createPlace() now returns SimilarPlaces object which contains token TFJ-439 adding Geo Resources examples --- .../main/java/twitter4j/SimilarPlaces.java | 34 ++++++++ .../java/twitter4j/SimilarPlacesImpl.java | 34 ++++++++ twitter4j-examples/bin/geo/createPlace.cmd | 9 +++ twitter4j-examples/bin/geo/createPlace.sh | 7 ++ twitter4j-examples/bin/geo/getGeoDetails.cmd | 9 +++ twitter4j-examples/bin/geo/getGeoDetails.sh | 7 ++ .../bin/geo/getSimilarPlaces.cmd | 9 +++ .../bin/geo/getSimilarPlaces.sh | 7 ++ twitter4j-examples/bin/geo/reverseGeoCode.cmd | 9 +++ twitter4j-examples/bin/geo/reverseGeoCode.sh | 7 ++ twitter4j-examples/bin/geo/searchPlaces.cmd | 9 +++ twitter4j-examples/bin/geo/searchPlaces.sh | 7 ++ .../twitter4j/examples/geo/CreatePlace.java | 70 +++++++++++++++++ .../twitter4j/examples/geo/GetGeoDetails.java | 68 ++++++++++++++++ .../examples/geo/GetSimilarPlaces.java | 77 +++++++++++++++++++ .../examples/geo/ReverseGeoCode.java | 71 +++++++++++++++++ .../twitter4j/examples/geo/SearchPlaces.java | 76 ++++++++++++++++++ 17 files changed, 510 insertions(+) create mode 100644 twitter4j-core/src/main/java/twitter4j/SimilarPlaces.java create mode 100644 twitter4j-core/src/main/java/twitter4j/SimilarPlacesImpl.java create mode 100644 twitter4j-examples/bin/geo/createPlace.cmd create mode 100755 twitter4j-examples/bin/geo/createPlace.sh create mode 100644 twitter4j-examples/bin/geo/getGeoDetails.cmd create mode 100755 twitter4j-examples/bin/geo/getGeoDetails.sh create mode 100644 twitter4j-examples/bin/geo/getSimilarPlaces.cmd create mode 100755 twitter4j-examples/bin/geo/getSimilarPlaces.sh create mode 100644 twitter4j-examples/bin/geo/reverseGeoCode.cmd create mode 100755 twitter4j-examples/bin/geo/reverseGeoCode.sh create mode 100644 twitter4j-examples/bin/geo/searchPlaces.cmd create mode 100755 twitter4j-examples/bin/geo/searchPlaces.sh create mode 100644 twitter4j-examples/src/main/java/twitter4j/examples/geo/CreatePlace.java create mode 100644 twitter4j-examples/src/main/java/twitter4j/examples/geo/GetGeoDetails.java create mode 100644 twitter4j-examples/src/main/java/twitter4j/examples/geo/GetSimilarPlaces.java create mode 100644 twitter4j-examples/src/main/java/twitter4j/examples/geo/ReverseGeoCode.java create mode 100644 twitter4j-examples/src/main/java/twitter4j/examples/geo/SearchPlaces.java diff --git a/twitter4j-core/src/main/java/twitter4j/SimilarPlaces.java b/twitter4j-core/src/main/java/twitter4j/SimilarPlaces.java new file mode 100644 index 000000000..07190001b --- /dev/null +++ b/twitter4j-core/src/main/java/twitter4j/SimilarPlaces.java @@ -0,0 +1,34 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j; + +/** + * @author Yusuke Yamamoto - yusuke at mac.com + * @since Twitter4J 2.1.7 + */ +public class SimilarPlaces { +} diff --git a/twitter4j-core/src/main/java/twitter4j/SimilarPlacesImpl.java b/twitter4j-core/src/main/java/twitter4j/SimilarPlacesImpl.java new file mode 100644 index 000000000..c78774839 --- /dev/null +++ b/twitter4j-core/src/main/java/twitter4j/SimilarPlacesImpl.java @@ -0,0 +1,34 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j; + +/** + * @author Yusuke Yamamoto - yusuke at mac.com + * @since Twitter4J 2.1.7 + */ +public class SimilarPlacesImpl { +} diff --git a/twitter4j-examples/bin/geo/createPlace.cmd b/twitter4j-examples/bin/geo/createPlace.cmd new file mode 100644 index 000000000..529450a84 --- /dev/null +++ b/twitter4j-examples/bin/geo/createPlace.cmd @@ -0,0 +1,9 @@ +echo off +SETLOCAL enabledelayedexpansion +cd .. +call setEnv.cmd + +echo on +"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.geo.CreatePlace %* + +ENDLOCAL diff --git a/twitter4j-examples/bin/geo/createPlace.sh b/twitter4j-examples/bin/geo/createPlace.sh new file mode 100755 index 000000000..62d7bfee9 --- /dev/null +++ b/twitter4j-examples/bin/geo/createPlace.sh @@ -0,0 +1,7 @@ +#/bin/sh +cd .. +. ./setEnv.sh + +RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.geo.CreatePlace $@" +echo $RUN_CMD +exec $RUN_CMD diff --git a/twitter4j-examples/bin/geo/getGeoDetails.cmd b/twitter4j-examples/bin/geo/getGeoDetails.cmd new file mode 100644 index 000000000..27487c85c --- /dev/null +++ b/twitter4j-examples/bin/geo/getGeoDetails.cmd @@ -0,0 +1,9 @@ +echo off +SETLOCAL enabledelayedexpansion +cd .. +call setEnv.cmd + +echo on +"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.geo.GetGeoDetails %* + +ENDLOCAL diff --git a/twitter4j-examples/bin/geo/getGeoDetails.sh b/twitter4j-examples/bin/geo/getGeoDetails.sh new file mode 100755 index 000000000..e754201e2 --- /dev/null +++ b/twitter4j-examples/bin/geo/getGeoDetails.sh @@ -0,0 +1,7 @@ +#/bin/sh +cd .. +. ./setEnv.sh + +RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.geo.GetGeoDetails $@" +echo $RUN_CMD +exec $RUN_CMD diff --git a/twitter4j-examples/bin/geo/getSimilarPlaces.cmd b/twitter4j-examples/bin/geo/getSimilarPlaces.cmd new file mode 100644 index 000000000..bf9332b36 --- /dev/null +++ b/twitter4j-examples/bin/geo/getSimilarPlaces.cmd @@ -0,0 +1,9 @@ +echo off +SETLOCAL enabledelayedexpansion +cd .. +call setEnv.cmd + +echo on +"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.geo.GetSimilarPlaces %* + +ENDLOCAL diff --git a/twitter4j-examples/bin/geo/getSimilarPlaces.sh b/twitter4j-examples/bin/geo/getSimilarPlaces.sh new file mode 100755 index 000000000..bf9cf24cf --- /dev/null +++ b/twitter4j-examples/bin/geo/getSimilarPlaces.sh @@ -0,0 +1,7 @@ +#/bin/sh +cd .. +. ./setEnv.sh + +RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.geo.GetSimilarPlaces $@" +echo $RUN_CMD +exec $RUN_CMD diff --git a/twitter4j-examples/bin/geo/reverseGeoCode.cmd b/twitter4j-examples/bin/geo/reverseGeoCode.cmd new file mode 100644 index 000000000..00be2fa7c --- /dev/null +++ b/twitter4j-examples/bin/geo/reverseGeoCode.cmd @@ -0,0 +1,9 @@ +echo off +SETLOCAL enabledelayedexpansion +cd .. +call setEnv.cmd + +echo on +"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.geo.ReverseGeoCode %* + +ENDLOCAL diff --git a/twitter4j-examples/bin/geo/reverseGeoCode.sh b/twitter4j-examples/bin/geo/reverseGeoCode.sh new file mode 100755 index 000000000..1f059dd3e --- /dev/null +++ b/twitter4j-examples/bin/geo/reverseGeoCode.sh @@ -0,0 +1,7 @@ +#/bin/sh +cd .. +. ./setEnv.sh + +RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.geo.ReverseGeoCode $@" +echo $RUN_CMD +exec $RUN_CMD diff --git a/twitter4j-examples/bin/geo/searchPlaces.cmd b/twitter4j-examples/bin/geo/searchPlaces.cmd new file mode 100644 index 000000000..6cc1bd816 --- /dev/null +++ b/twitter4j-examples/bin/geo/searchPlaces.cmd @@ -0,0 +1,9 @@ +echo off +SETLOCAL enabledelayedexpansion +cd .. +call setEnv.cmd + +echo on +"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.geo.SearchPlaces %* + +ENDLOCAL diff --git a/twitter4j-examples/bin/geo/searchPlaces.sh b/twitter4j-examples/bin/geo/searchPlaces.sh new file mode 100755 index 000000000..28010e61e --- /dev/null +++ b/twitter4j-examples/bin/geo/searchPlaces.sh @@ -0,0 +1,7 @@ +#/bin/sh +cd .. +. ./setEnv.sh + +RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.geo.SearchPlaces $@" +echo $RUN_CMD +exec $RUN_CMD diff --git a/twitter4j-examples/src/main/java/twitter4j/examples/geo/CreatePlace.java b/twitter4j-examples/src/main/java/twitter4j/examples/geo/CreatePlace.java new file mode 100644 index 000000000..bd0aa2c8b --- /dev/null +++ b/twitter4j-examples/src/main/java/twitter4j/examples/geo/CreatePlace.java @@ -0,0 +1,70 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j.examples.geo; + +import twitter4j.GeoLocation; +import twitter4j.Place; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +/** + * Creates a place. + * + * @author Yusuke Yamamoto - yusuke at mac.com + */ +public final class CreatePlace { + /** + * Usage: java twitter4j.examples.geo.CreatePlace [name] [contained within] [token] [lat] [long] [street address] + * + * @param args message + */ + public static void main(String[] args) { + if (args.length < 5) { + System.out.println("Usage: java twitter4j.examples.geo.CreatePlace [name] [contained within] [token] [lat] [long] [street address]"); + System.exit(-1); + } + try { + Twitter twitter = new TwitterFactory().getInstance(); + String name = args[0]; + String containedWithin = args[1]; + String token = args[2]; + GeoLocation location = new GeoLocation(Double.parseDouble(args[3]), Double.parseDouble(args[4])); + String streetAddress = null; + if (args.length >= 5) { + streetAddress = args[5]; + } + Place place = twitter.createPlace(name, containedWithin, token, location, streetAddress); + System.out.println("Successfully created a place [" + place.toString() + "]."); + System.exit(0); + } catch (TwitterException te) { + te.printStackTrace(); + System.out.println("Failed to create a place: " + te.getMessage()); + System.exit(-1); + } + } +} diff --git a/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetGeoDetails.java b/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetGeoDetails.java new file mode 100644 index 000000000..68e46c1d0 --- /dev/null +++ b/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetGeoDetails.java @@ -0,0 +1,68 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j.examples.geo; + +import twitter4j.Place; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +/** + * Shows specified place's detailed information + * Destroys specified status. + * + * @author Yusuke Yamamoto - yusuke at mac.com + */ +public final class GetGeoDetails { + /** + * Usage: java twitter4j.examples.geo.GetGeoDetails [place id] + * + * @param args message + */ + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Usage: java twitter4j.examples.geo.GetGeoDetails [place id]"); + System.exit(-1); + } + try { + Twitter twitter = new TwitterFactory().getInstance(); + Place place = twitter.getGeoDetails(args[0]); + System.out.println("name: " + place.getName()); + System.out.println("country: " + place.getCountry()); + System.out.println("country code: " + place.getCountryCode()); + System.out.println("full name: " + place.getFullName()); + System.out.println("id: " + place.getId()); + System.out.println("place type: " + place.getPlaceType()); + System.out.println("street address: " + place.getStreetAddress()); + System.exit(0); + } catch (TwitterException te) { + te.printStackTrace(); + System.out.println("Failed to retrieve geo details: " + te.getMessage()); + System.exit(-1); + } + } +} diff --git a/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetSimilarPlaces.java b/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetSimilarPlaces.java new file mode 100644 index 000000000..e7b79f9ae --- /dev/null +++ b/twitter4j-examples/src/main/java/twitter4j/examples/geo/GetSimilarPlaces.java @@ -0,0 +1,77 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j.examples.geo; + +import twitter4j.GeoLocation; +import twitter4j.Place; +import twitter4j.ResponseList; +import twitter4j.SimilarPlaces; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +/** + * Locates places near the given coordinates which are similar in name. + * + * @author Yusuke Yamamoto - yusuke at mac.com + */ +public final class GetSimilarPlaces { + /** + * Usage: java twitter4j.examples.geo.GetSimilarPlaces [latitude] [longitude] [place id] + * + * @param args message + */ + public static void main(String[] args) { + if (args.length < 3) { + System.out.println("Usage: java twitter4j.examples.geo.GetSimilarPlaces [latitude] [longitude] [name] [place id]"); + System.exit(-1); + } + try { + Twitter twitter = new TwitterFactory().getInstance(); + GeoLocation location = new GeoLocation(Double.parseDouble(args[0]), Double.parseDouble(args[1])); + String name = args[2]; + String containedWithin = null; + if (args.length >= 4) { + containedWithin = args[3]; + } + SimilarPlaces places = twitter.getSimilarPlaces(location, name, containedWithin, null); + System.out.println("token: "+ places.getToken()); + if (places.size() == 0) { + System.out.println("No location associated with the specified condition"); + } else { + for (Place place : places) { + System.out.println("id: " + place.getId() + " name: " + place.getFullName() + " name: " + place.getFullName()); + } + } + System.exit(0); + } catch (TwitterException te) { + te.printStackTrace(); + System.out.println("Failed to find similar places: " + te.getMessage()); + System.exit(-1); + } + } +} diff --git a/twitter4j-examples/src/main/java/twitter4j/examples/geo/ReverseGeoCode.java b/twitter4j-examples/src/main/java/twitter4j/examples/geo/ReverseGeoCode.java new file mode 100644 index 000000000..3fb48e592 --- /dev/null +++ b/twitter4j-examples/src/main/java/twitter4j/examples/geo/ReverseGeoCode.java @@ -0,0 +1,71 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j.examples.geo; + +import twitter4j.GeoLocation; +import twitter4j.GeoQuery; +import twitter4j.Place; +import twitter4j.ResponseList; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +/** + * Given a latitude and a longitude, searches for up to 20 places that can be used as a place_id when updating a status. + * + * @author Yusuke Yamamoto - yusuke at mac.com + */ +public final class ReverseGeoCode { + /** + * Usage: java twitter4j.examples.geo.ReverseGeoCode [latitude] [longitude] + * + * @param args message + */ + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("Usage: java twitter4j.examples.geo.ReverseGeoCode [latitude] [longitude]"); + System.exit(-1); + } + try { + Twitter twitter = new TwitterFactory().getInstance(); + GeoQuery query = new GeoQuery(new GeoLocation(Double.parseDouble(args[0]), Double.parseDouble(args[1]))); + ResponseList places = twitter.reverseGeoCode(query); + if (places.size() == 0) { + System.out.println("No location associated with the specified lat/lang"); + } else { + for (Place place : places) { + System.out.println("id: " + place.getId() + " name: " + place.getFullName()); + } + } + System.exit(0); + } catch (TwitterException te) { + te.printStackTrace(); + System.out.println("Failed to retrieve places: " + te.getMessage()); + System.exit(-1); + } + } +} diff --git a/twitter4j-examples/src/main/java/twitter4j/examples/geo/SearchPlaces.java b/twitter4j-examples/src/main/java/twitter4j/examples/geo/SearchPlaces.java new file mode 100644 index 000000000..031478ce1 --- /dev/null +++ b/twitter4j-examples/src/main/java/twitter4j/examples/geo/SearchPlaces.java @@ -0,0 +1,76 @@ +/* +Copyright (c) 2007-2010, Yusuke Yamamoto +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yusuke Yamamoto nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package twitter4j.examples.geo; + +import twitter4j.GeoLocation; +import twitter4j.GeoQuery; +import twitter4j.Place; +import twitter4j.ResponseList; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; + +/** + * Given a latitude and a longitude or IP address, searches for up to 20 places that can be used as a place_id when updating a status. + * + * @author Yusuke Yamamoto - yusuke at mac.com + */ +public final class SearchPlaces { + /** + * Usage: java twitter4j.examples.geo.SearchPlaces [ip address] or [latitude] [longitude] + * + * @param args message + */ + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Usage: java twitter4j.examples.geo.SearchPlaces [ip address] or [latitude] [longitude]"); + System.exit(-1); + } + try { + Twitter twitter = new TwitterFactory().getInstance(); + GeoQuery query; + if (args.length == 2) { + query = new GeoQuery(new GeoLocation(Double.parseDouble(args[0]), Double.parseDouble(args[1]))); + } else { + query = new GeoQuery(args[0]); + } + ResponseList places = twitter.searchPlaces(query); + if (places.size() == 0) { + System.out.println("No location associated with the specified IP address or lat/lang"); + } else { + for (Place place : places) { + System.out.println("id: " + place.getId() + " name: " + place.getFullName()); + } + } + System.exit(0); + } catch (TwitterException te) { + te.printStackTrace(); + System.out.println("Failed to retrieve places: " + te.getMessage()); + System.exit(-1); + } + } +}