forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScrapingController.java
146 lines (132 loc) · 5.28 KB
/
ScrapingController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2018, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixedproject.controller.scraping;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.controller.ComiXedControllerException;
import org.comixedproject.model.comic.Comic;
import org.comixedproject.model.net.ComicScrapeRequest;
import org.comixedproject.model.net.GetScrapingIssueRequest;
import org.comixedproject.model.net.GetVolumesRequest;
import org.comixedproject.scrapers.ScrapingException;
import org.comixedproject.scrapers.model.ScrapingIssue;
import org.comixedproject.scrapers.model.ScrapingVolume;
import org.comixedproject.service.scraping.ScrapingService;
import org.comixedproject.views.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
* <code>ScrapingController</code> processes REST APIs relating to scraping comics.
*
* @author Darryl L. Pierce
*/
@RestController
@RequestMapping("/api/scraping")
@Log4j2
public class ScrapingController {
@Autowired private ScrapingService scrapingService;
/**
* Retrieves the minimal {@link ScrapingIssue} for the specified issue of the given volume.
*
* @param volume the volume id
* @param request the request body
* @return the issue
* @throws ComiXedControllerException if an error occurs
*/
@PostMapping(
value = "/volumes/{volume}/issues",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public ScrapingIssue queryForIssue(
@PathVariable("volume") final Integer volume,
@RequestBody() final GetScrapingIssueRequest request)
throws ComiXedControllerException {
String issue = request.getIssueNumber();
boolean skipCache = request.isSkipCache();
String apiKey = request.getApiKey();
log.info(
"Preparing to retrieve issue={} for volume={} (skipCache={})", issue, volume, skipCache);
try {
return this.scrapingService.getIssue(apiKey, volume, issue, skipCache);
} catch (ScrapingException error) {
throw new ComiXedControllerException("Failed to get single scraping issue", error);
}
}
/**
* Retrieves the list of potential volumes for the given series name.
*
* @param request the reqwuest body
* @return the list of volumes
* @throws ComiXedControllerException if an error occurs
*/
@PostMapping(
value = "/volumes",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public List<ScrapingVolume> queryForVolumes(@RequestBody() final GetVolumesRequest request)
throws ComiXedControllerException {
String apiKey = request.getApiKey();
boolean skipCache = request.getSkipCache();
String series = request.getSeries();
final Integer maxRecords = request.getMaxRecords();
log.info(
"Getting volumes: series={} (max records={}) {}",
series,
maxRecords,
skipCache ? "(Skipping cache)" : "");
try {
final List<ScrapingVolume> result =
this.scrapingService.getVolumes(apiKey, series, maxRecords, skipCache);
log.debug("Returning {} volume{}", result.size(), result.size() == 1 ? "" : "s");
return result;
} catch (ScrapingException error) {
throw new ComiXedControllerException("Failed to get list of volumes", error);
}
}
/**
* Scrapes a single {@link Comic} using the specified source issue.
*
* @param comicId the comic id
* @param issueId the issue id
* @param request the request body
* @return the scraped and updaed {@link Comic}
* @throws ComiXedControllerException if an error occurs
*/
@PostMapping(
value = "/comics/{comicId}/issue/{issueId}",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@JsonView(View.ComicDetails.class)
public Comic scrapeAndSaveComicDetails(
@PathVariable("comicId") final Long comicId,
@PathVariable("issueId") final Integer issueId,
@RequestBody() final ComicScrapeRequest request)
throws ComiXedControllerException {
boolean skipCache = request.getSkipCache();
String apiKey = request.getApiKey();
log.info("Scraping code: id={} issue id={} (skip cache={})", comicId, issueId, apiKey);
try {
log.debug("Scraping comic details");
return this.scrapingService.scrapeComic(apiKey, comicId, issueId, skipCache);
} catch (ScrapingException error) {
throw new ComiXedControllerException("Failed to scrape comic", error);
}
}
}