Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from TouristGuideApp/v3_us17
Browse files Browse the repository at this point in the history
V3 us17
  • Loading branch information
ErminaTrontzou authored Nov 26, 2022
2 parents cf5af9d + 0c25a1a commit 99796a9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package gr.thegoodsideofe1.tourguide.controllers;

import gr.thegoodsideofe1.tourguide.entities.Image;
import gr.thegoodsideofe1.tourguide.entities.Tag;
import gr.thegoodsideofe1.tourguide.repositories.ImageRepository;
import gr.thegoodsideofe1.tourguide.services.TagService;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("api/v1/flickr")
@ResponseBody
public class FlickrController {
@Autowired
ImageRepository imageRepository;

@Autowired
TagService tagService;

@Transactional
@GetMapping("/getByTitle/{title}")
public ResponseEntity<?> getNewImages(@PathVariable String title){
if (getFlickr(title)){
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "success");
returnMap.put("message", "Images Imported");
return ResponseEntity.status(201).body(returnMap);
}
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "error");
returnMap.put("message", "Problem on image import");
return ResponseEntity.status(200).body(returnMap);
}

private boolean getFlickr(String title) {
String flickrAPIURl = "https://www.flickr.com/services/rest/";
flickrAPIURl += "?method=flickr.photos.search";
flickrAPIURl += "&api_key=d4e4f456722f8f76048260df1e0c1880";
flickrAPIURl += "&format=json";
flickrAPIURl += "&text=" + title;
flickrAPIURl += "&has_geo=1";
flickrAPIURl += "&privacy_filter=1";
flickrAPIURl += "&accuracy=11";
flickrAPIURl += "&content_type=1";
flickrAPIURl += "&media=all";
flickrAPIURl += "&extras=geo, description, media, tags, url_o, date_upload, date_taken, views, owner_name";

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(flickrAPIURl)
.addHeader("Accept", "application/json")
.addHeader("charset", "utf-8")
.build();

try (Response response = client.newCall(request).execute()) {
JSONObject flickResponse = convertFlickrResponseToJSON(response.body().string());
serializeFlickrResponseData(flickResponse);
} catch (IOException e){
return false;
}
return true;
}

private void serializeFlickrResponseData(JSONObject allData){
JSONObject photosObj = new JSONObject(allData.get("photos").toString());
JSONArray photoArray = new JSONArray(photosObj.get("photo").toString());

for (int i = 0; i<photoArray.length(); i++){
JSONObject singlePhotoObj = new JSONObject(photoArray.get(i).toString());
JSONObject singlePhotoDescription = new JSONObject(singlePhotoObj.get("description").toString());

Image imageToSave = new Image();
imageToSave.setTitle(singlePhotoObj.getString("title"));
imageToSave.setDescription(singlePhotoDescription.getString("_content"));
imageToSave.setLatitude(singlePhotoObj.getString("latitude"));
imageToSave.setLongitude(singlePhotoObj.getString("longitude"));
imageToSave.setFileName(singlePhotoObj.getString("url_o"));
imageToSave.setDateTaken(singlePhotoObj.getString("datetaken"));
imageToSave.setViews(Integer.parseInt(singlePhotoObj.getString("views")));
imageToSave.setOwnerName(singlePhotoObj.getString("ownername"));

Set<Tag> allTagsToSave = serializeTags(singlePhotoObj.getString("tags"));
imageToSave.setTags(allTagsToSave);

System.out.println(imageToSave.getTitle());
imageRepository.save(imageToSave);
}
}

private JSONObject convertFlickrResponseToJSON(String bodyData){
//Trim Body Data String
String trimmedBodyData = bodyData.replace("jsonFlickrApi(", "");
StringBuffer bodyResponseBuffer = new StringBuffer(trimmedBodyData);
bodyResponseBuffer.deleteCharAt(bodyResponseBuffer.length() - 1);

//Convert to JSON
JSONObject jsonObj = new JSONObject(bodyResponseBuffer.toString());
return jsonObj;
}

