package heufybot.modules;

import heufybot.utils.URLUtils;

import java.util.HashMap;
import java.util.List;

public class RandomCuteness extends Module
{
	public RandomCuteness()
	{
		this.authType = Module.AuthType.Anyone;
		this.trigger = "^" + commandPrefix + "(aww)($)";
	}
	
	@Override
	public void processEvent(String source, String message, String triggerUser, List<String> params) 
	{
		try
		{
			HashMap<String, String> feedElements = URLUtils.grabRSSFeed("http://imgur.com/r/aww/rss");
			String[] titles = new String[feedElements.keySet().size()];
			titles = feedElements.keySet().toArray(titles);
			
			int id = (int) (Math.random() * titles.length);
			bot.getIRC().cmdPRIVMSG(source, titles[id] + " | " + feedElements.get(titles[id]));
		}
		catch(Exception e)
		{
			bot.getIRC().cmdPRIVMSG(source, "Something went wrong while trying to read the RSS feed.");
		}
	}

	public String getHelp(String message)
	{
		return "Commands: " + commandPrefix + "aww | Returns random cuteness from the imgur /r/aww RSS feed.";
	}

	@Override
	public void onLoad() 
	{
	}

	@Override
	public void onUnload()
	{
	}
}

@@ -74,38 +74,170 @@
 til.
-HashMap;%0Aimport java.util.List
+List;%0A%0Aimport org.json.simple.JSONArray;%0Aimport org.json.simple.JSONObject;%0Aimport org.json.simple.parser.JSONParser;%0Aimport org.json.simple.parser.ParseException
 ;%0A%0Ap
@@ -524,24 +524,25 @@
 %09%7B%0A%09%09try
+ 
 %0A%09%09%7B%0A%09%09%09
 HashMap%3C
@@ -537,76 +537,117 @@
 %0A%09%09%09
-HashMap%3CString, String%3E feedElements = URLUtils.grabRSSFeed
+int pageNumber = (int) (Math.random() * 100 + 1);%0A%09%09%09JSONArray dataArray = (JSONArray) (getJSON
 (%22http
+s
 ://
+api.
 imgu
@@ -656,189 +656,434 @@
 com/
-r/aww/rss%22);%0A%09%09%09String%5B%5D titles = new String%5BfeedElements.keySet()
+3/gallery/r/aww/time/all/%22 + pageNumber)).get(%22data%22);%0A%09%09%09JSONObject object = (JSONObject) dataArray.get((int) (Math.random() * dataArray
 .size()
-%5D
+))
 ;%0A%09%09%09
+%0A%09%09%09String 
 title
-s
  = 
-feedElements.keySet().toArray(titles);
+object.get(%22title%22).toString();%0A%09%09%09String url = object.get(%22link%22).toString();%0A%09%09%09%0A%09%09%09if(url.equals(%22%22))
 %0A%09%09%09
+%7B
 %0A%09%09%09
-int id = (int) (Math.random() * titles.length);
+%09bot.getIRC().cmdPRIVMSG(source, %22Something went wrong while trying to get an image. Most likely the Imgur API is down.%22);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09
 %0A%09%09%09
@@ -1119,21 +1119,16 @@
 e, title
-s%5Bid%5D
  + %22 %7C %22
@@ -1134,50 +1134,26 @@
 %22 + 
-feedElements.get(titles%5Bid%5D)
+url
 );%0A%09%09%7D
+ 
 %0A%09%09catch
 (Exc
@@ -1148,17 +1148,23 @@
 %0A%09%09catch
-(
+ (Parse
 Exceptio
@@ -1167,16 +1167,17 @@
 ption e)
+ 
 %0A%09%09%7B%0A%09%09%09
@@ -1255,24 +1255,20 @@
 ead the 
-RSS feed
+data
 .%22);%0A%09%09%7D
@@ -1270,16 +1270,164 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09private JSONObject getJSON(String urlString) throws ParseException %0A%09%7B%0A%09%09return (JSONObject)new JSONParser().parse(URLUtils.grab(urlString));%0A%09%7D
 %0A%0A%09publi
@@ -1546,29 +1546,24 @@
 the 
-imgur 
 /r/aww 
-RSS feed
+subreddit
 .%22;%0A

Change RandomCuteness to use the imgur API (#46)