/Flickr4Java/src/main/java/com/flickr4java/flickr/tags/TagsInterface.java

https://gitlab.com/sugesk/flickr4java · Java · 411 lines · 220 code · 67 blank · 124 comment · 20 complexity · 206f162ebfd321165d7a53bde34f2f69 MD5 · raw file

  1. /*
  2. * Copyright (c) 2005 Aetrion LLC.
  3. */
  4. package com.flickr4java.flickr.tags;
  5. import com.flickr4java.flickr.FlickrException;
  6. import com.flickr4java.flickr.Response;
  7. import com.flickr4java.flickr.Transport;
  8. import com.flickr4java.flickr.photos.Photo;
  9. import com.flickr4java.flickr.photos.PhotoList;
  10. import com.flickr4java.flickr.photos.PhotoUtils;
  11. import com.flickr4java.flickr.util.XMLUtilities;
  12. import org.w3c.dom.Element;
  13. import org.w3c.dom.NodeList;
  14. import org.w3c.dom.Text;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * Interface for working with Flickr tags.
  22. *
  23. * @author Anthony Eden
  24. * @version $Id: TagsInterface.java,v 1.19 2009/07/02 21:52:35 x-mago Exp $
  25. */
  26. public class TagsInterface {
  27. public static final String METHOD_GET_CLUSTERS = "flickr.tags.getClusters";
  28. public static final String METHOD_GET_HOT_LIST = "flickr.tags.getHotList";
  29. public static final String METHOD_GET_LIST_PHOTO = "flickr.tags.getListPhoto";
  30. public static final String METHOD_GET_LIST_USER = "flickr.tags.getListUser";
  31. public static final String METHOD_GET_LIST_USER_POPULAR = "flickr.tags.getListUserPopular";
  32. public static final String METHOD_GET_LIST_USER_RAW = "flickr.tags.getListUserRaw";
  33. public static final String METHOD_GET_RELATED = "flickr.tags.getRelated";
  34. public static final String METHOD_GET_CLUSTER_PHOTOS = "flickr.tags.getClusterPhotos";
  35. public static final String PERIOD_WEEK = "week";
  36. public static final String PERIOD_DAY = "day";
  37. private final String apiKey;
  38. private final String sharedSecret;
  39. private final Transport transportAPI;
  40. /**
  41. * Construct a TagsInterface.
  42. *
  43. * @param apiKey
  44. * The API key
  45. * @param transportAPI
  46. * The Transport interface
  47. */
  48. public TagsInterface(String apiKey, String sharedSecret, Transport transportAPI) {
  49. this.apiKey = apiKey;
  50. this.sharedSecret = sharedSecret;
  51. this.transportAPI = transportAPI;
  52. }
  53. /**
  54. * Search for tag-clusters.
  55. * <p/>
  56. *
  57. * <p>
  58. * This method does not require authentication.
  59. * </p>
  60. *
  61. * @since 1.2
  62. * @param searchTag
  63. * @return a list of clusters
  64. */
  65. public ClusterList getClusters(String searchTag) throws FlickrException {
  66. Map<String, Object> parameters = new HashMap<String, Object>();
  67. parameters.put("method", METHOD_GET_CLUSTERS);
  68. parameters.put("tag", searchTag);
  69. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  70. if (response.isError()) {
  71. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  72. }
  73. ClusterList clusters = new ClusterList();
  74. Element clustersElement = response.getPayload();
  75. NodeList clusterElements = clustersElement.getElementsByTagName("cluster");
  76. for (int i = 0; i < clusterElements.getLength(); i++) {
  77. Cluster cluster = new Cluster();
  78. NodeList tagElements = ((Element) clusterElements.item(i)).getElementsByTagName("tag");
  79. for (int j = 0; j < tagElements.getLength(); j++) {
  80. Tag tag = new Tag();
  81. tag.setValue(((Text) tagElements.item(j).getFirstChild()).getData());
  82. cluster.addTag(tag);
  83. }
  84. clusters.addCluster(cluster);
  85. }
  86. return clusters;
  87. }
  88. /**
  89. * Returns the first 24 photos for a given tag cluster.
  90. *
  91. * <p>
  92. * This method does not require authentication.
  93. * </p>
  94. *
  95. * @param tag
  96. * @param clusterId
  97. * @return PhotoList
  98. * @throws FlickrException
  99. */
  100. public PhotoList<Photo> getClusterPhotos(String tag, String clusterId) throws FlickrException {
  101. PhotoList<Photo> photos = new PhotoList<Photo>();
  102. Map<String, Object> parameters = new HashMap<String, Object>();
  103. parameters.put("method", METHOD_GET_CLUSTER_PHOTOS);
  104. parameters.put("tag", tag);
  105. parameters.put("cluster_id", clusterId);
  106. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  107. if (response.isError()) {
  108. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  109. }
  110. Element photosElement = response.getPayload();
  111. NodeList photoNodes = photosElement.getElementsByTagName("photo");
  112. photos.setPage("1");
  113. photos.setPages("1");
  114. photos.setPerPage("" + photoNodes.getLength());
  115. photos.setTotal("" + photoNodes.getLength());
  116. for (int i = 0; i < photoNodes.getLength(); i++) {
  117. Element photoElement = (Element) photoNodes.item(i);
  118. photos.add(PhotoUtils.createPhoto(photoElement));
  119. }
  120. return photos;
  121. }
  122. /**
  123. * Returns a list of hot tags for the given period.
  124. *
  125. * <p>
  126. * This method does not require authentication.
  127. * </p>
  128. *
  129. * @param period
  130. * valid values are 'day' or 'week'
  131. * @param count
  132. * maximum is 200
  133. * @return The collection of HotlistTag objects
  134. */
  135. public Collection<HotlistTag> getHotList(String period, int count) throws FlickrException {
  136. Map<String, Object> parameters = new HashMap<String, Object>();
  137. parameters.put("method", METHOD_GET_HOT_LIST);
  138. parameters.put("period", period);
  139. parameters.put("count", "" + count);
  140. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  141. if (response.isError()) {
  142. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  143. }
  144. Element tagsElement = response.getPayload();
  145. List<HotlistTag> tags = new ArrayList<HotlistTag>();
  146. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  147. for (int i = 0; i < tagElements.getLength(); i++) {
  148. Element tagElement = (Element) tagElements.item(i);
  149. HotlistTag tag = new HotlistTag();
  150. tag.setScore(tagElement.getAttribute("score"));
  151. tag.setValue(((Text) tagElement.getFirstChild()).getData());
  152. tags.add(tag);
  153. }
  154. return tags;
  155. }
  156. /**
  157. * Get a list of tags for the specified photo.
  158. *
  159. * <p>
  160. * This method does not require authentication.
  161. * </p>
  162. *
  163. * @param photoId
  164. * The photo ID
  165. * @return The collection of Tag objects
  166. */
  167. public Photo getListPhoto(String photoId) throws FlickrException {
  168. Map<String, Object> parameters = new HashMap<String, Object>();
  169. parameters.put("method", METHOD_GET_LIST_PHOTO);
  170. parameters.put("photo_id", photoId);
  171. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  172. if (response.isError()) {
  173. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  174. }
  175. Element photoElement = response.getPayload();
  176. Photo photo = new Photo();
  177. photo.setId(photoElement.getAttribute("id"));
  178. List<Tag> tags = new ArrayList<Tag>();
  179. Element tagsElement = (Element) photoElement.getElementsByTagName("tags").item(0);
  180. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  181. for (int i = 0; i < tagElements.getLength(); i++) {
  182. Element tagElement = (Element) tagElements.item(i);
  183. Tag tag = new Tag();
  184. tag.setId(tagElement.getAttribute("id"));
  185. tag.setAuthor(tagElement.getAttribute("author"));
  186. tag.setAuthorName(tagElement.getAttribute("authorname"));
  187. tag.setRaw(tagElement.getAttribute("raw"));
  188. tag.setValue(((Text) tagElement.getFirstChild()).getData());
  189. tags.add(tag);
  190. }
  191. photo.setTags(tags);
  192. return photo;
  193. }
  194. /**
  195. * Get a collection of tags used by the specified user.
  196. *
  197. * <p>
  198. * This method does not require authentication.
  199. * </p>
  200. *
  201. * @param userId
  202. * The User ID
  203. * @return The User object
  204. * @throws FlickrException
  205. */
  206. public Collection<Tag> getListUser(String userId) throws FlickrException {
  207. Map<String, Object> parameters = new HashMap<String, Object>();
  208. parameters.put("method", METHOD_GET_LIST_USER);
  209. parameters.put("user_id", userId);
  210. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  211. if (response.isError()) {
  212. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  213. }
  214. Element whoElement = response.getPayload();
  215. List<Tag> tags = new ArrayList<Tag>();
  216. Element tagsElement = (Element) whoElement.getElementsByTagName("tags").item(0);
  217. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  218. for (int i = 0; i < tagElements.getLength(); i++) {
  219. Element tagElement = (Element) tagElements.item(i);
  220. Tag tag = new Tag();
  221. tag.setValue(((Text) tagElement.getFirstChild()).getData());
  222. tags.add(tag);
  223. }
  224. return tags;
  225. }
  226. /**
  227. * Get a list of the user's popular tags.
  228. *
  229. * <p>
  230. * This method does not require authentication.
  231. * </p>
  232. *
  233. * @param userId
  234. * The user ID
  235. * @return The collection of Tag objects
  236. * @throws FlickrException
  237. */
  238. public Collection<Tag> getListUserPopular(String userId) throws FlickrException {
  239. Map<String, Object> parameters = new HashMap<String, Object>();
  240. parameters.put("method", METHOD_GET_LIST_USER_POPULAR);
  241. parameters.put("user_id", userId);
  242. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  243. if (response.isError()) {
  244. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  245. }
  246. Element whoElement = response.getPayload();
  247. List<Tag> tags = new ArrayList<Tag>();
  248. Element tagsElement = (Element) whoElement.getElementsByTagName("tags").item(0);
  249. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  250. for (int i = 0; i < tagElements.getLength(); i++) {
  251. Element tagElement = (Element) tagElements.item(i);
  252. Tag tag = new Tag();
  253. tag.setCount(tagElement.getAttribute("count"));
  254. tag.setValue(((Text) tagElement.getFirstChild()).getData());
  255. tags.add(tag);
  256. }
  257. return tags;
  258. }
  259. /**
  260. * Get a list of the user's (identified by token) popular tags.
  261. *
  262. * <p>
  263. * This method does not require authentication.
  264. * </p>
  265. *
  266. * @return The collection of Tag objects
  267. * @throws FlickrException
  268. */
  269. public Collection<TagRaw> getListUserRaw() throws FlickrException {
  270. return getListUserRaw(null);
  271. }
  272. /**
  273. * Get a list of the user's (identified by token) popular tags.
  274. *
  275. * <p>
  276. * This method does not require authentication.
  277. * </p>
  278. *
  279. * @param tagVal
  280. * a tag to search for (optional)
  281. *
  282. * @return The collection of Tag objects
  283. * @throws FlickrException
  284. */
  285. public Collection<TagRaw> getListUserRaw(String tagVal) throws FlickrException {
  286. Map<String, Object> parameters = new HashMap<String, Object>();
  287. parameters.put("method", METHOD_GET_LIST_USER_RAW);
  288. if (tagVal != null) {
  289. parameters.put("tag", tagVal);
  290. }
  291. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  292. if (response.isError()) {
  293. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  294. }
  295. Element whoElement = response.getPayload();
  296. List<TagRaw> tags = new ArrayList<TagRaw>();
  297. Element tagsElement = (Element) whoElement.getElementsByTagName("tags").item(0);
  298. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  299. for (int i = 0; i < tagElements.getLength(); i++) {
  300. Element tagElement = (Element) tagElements.item(i);
  301. TagRaw tag = new TagRaw();
  302. tag.setClean(tagElement.getAttribute("clean"));
  303. NodeList rawElements = tagElement.getElementsByTagName("raw");
  304. for (int j = 0; j < rawElements.getLength(); j++) {
  305. Element rawElement = (Element) rawElements.item(j);
  306. tag.addRaw(((Text) rawElement.getFirstChild()).getData());
  307. }
  308. tags.add(tag);
  309. }
  310. return tags;
  311. }
  312. /**
  313. * Get the related tags.
  314. *
  315. * <p>
  316. * This method does not require authentication.
  317. * </p>
  318. *
  319. * @param tag
  320. * The source tag
  321. * @return A RelatedTagsList object
  322. * @throws FlickrException
  323. */
  324. public RelatedTagsList getRelated(String tag) throws FlickrException {
  325. Map<String, Object> parameters = new HashMap<String, Object>();
  326. parameters.put("method", METHOD_GET_RELATED);
  327. parameters.put("tag", tag);
  328. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  329. if (response.isError()) {
  330. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  331. }
  332. Element tagsElement = response.getPayload();
  333. RelatedTagsList tags = new RelatedTagsList();
  334. tags.setSource(tagsElement.getAttribute("source"));
  335. NodeList tagElements = tagsElement.getElementsByTagName("tag");
  336. for (int i = 0; i < tagElements.getLength(); i++) {
  337. Element tagElement = (Element) tagElements.item(i);
  338. Tag t = new Tag();
  339. t.setValue(XMLUtilities.getValue(tagElement));
  340. tags.add(t);
  341. }
  342. return tags;
  343. }
  344. }