private Set<Tag> serializeTags(String allTags){
Set<Tag> imageTagsSet = new HashSet<Tag>();
if (allTags.isEmpty()){
return imageTagsSet;
}
String[] splitTags = allTags.split(" ");
for (String tag: splitTags){
long tagCount = tagService.getByTagName(tag);
if (tagCount != 0){
//Tag Exists
Tag singleTagDB = tagService.getTagByTagName(tag);
imageTagsSet.add(singleTagDB);
continue;
}
//Create Tag
Tag tagToSave = new Tag();
tagToSave.setName(tag);
tagService.save(tagToSave);
imageTagsSet.add(tagToSave);
}
return imageTagsSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,107 +62,21 @@ public ResponseEntity<Image> get (@PathVariable Integer id){
public ResponseEntity<?> imageByTitle(@PathVariable String title,
@RequestParam(value="page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "8") int size) {
Pageable paging = PageRequest.of(page, size);
Page<Image> imagesPage = imageRepository.findAllImagesByTitle(title, paging);

if (imagesPage.getContent().isEmpty()) {
if (getFlickr(title)) {
imagesPage = imageService.getImageByTitle(title, paging);
}
}
return new ResponseEntity<>(imagesPage, HttpStatus.OK);
}

private boolean getFlickr(String title) {
String flickrAPIURl = "https://www.flickr.com/services/rest/";
flickrAPIURl += "?method=flickr.photos.search";
flickrAPIURl += "&api_key=d4e4f456722f8f76048260df1e0c1880";
flickrAPIURl += "&format=json";
flickrAPIURl += "&text=" + title;
flickrAPIURl += "&has_geo=1";
flickrAPIURl += "&privacy_filter=1";
flickrAPIURl += "&accuracy=11";
flickrAPIURl += "&content_type=1";
flickrAPIURl += "&media=all";
flickrAPIURl += "&extras=geo, description, media, tags, url_o, date_upload, date_taken, views, owner_name";

MediaType JSON = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(flickrAPIURl)
.addHeader("Accept", "application/json")
.addHeader("charset", "utf-8")
.build();

try (Response response = client.newCall(request).execute()) {
JSONObject flickResponse = convertFlickrResponseToJSON(response.body().string());
serializeFlickrResponseData(flickResponse);
} catch (IOException e){
return false;
}
return true;
}

private void serializeFlickrResponseData(JSONObject allData){
JSONObject photosObj = new JSONObject(allData.get("photos").toString());
JSONArray photoArray = new JSONArray(photosObj.get("photo").toString());

for (int i = 0; i<photoArray.length(); i++){
JSONObject singlePhotoObj = new JSONObject(photoArray.get(i).toString());
JSONObject singlePhotoDescription = new JSONObject(singlePhotoObj.get("description").toString());

Image imageToSave = new Image();
imageToSave.setTitle(singlePhotoObj.getString("title"));
imageToSave.setDescription(singlePhotoDescription.getString("_content"));
imageToSave.setLatitude(singlePhotoObj.getString("latitude"));
imageToSave.setLongitude(singlePhotoObj.getString("longitude"));
imageToSave.setFileName(singlePhotoObj.getString("url_o"));
imageToSave.setDateTaken(singlePhotoObj.getString("datetaken"));
imageToSave.setViews(Integer.parseInt(singlePhotoObj.getString("views")));
imageToSave.setOwnerName(singlePhotoObj.getString("ownername"));

Set<Tag> allTagsToSave = serializeTags(singlePhotoObj.getString("tags"));
imageToSave.setTags(allTagsToSave);

System.out.println(imageToSave.getTitle());
imageRepository.save(imageToSave);
}
}

private JSONObject convertFlickrResponseToJSON(String bodyData){
//Trim Body Data String
String trimmedBodyData = bodyData.replace("jsonFlickrApi(", "");
StringBuffer bodyResponseBuffer = new StringBuffer(trimmedBodyData);
bodyResponseBuffer.deleteCharAt(bodyResponseBuffer.length() - 1);

//Convert to JSON
JSONObject jsonObj = new JSONObject(bodyResponseBuffer.toString());
return jsonObj;
}

private Set<Tag> serializeTags(String allTags){
Set<Tag> imageTagsSet = new HashSet<Tag>();
if (allTags.isEmpty()){
return imageTagsSet;
}
String[] splitTags = allTags.split(" ");
for (String tag: splitTags){
long tagCount = tagService.getByTagName(tag);
if (tagCount != 0){
//Tag Exists
Tag singleTagDB = tagService.getTagByTagName(tag);
imageTagsSet.add(singleTagDB);
continue;
int imagesCount = imageService.getImageCount(title);
if (imagesCount != 0){
Pageable paging = PageRequest.of(page, size);
Page<Image> imagesPage = imageRepository.findAllImagesByTitle(title, paging);
if (imagesPage.getContent().isEmpty()) {
if (getFlickr(title)) {
imagesPage = imageService.getImageByTitle(title, paging);
}
}
//Create Tag
Tag tagToSave = new Tag();
tagToSave.setName(tag);
tagService.save(tagToSave);
imageTagsSet.add(tagToSave);
}
return imageTagsSet;
return new ResponseEntity<>(imagesPage, HttpStatus.OK);
}
HashMap<String, String> returnMap = new HashMap<String, String>();
returnMap.put("status", "error");
returnMap.put("message", "No images with your search criteria");
return ResponseEntity.status(204).body(returnMap);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface ImageRepository extends CrudRepository<Image, Integer> {
@Query(value = "SELECT i FROM Image i WHERE i.title LIKE %:title% OR i.description LIKE %:title%")
Page<Image> findAllImagesByTitle(@Param("title") String title, Pageable pageable);

@Query(value = "SELECT COUNT(i) FROM Image i WHERE i.title LIKE %:title% OR i.description LIKE %:title%")
int countImagesByTitle(@Param("title") String title);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public Image getImage(Integer id){
return imageRepository.findById(id).get();
}


public Page<Image> getImageByTitle(String title, Pageable pageable){
return imageRepository.findAllImagesByTitle(title, pageable);
}

public int getImageCount(String title){
return imageRepository.countImagesByTitle(title);
}
}

0 comments on commit 99796a9

Please sign in to comment.