#!/usr/bin/env python
import sys
import pyperclip
import time
import subprocess
import urllib
from bs4 import BeautifulSoup

class Dictionary():
	def __init__(self, word):
		self.word = word
		self.get_url(word)
	
	def get_url(self, word):
		self.base_url = 'http://google-dictionary.so8848.com/meaning?word='
		self.url = self.base_url+str(word)
		self.get_meaning()
	
	def get_meaning(self):
		html_obj = urllib.urlopen(self.url)
		html_string = html_obj.read().decode('utf-8')
		#parser.feed(html_string)
		bs_obj = BeautifulSoup(html_string,"html.parser")
		if bs_obj:
			content = bs_obj.find_all('li')
        	result = content[4].contents[0]
        	result = self.word+" = "+str(result)
        	subprocess.Popen(['notify-send',"QuickDictionary", result])

class ShowMeaning():
	def __init__(self):
		self.start_process()

	def start_process(self):
		try:
			last_text = pyperclip.paste()
		except:
			print "install xclip using command 'sudo apt-get install xclip'"
		while(True):
			current_text = str(pyperclip.paste())
			if current_text and (str.isalpha(current_text) and current_text!=last_text):
				try:
					Dictionary(current_text)
					print "Found %s..." %current_text
				except:
					print "Error getting meaning"
					pass
			last_text = current_text
			
			
if __name__ == '__main__':
	ShowMeaning()
