This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from TouristGuideApp/v4_integration
V4 integration
- Loading branch information
Showing
30 changed files
with
1,436 additions
and
749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 6 additions & 116 deletions
122
src/main/java/gr/thegoodsideofe1/tourguide/controllers/FlickrController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,27 @@ | ||
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 gr.thegoodsideofe1.tourguide.services.FlickrService; | ||
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; | ||
FlickrService flickrService; | ||
|
||
@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; | ||
return flickrService.getNewImagesForLocation(title); | ||
} | ||
|
||
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; | ||
@PostMapping("/imageToSave") | ||
public ResponseEntity<?>imageToSave(@RequestBody Image photo){ | ||
return flickrService.saveImage(photo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 6 additions & 18 deletions
24
src/main/java/gr/thegoodsideofe1/tourguide/controllers/ImageTagsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,25 @@ | ||
package gr.thegoodsideofe1.tourguide.controllers; | ||
|
||
|
||
import gr.thegoodsideofe1.tourguide.entities.ImageTags; | ||
import gr.thegoodsideofe1.tourguide.services.ImageTagsService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/image_tags") | ||
public class ImageTagsController { | ||
@Autowired | ||
ImageTagsService imageTagsService; | ||
|
||
@GetMapping("") | ||
public List<ImageTags> list(){ | ||
@RequestMapping(value = "", method = RequestMethod.GET) | ||
public ResponseEntity<?> list() { | ||
return imageTagsService.listAllImageTags(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<ImageTags> get (@PathVariable Integer id){ | ||
try{ | ||
ImageTags imageTags = imageTagsService.getImageTags(id); | ||
return new ResponseEntity<ImageTags>(imageTags, HttpStatus.OK); | ||
}catch (NoSuchElementException e){ | ||
return new ResponseEntity<ImageTags>(HttpStatus.NOT_FOUND); | ||
} | ||
@RequestMapping(value = "/{id}", method = RequestMethod.GET) | ||
public ResponseEntity<?> getSpecificImage(@PathVariable int id) { | ||
return imageTagsService.getImageTags(id); | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/main/java/gr/thegoodsideofe1/tourguide/controllers/Responder.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.