import json
import random
import re
import urllib.request
import urllib.error


def get_google_result(query):
	url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='
	url += '%20'.join(query)
	qs = '+'.join(query)
	search_url = 'https://www.google.com/?q='+qs+'#q='+qs

	result = urllib.request.urlopen(url)
	response = result.read().decode()
	result.close()
	data = json.loads(response)

	if data['responseData'] and data['responseData']['results']:
		result = data['responseData']['results'][0]
		result = result['title'] + ' - ' + result['unescapedUrl']
		return 'First result: ' + result + ' -- ' + search_url
	else:
		return 'No results!'


def get_random_yp_comment():
	url = "http://www.youporn.com/random/video/"

	result = urllib.request.urlopen(url)
	response = result.read().decode()
	result.close()

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

	if result:
		return random.choice(result)

@@ -277,16 +277,23 @@
 q='+qs%0A%0A
+%09try:%0A%09
 %09result 
@@ -312,34 +312,46 @@
 uest.urlopen(url
+, timeout=2
 )%0A
+%09
 %09response = resu
@@ -361,32 +361,33 @@
 read().decode()%0A
+%09
 %09result.close()%0A
@@ -386,16 +386,17 @@
 close()%0A
+%09
 %09data = 
@@ -413,24 +413,25 @@
 (response)%0A%0A
+%09
 %09if data%5B're
@@ -478,24 +478,25 @@
 esults'%5D:%0A%09%09
+%09
 result = dat
@@ -527,16 +527,17 @@
 ts'%5D%5B0%5D%0A
+%09
 %09%09result
@@ -590,16 +590,17 @@
 Url'%5D%0A%09%09
+%09
 return '
@@ -652,14 +652,45 @@
 l%0A%09e
-lse:%0A%09
+xcept urllib.error.URLError:%0A%09%09pass%0A%0A
 %09ret
@@ -785,16 +785,23 @@
 ideo/%22%0A%0A
+%09try:%0A%09
 %09result 
@@ -828,18 +828,30 @@
 open(url
+, timeout=2
 )%0A
+%09
 %09respons
@@ -877,16 +877,17 @@
 ecode()%0A
+%09
 %09result.
@@ -895,16 +895,17 @@
 lose()%0A%0A
+%09
 %09result 
@@ -968,16 +968,17 @@
 ponse)%0A%0A
+%09
 %09if resu
@@ -983,16 +983,17 @@
 sult:%0A%09%09
+%09
 return r
@@ -1012,9 +1012,77 @@
 (result)
+.strip()%0A%09except urllib.error.URLError:%0A%09%09pass%0A%0A%09return 'Try again!'
 %0A

add timeouts and error handling