import ircbot.plugin
import json
import socket
import urllib.error
import urllib.parse
import urllib.request

BASE_URL = 'http://qdb.lutro.me'


def _search_for_quote(quote):
	search = False
	if isinstance(quote, int):
		url = BASE_URL+'/'+str(quote)
		single_quote = True
	else:
		single_quote = False
		if quote == 'random':
			url = BASE_URL+'/random'
		elif quote == 'latest':
			url = BASE_URL
		else:
			search = str(quote)
			url = BASE_URL+'?'+urllib.parse.urlencode({'s': search})

	try:
		request = urllib.request.Request(url)
		request.add_header('Accept', 'application/json')
		response = urllib.request.urlopen(request, timeout=2)
		content = response.read().decode()
	except (urllib.error.URLError, socket.timeout):
		return 'HTTP error, try again!'

	data = json.loads(content)

	if single_quote:
		quote = data['quote']
	else:
		quotes = data['quotes']
		if 'items' in quotes:
			quotes = quotes['items']
		if len(quotes) < 1:
			return 'No quotes found!'
		quote = quotes[0]

	url = BASE_URL+'/'+str(quote['id'])

	if len(quote['body']) > 160:
		body = quote['body']
		if search:
			try:
				substr_pos = body.lower().index(search.lower())
				start = body.rfind('\n', 0, substr_pos) + 1
				start = max(start, substr_pos - 100)
				end = start + 150 - len(search)
			except ValueError:
				start = 0
				end = 100
		else:
			start = 0
			end = 100

		body = body.replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		excerpt = body[start:end]

		if start > 0:
			excerpt = '[...] ' + excerpt
		if end < len(quote['body']):
			excerpt = excerpt + ' [...]'
		body = excerpt
	else:
		body = quote['body'].replace('\r', '').replace('\n', ' ').replace('\t', ' ')

	return url + ' - ' + body


class RedditeuQdbPlugin(ircbot.plugin.Plugin):
	@ircbot.plugin.command('qdb')
	def search(self, cmd):
		if len(cmd.args) < 1:
			return

		if cmd.args[0][0] == '#':
			arg = int(cmd.args[0][1:])
		else:
			arg = ' '.join(cmd.args)

		return _search_for_quote(arg)

import ircbot.plugin
import json
import socket
import urllib.error
import urllib.parse
import urllib.request

BASE_URL = 'http://qdb.lutro.me'


def _search_for_quote(quote):
	search = False
	if isinstance(quote, int):
		url = BASE_URL+'/'+str(quote)
		single_quote = True
	else:
		single_quote = False
		if quote == 'random':
			url = BASE_URL+'/random'
		elif quote == 'latest':
			url = BASE_URL
		else:
			search = str(quote)
			url = BASE_URL+'?'+urllib.parse.urlencode({'s': search})

	try:
		request = urllib.request.Request(url)
		request.add_header('Accept', 'application/json')
		response = urllib.request.urlopen(request, timeout=2)
		content = response.read().decode()
	except (urllib.error.URLError, socket.timeout):
		return 'HTTP error, try again!'

	data = json.loads(content)

	if single_quote:
		quote = data['quote']
	else:
		quotes = data['quotes']
		if 'items' in quotes:
			quotes = quotes['items']
		if len(quotes) < 1:
			return 'No quotes found!'
		quote = quotes[0]

	url = BASE_URL+'/'+str(quote['id'])

	if len(quote['body']) > 160:
		body = quote['body']
		if search:
			try:
				body_len = len(body)
				substr_pos = body.lower().index(search.lower())
				start = body.rfind('\n', 0, substr_pos) + 1
				while body_len - start < 100:
					substr_pos = body.rfind('\n', 0, start - 1) + 1
					if body_len - substr_pos < 100:
						start = substr_pos
					else:
						break
				end = start + 150 - len(search)
			except ValueError:
				start = 0
				end = 100
		else:
			start = 0
			end = 100

		body = body.replace('\r', '').replace('\n', ' ').replace('\t', ' ')
		excerpt = body[start:end]

		if start > 0:
			excerpt = '[...] ' + excerpt
		if end < len(quote['body']):
			excerpt = excerpt + ' [...]'
		body = excerpt
	else:
		body = quote['body'].replace('\r', '').replace('\n', ' ').replace('\t', ' ')

	return url + ' - ' + body


class RedditeuQdbPlugin(ircbot.plugin.Plugin):
	@ircbot.plugin.command('qdb')
	def search(self, cmd):
		if len(cmd.args) < 1:
			return

		if cmd.args[0][0] == '#':
			arg = int(cmd.args[0][1:])
		else:
			arg = ' '.join(cmd.args)

		return _search_for_quote(arg)

improve excerpt functionality