@@ -58,12 +58,7 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
5858
5959 Map <String , String > param = new HashMap <String , String >();
6060 param .put ("api_type" , "json" );
61- param .put ("kind" , "self" );
62- param .put ("sr" , "api" );
63- // param.put("iden", "XCzyTdJveIcYXNhLJ4a2X9WVDswtx83u");
64- // param.put("captcha", "BJMGMU");
65- // param.put("title", "http2 is coming soon");
66- // param.put("text", "http2 is coming soon what do you think about that");
61+ param .put ("kind" , "link" );
6762 param .putAll (formParams );
6863
6964 System .out .println (param .keySet ());
@@ -83,20 +78,33 @@ public String submit(Model model, @RequestParam Map<String, String> formParams)
8378
8479 @ RequestMapping ("/post" )
8580 public String showSubmissionForm (Model model ) throws JsonProcessingException , IOException {
86- String needsCaptchaResult = needsCaptcha ();
87- if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
88- String newCaptchaResult = getNewCaptcha ();
89- String [] split = newCaptchaResult .split ("\" " );
90- String iden = split [split .length - 2 ];
91- model .addAttribute ("iden" , iden .trim ());
81+ try {
82+ List <String > subreddits = getSubreddit ();
83+ model .addAttribute ("subreddits" , subreddits );
84+
85+ String needsCaptchaResult = needsCaptcha ();
86+ if (needsCaptchaResult .equalsIgnoreCase ("true" )) {
87+ String newCaptchaResult = getNewCaptcha ();
88+ String [] split = newCaptchaResult .split ("\" " );
89+ String iden = split [split .length - 2 ];
90+ model .addAttribute ("iden" , iden .trim ());
91+ }
92+ } catch (UserApprovalRequiredException e ) {
93+ throw e ;
94+ } catch (UserRedirectRequiredException e ) {
95+ throw e ;
96+ } catch (Exception e ) {
97+ LOGGER .error ("Error occurred" , e );
98+ model .addAttribute ("error" , e .getLocalizedMessage ());
99+ return "reddit" ;
92100 }
93101 return "submissionForm" ;
94102 }
95103
96104 //
97105
98- public List <String > getSubreddit (Model model ) throws JsonProcessingException , IOException {
99- String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/subreddits/popular" , String .class );
106+ private List <String > getSubreddit () throws JsonProcessingException , IOException {
107+ String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/subreddits/popular?limit=50 " , String .class );
100108 JsonNode node = new ObjectMapper ().readTree (result );
101109 node = node .get ("data" ).get ("children" );
102110 List <String > subreddits = new ArrayList <String >();
@@ -106,12 +114,12 @@ public List<String> getSubreddit(Model model) throws JsonProcessingException, IO
106114 return subreddits ;
107115 }
108116
109- public String needsCaptcha () {
117+ private String needsCaptcha () {
110118 String result = redditRestTemplate .getForObject ("https://oauth.reddit.com/api/needs_captcha.json" , String .class );
111119 return result ;
112120 }
113121
114- public String getNewCaptcha () {
122+ private String getNewCaptcha () {
115123 HttpHeaders headers = new HttpHeaders ();
116124 headers .setContentType (MediaType .APPLICATION_JSON );
117125 HttpEntity req = new HttpEntity (headers );
0 commit comments