Merge pull request #147 from Doha2012/master

modify submit method
This commit is contained in:
Eugen 2015-02-22 18:43:30 +02:00
commit 357d483e3f
3 changed files with 40 additions and 20 deletions

View File

@ -58,12 +58,7 @@ public class RedditController {
Map<String, String> param = new HashMap<String, String>();
param.put("api_type", "json");
param.put("kind", "self");
param.put("sr", "api");
// param.put("iden", "XCzyTdJveIcYXNhLJ4a2X9WVDswtx83u");
// param.put("captcha", "BJMGMU");
// param.put("title", "http2 is coming soon");
// param.put("text", "http2 is coming soon what do you think about that");
param.put("kind", "link");
param.putAll(formParams);
System.out.println(param.keySet());
@ -83,6 +78,10 @@ public class RedditController {
@RequestMapping("/post")
public String showSubmissionForm(Model model) throws JsonProcessingException, IOException {
try {
List<String> subreddits = getSubreddit();
model.addAttribute("subreddits", subreddits);
String needsCaptchaResult = needsCaptcha();
if (needsCaptchaResult.equalsIgnoreCase("true")) {
String newCaptchaResult = getNewCaptcha();
@ -90,13 +89,22 @@ public class RedditController {
String iden = split[split.length - 2];
model.addAttribute("iden", iden.trim());
}
} catch (UserApprovalRequiredException e) {
throw e;
} catch (UserRedirectRequiredException e) {
throw e;
} catch (Exception e) {
LOGGER.error("Error occurred", e);
model.addAttribute("error", e.getLocalizedMessage());
return "reddit";
}
return "submissionForm";
}
//
public List<String> getSubreddit(Model model) throws JsonProcessingException, IOException {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular", String.class);
private List<String> getSubreddit() throws JsonProcessingException, IOException {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
JsonNode node = new ObjectMapper().readTree(result);
node = node.get("data").get("children");
List<String> subreddits = new ArrayList<String>();
@ -106,12 +114,12 @@ public class RedditController {
return subreddits;
}
public String needsCaptcha() {
private String needsCaptcha() {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/api/needs_captcha.json", String.class);
return result;
}
public String getNewCaptcha() {
private String getNewCaptcha() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity req = new HttpEntity(headers);

View File

@ -9,7 +9,7 @@
<c:when test="${info != null}">
<h1>Your Reddit Info</h1>
<b>Your reddit username is </b>${info}
<br>
<br><br><br>
<a href="post">Submit to Reddit</a>
</c:when>
<c:otherwise>

View File

@ -17,10 +17,22 @@
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Content</label>
<span class="col-sm-9"><textarea placeholder="content" class="form-control"></textarea></span>
<label class="col-sm-3">Url</label>
<span class="col-sm-9"><input name="url" placeholder="url" class="form-control" /></span>
</div>
<br><br>
<div class="form-group">
<label class="col-sm-3">Subreddit</label>
<span class="col-sm-9">
<select name="sr">
<c:forEach items="${subreddits}" var="item">
<option value="${item}">${item}</option>
</c:forEach>
</select>
</span>
</div>
<br><br>
<c:if test="${iden != null}">
<input type="hidden" name="iden" value="${iden}"/>