Skip to content

Instantly share code, notes, and snippets.

@must1
Created July 23, 2019 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save must1/0124f5b294160d8ad5824fe9f52903fe to your computer and use it in GitHub Desktop.
Save must1/0124f5b294160d8ad5824fe9f52903fe to your computer and use it in GitHub Desktop.
package bookstore.scraper.book.scrapingtypes;
import bookstore.scraper.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.empik.EmpikFetchingBookService;
import bookstore.scraper.merlin.MerlinFetchingBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static bookstore.scraper.JSoupConnector.connect;
@Service
public class BestSellersService {
@Value("${external.library.url.empik.bestsellers}")
private String bestSellersEmpikURL;
@Value("${external.library.url.merlin.bestsellers}")
private String bestSellersMerlinURL;
private final EmpikFetchingBookService empikBookService;
private final MerlinFetchingBookService merlinBookService;
@Autowired
public BestSellersService(EmpikFetchingBookService empikBookService, MerlinFetchingBookService merlinBookService) {
this.empikBookService = empikBookService;
this.merlinBookService = merlinBookService;
}
public Map<Bookstore, List<Book>> getBestSellers() throws IOException {
Map<Bookstore, List<Book>> bookstoreWithBestSellers = new HashMap<>();
bookstoreWithBestSellers.put(Bookstore.EMPIK, empikBookService
.get5BestSellersEmpik(connect(bestSellersEmpikURL)));
bookstoreWithBestSellers.put(Bookstore.MERLIN, merlinBookService
.get5BestSellersMerlin(connect(bestSellersMerlinURL)));
return bookstoreWithBestSellers;
}
}
package bookstore.scraper.controller;
import bookstore.scraper.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.book.scrapingtypes.BestSellersService;
import bookstore.scraper.book.scrapingtypes.CategorizedBookService;
import bookstore.scraper.book.scrapingtypes.MostPreciseBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class BookController {
private final MostPreciseBookService mostPreciseBookService;
private final CategorizedBookService categorizedBookService;
private final BestSellersService bestSellersService;
@Autowired
public BookController(MostPreciseBookService bookOperationsService, CategorizedBookService categorizedBookService, BestSellersService bestSellersService) {
this.mostPreciseBookService = bookOperationsService;
this.categorizedBookService = categorizedBookService;
this.bestSellersService = bestSellersService;
}
@GetMapping("/book/{title}")
public Map<Bookstore, Book> getBookByTitle(@PathVariable String title) {
return mostPreciseBookService.getBookByTitle(title);
}
@GetMapping("/romances")
public Map<Bookstore, List<Book>> get15RomanticBooks() {
return categorizedBookService.get15BooksFromRomanceCategory();
}
@GetMapping("/biographies")
public Map<Bookstore, List<Book>> get15BiographiesBooks() {
return categorizedBookService.get15BooksFromBiographiesCategory();
}
@GetMapping("/guides")
public Map<Bookstore, List<Book>> get15GuidesBooks() {
return categorizedBookService.get15BooksFromGuidesCategory();
}
@GetMapping("/fantasy")
public Map<Bookstore, List<Book>> get15FantasyBooks() {
return categorizedBookService.get15BooksFromFantasyCategory();
}
@GetMapping("/crime")
public Map<Bookstore, List<Book>> get15CrimeBooks() {
return categorizedBookService.get15BooksFromCrimeCategory();
}
}
package bookstore.scraper.book.scrapingtypes;
import bookstore.scraper.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.empik.EmpikFetchingBookService;
import bookstore.scraper.merlin.MerlinFetchingBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import static bookstore.scraper.JSoupConnector.connect;
@Service
@Slf4j
public class MostPreciseBookService {
private final EmpikFetchingBookService empikBookService;
private final MerlinFetchingBookService merlinBookService;
@Value("${external.library.url.empik.most.precise.book}")
private String mostPreciseBookEmpikURL;
@Value("${external.library.url.merlin.most.precise.book}")
private String mostPreciseBookMerlinURL;
@Autowired
public MostPreciseBookService(EmpikFetchingBookService empikBookService, MerlinFetchingBookService merlinBookService) {
this.empikBookService = empikBookService;
this.merlinBookService = merlinBookService;
}
public Map<Bookstore, Book> getBookByTitle(String title) {
Map<Bookstore, Book> bookstoreWithMostPreciseBook = new HashMap<>();
bookstoreWithMostPreciseBook.put(Bookstore.MERLIN,
merlinBookService.getMostPreciseMerlinBook(
connect(concatURLWithTitle(mostPreciseBookEmpikURL, title))));
bookstoreWithMostPreciseBook.put(Bookstore.EMPIK,
empikBookService.getMostPreciseEmpikBook(
connect(concatURLWithTitle(mostPreciseBookMerlinURL, title))));
return bookstoreWithMostPreciseBook;
}
private String concatURLWithTitle(String URL, String title) {
return String.format(URL, title);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment