add reddit resubmit inactive post
This commit is contained in:
parent
4d6ec4a16f
commit
821c313abe
|
@ -13,7 +13,7 @@ public class SessionListener implements HttpSessionListener {
|
|||
@Override
|
||||
public void sessionCreated(HttpSessionEvent event) {
|
||||
logger.info("==== Session is created ====");
|
||||
event.getSession().setMaxInactiveInterval(1 * 60);
|
||||
event.getSession().setMaxInactiveInterval(30 * 60);
|
||||
event.getSession().setAttribute("PREDICTION_FEATURE", MyFeatures.PREDICTION_FEATURE);
|
||||
}
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||
details.setAccessTokenUri(accessTokenUri);
|
||||
details.setUserAuthorizationUri(userAuthorizationUri);
|
||||
details.setTokenName("oauth_token");
|
||||
details.setScope(Arrays.asList("identity", "read", "submit"));
|
||||
details.setScope(Arrays.asList("identity", "read", "submit", "edit"));
|
||||
details.setGrantType("authorization_code");
|
||||
details.setPreEstablishedRedirectUri("http://localhost:8080/spring-security-oauth/login");
|
||||
details.setUseCurrentUri(false);
|
||||
|
|
|
@ -12,4 +12,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
|
|||
List<Post> findBySubmissionDateBeforeAndIsSent(Date date, boolean isSent);
|
||||
|
||||
List<Post> findByUser(User user);
|
||||
|
||||
List<Post> findByRedditIDNotNullAndNoOfAttemptsGreaterThan(int attempts);
|
||||
}
|
|
@ -35,6 +35,14 @@ public class Post {
|
|||
|
||||
private String submissionResponse;
|
||||
|
||||
private String redditID;
|
||||
|
||||
private int noOfAttempts;
|
||||
|
||||
private int timeInterval;
|
||||
|
||||
private int minScoreRequired;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id", nullable = false)
|
||||
private User user;
|
||||
|
@ -115,6 +123,38 @@ public class Post {
|
|||
this.submissionResponse = submissionResponse;
|
||||
}
|
||||
|
||||
public String getRedditID() {
|
||||
return redditID;
|
||||
}
|
||||
|
||||
public void setRedditID(String redditID) {
|
||||
this.redditID = redditID;
|
||||
}
|
||||
|
||||
public int getNoOfAttempts() {
|
||||
return noOfAttempts;
|
||||
}
|
||||
|
||||
public void setNoOfAttempts(int noOfAttempts) {
|
||||
this.noOfAttempts = noOfAttempts;
|
||||
}
|
||||
|
||||
public int getTimeInterval() {
|
||||
return timeInterval;
|
||||
}
|
||||
|
||||
public void setTimeInterval(int timeInterval) {
|
||||
this.timeInterval = timeInterval;
|
||||
}
|
||||
|
||||
public int getMinScoreRequired() {
|
||||
return minScoreRequired;
|
||||
}
|
||||
|
||||
public void setMinScoreRequired(int minScoreRequired) {
|
||||
this.minScoreRequired = minScoreRequired;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Post [title=" + title + ", subreddit=" + subreddit + ", url=" + url + ", submissionDate=" + submissionDate + ", user=" + user + "]";
|
||||
|
|
|
@ -114,6 +114,10 @@ public class RedditController {
|
|||
post.setTitle(formParams.get("title"));
|
||||
post.setSubreddit(formParams.get("sr"));
|
||||
post.setUrl(formParams.get("url"));
|
||||
post.setNoOfAttempts(Integer.parseInt(formParams.get("attempt")));
|
||||
post.setTimeInterval(Integer.parseInt(formParams.get("interval")));
|
||||
post.setMinScoreRequired(Integer.parseInt(formParams.get("score")));
|
||||
|
||||
if (formParams.containsKey("sendreplies")) {
|
||||
post.setSendReplies(true);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.baeldung.web.schedule;
|
|||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.baeldung.persistence.dao.PostRepository;
|
||||
import org.baeldung.persistence.model.Post;
|
||||
|
@ -38,6 +39,12 @@ public class ScheduledTasks {
|
|||
for (final Post post : posts) {
|
||||
submitPost(post);
|
||||
}
|
||||
|
||||
final List<Post> submitted = postReopsitory.findByRedditIDNotNullAndNoOfAttemptsGreaterThan(0);
|
||||
logger.info(submitted.size() + " Posts to check their score");
|
||||
for (final Post post : submitted) {
|
||||
checkIfNeedResubmit(post);
|
||||
}
|
||||
}
|
||||
|
||||
private void submitPost(final Post post) {
|
||||
|
@ -72,10 +79,16 @@ public class ScheduledTasks {
|
|||
|
||||
logger.info("Submit link with these parameters: " + param.entrySet());
|
||||
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/submit", param, JsonNode.class);
|
||||
parseResponse(node, post);
|
||||
}
|
||||
|
||||
private void parseResponse(JsonNode node, Post post) {
|
||||
final JsonNode errorNode = node.get("json").get("errors").get(0);
|
||||
if (errorNode == null) {
|
||||
post.setSent(true);
|
||||
post.setSubmissionResponse("Successfully sent");
|
||||
post.setRedditID(node.get("json").get("data").get("id").asText());
|
||||
post.setNoOfAttempts(post.getNoOfAttempts() - 1);
|
||||
postReopsitory.save(post);
|
||||
logger.info("Successfully sent " + post.toString());
|
||||
} else {
|
||||
|
@ -85,6 +98,41 @@ public class ScheduledTasks {
|
|||
}
|
||||
}
|
||||
|
||||
private int getPostScore(String redditId) {
|
||||
final JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/info?id=t3_" + redditId, JsonNode.class);
|
||||
logger.info(node.toString());
|
||||
final int score = node.get("data").get("children").get(0).get("data").get("score").asInt();
|
||||
logger.info("post score = " + score);
|
||||
return score;
|
||||
}
|
||||
|
||||
private void deletePost(String redditId) {
|
||||
final MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
|
||||
param.add("id", "t3_" + redditId);
|
||||
final JsonNode node = redditRestTemplate.postForObject("https://oauth.reddit.com/api/del.json", param, JsonNode.class);
|
||||
logger.info(node.toString());
|
||||
}
|
||||
|
||||
private void checkIfNeedResubmit(Post post) {
|
||||
final long currentTime = new Date().getTime();
|
||||
final long interval = currentTime - post.getSubmissionDate().getTime();
|
||||
final long intervalInMinutes = TimeUnit.MINUTES.convert(interval, TimeUnit.MILLISECONDS);
|
||||
if (intervalInMinutes > post.getTimeInterval()) {
|
||||
final int score = getPostScore(post.getRedditID());
|
||||
if (score < post.getMinScoreRequired()) {
|
||||
deletePost(post.getRedditID());
|
||||
post.setRedditID(null);
|
||||
post.setSubmissionDate(new Date(currentTime + interval));
|
||||
post.setSent(false);
|
||||
post.setSubmissionResponse("Not sent yet");
|
||||
postReopsitory.save(post);
|
||||
} else {
|
||||
post.setNoOfAttempts(0);
|
||||
postReopsitory.save(post);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setRedditRestTemplate(final OAuth2RestTemplate redditRestTemplate) {
|
||||
this.redditRestTemplate = redditRestTemplate;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="<c:url value="/resources/datetime-picker.css" />">
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
|
||||
<script src="<c:url value="/resources/datetime-picker.js" />"></script>
|
||||
<script src="<c:url value="/resources/validator.js" />"></script>
|
||||
<style type="text/css">
|
||||
|
@ -44,6 +45,31 @@ border-color: #ddd;
|
|||
<span class="col-sm-9"><input name="sr" placeholder="Subreddit (e.g. kitten)" class="form-control" required data-minlength="3"/></span>
|
||||
</div>
|
||||
<br><br>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-3">Resubmit Settings</label>
|
||||
<span class="col-sm-3">Number of Attempts
|
||||
<select name="attempt">
|
||||
<option value="0" selected>None</option>
|
||||
<option value="2">2</option>
|
||||
<option value="3">3</option>
|
||||
<option value="4">4</option>
|
||||
<option value="5">5</option>
|
||||
</select>
|
||||
</span>
|
||||
<span class="col-sm-3">Time interval
|
||||
<select name="interval">
|
||||
<option value="0" selected>None</option>
|
||||
<option value="45">45 minutes</option>
|
||||
<option value="60">1 hour</option>
|
||||
<option value="120">2 hours</option>
|
||||
</select>
|
||||
</span>
|
||||
<span class="col-sm-1">Min score</span>
|
||||
<span class="col-sm-2">
|
||||
<input type="number" class="form-control" value="0" name="score" required/>
|
||||
</span>
|
||||
</div>
|
||||
<br><br>
|
||||
<div >
|
||||
<label class="col-sm-3">Send replies to my inbox</label> <span class="col-sm-9"><input type="checkbox" name="sendreplies" value="true"/></span>
|
||||
</div>
|
||||
|
@ -57,7 +83,8 @@ border-color: #ddd;
|
|||
});
|
||||
</script>
|
||||
|
||||
<br><br>
|
||||
<br><br>
|
||||
|
||||
|
||||
<button type="submit" class="btn btn-primary">Schedule</button>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue