import random
import urllib.request
import urllib.error
import re
import ircbot.plugin


def get_random():
	try:
		result = urllib.request.urlopen('http://www.youporn.com/random/video/',
			timeout=2)
		response = result.read().decode()
		result.close()

		result = re.findall('<p class="message">((?:.|\\n)*?)</p>', response)

		if not result:
			return None

		result = random.choice(result).strip()

		if ' ' not in result and '+' in result:
			result = result.replace('+', ' ')

		return result
	except (timeout, urllib.error.URLError, UnicodeDecodeError):
		pass

	return None


class Bitcoin:
	currencies = (
		'USD', 'EUR', 'hamburgers', 'farts', 'Razielcoins', 'BTC', 'salmons',
		'marble eggs in a shitty condom', 'typematrix keyboards', 'clean teeth',
		'dead Palestinian children', 'cmd.exe resizes', 'warp-in staplers',
		'mutalisks on creep', 'mutalisks off creep', 'floating cars',
		'burned rice', 'wordpress conference tickets', 'ice creams',
		'base64 encoded o\'reilly books', 'rolls of vitamin E toilet paper',
		'WISPY BEARDED POT SMOKING FAT FAGCUNT BITCOIN WORSHIPPERS WHO OBSESS OVER ME AND MAKE A SPORT OUT OF DRIVING ME INSANE AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
	)

	@classmethod
	def get_worth(cls):
		num = random.randint(100,100000) / 100
		currency = random.choice(cls.currencies)
		return '%.2f %s' % (num, currency)	


class Joke:
	reactions = (
		';ppppppppppppppp;', 'Lololooooolillololo', 'rofl haha xD',
		'pr;f', 'rofo',
	)

	@classmethod
	def get_reaction(cls):
		return random.choice(cls.reactions)


class RedditeuPlugin(ircbot.plugin.Plugin):
	"""#redditeu plugin."""
	@ircbot.plugin.command('btc')
	def get_btc_worth(self, msg):
		return '1 bitcoin is currently worth ' + Bitcoin.get_worth()

	@ircbot.plugin.command('joke')
	def get_joke_reaction(self, msg):
		return Joke.get_reaction()

	@ircbot.plugin.command('random')
	def get_yp_comment(self, msg):
		result = get_random()
		if result:
			return result
		return 'Error, try again!'

	@ircbot.plugin.reply
	def nay_here(self, msg):
		if 'nay' not in msg.user.nick.lower():
			return

		# strip all non-standard characters
		msg_str = ''.join([c for c in msg.message if 32 <= ord(c) <= 122])
		msg_str = msg_str.lower().strip()

		if 'sup' == msg_str or 'yo' == msg_str:
			return 'gay here'

@@ -1398,197 +1398,8 @@
 %09%0A%0A%0A
-class Joke:%0A%09reactions = (%0A%09%09';ppppppppppppppp;', 'Lololooooolillololo', 'rofl haha xD',%0A%09%09'pr;f', 'rofo',%0A%09)%0A%0A%09@classmethod%0A%09def get_reaction(cls):%0A%09%09return random.choice(cls.reactions)%0A%0A%0A
 clas
@@ -1593,105 +1593,8 @@
 ()%0A%0A
-%09@ircbot.plugin.command('joke')%0A%09def get_joke_reaction(self, msg):%0A%09%09return Joke.get_reaction()%0A%0A
 %09@ir

remove !joke