33import java .util .Arrays ;
44import java .util .Date ;
55import java .util .List ;
6+ import java .util .concurrent .TimeUnit ;
67
78import org .baeldung .persistence .dao .PostRepository ;
89import org .baeldung .persistence .model .Post ;
@@ -38,6 +39,12 @@ public void reportCurrentTime() {
3839 for (final Post post : posts ) {
3940 submitPost (post );
4041 }
42+
43+ final List <Post > submitted = postReopsitory .findByRedditIDNotNullAndNoOfAttemptsGreaterThan (0 );
44+ logger .info (submitted .size () + " Posts to check their score" );
45+ for (final Post post : submitted ) {
46+ checkIfNeedResubmit (post );
47+ }
4148 }
4249
4350 private void submitPost (final Post post ) {
@@ -72,10 +79,16 @@ private void submitPostInternal(final Post post) {
7279
7380 logger .info ("Submit link with these parameters: " + param .entrySet ());
7481 final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/submit" , param , JsonNode .class );
82+ parseResponse (node , post );
83+ }
84+
85+ private void parseResponse (JsonNode node , Post post ) {
7586 final JsonNode errorNode = node .get ("json" ).get ("errors" ).get (0 );
7687 if (errorNode == null ) {
7788 post .setSent (true );
7889 post .setSubmissionResponse ("Successfully sent" );
90+ post .setRedditID (node .get ("json" ).get ("data" ).get ("id" ).asText ());
91+ post .setNoOfAttempts (post .getNoOfAttempts () - 1 );
7992 postReopsitory .save (post );
8093 logger .info ("Successfully sent " + post .toString ());
8194 } else {
@@ -85,7 +98,42 @@ private void submitPostInternal(final Post post) {
8598 }
8699 }
87100
101+ private int getPostScore (String redditId ) {
102+ final JsonNode node = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/info?id=t3_" + redditId , JsonNode .class );
103+ logger .info (node .toString ());
104+ final int score = node .get ("data" ).get ("children" ).get (0 ).get ("data" ).get ("score" ).asInt ();
105+ logger .info ("post score = " + score );
106+ return score ;
107+ }
108+
109+ private void deletePost (String redditId ) {
110+ final MultiValueMap <String , String > param = new LinkedMultiValueMap <String , String >();
111+ param .add ("id" , "t3_" + redditId );
112+ final JsonNode node = redditRestTemplate .postForObject ("https://oauth.reddit.com/api/del.json" , param , JsonNode .class );
113+ logger .info (node .toString ());
114+ }
115+
116+ private void checkIfNeedResubmit (Post post ) {
117+ final long currentTime = new Date ().getTime ();
118+ final long interval = currentTime - post .getSubmissionDate ().getTime ();
119+ final long intervalInMinutes = TimeUnit .MINUTES .convert (interval , TimeUnit .MILLISECONDS );
120+ if (intervalInMinutes > post .getTimeInterval ()) {
121+ final int score = getPostScore (post .getRedditID ());
122+ if (score < post .getMinScoreRequired ()) {
123+ deletePost (post .getRedditID ());
124+ post .setRedditID (null );
125+ post .setSubmissionDate (new Date (currentTime + interval ));
126+ post .setSent (false );
127+ post .setSubmissionResponse ("Not sent yet" );
128+ postReopsitory .save (post );
129+ } else {
130+ post .setNoOfAttempts (0 );
131+ postReopsitory .save (post );
132+ }
133+ }
134+ }
135+
88136 public void setRedditRestTemplate (final OAuth2RestTemplate redditRestTemplate ) {
89137 this .redditRestTemplate = redditRestTemplate ;
90138 }
91- }
139+ }
0 commit comments