Created
December 21, 2013 05:56
-
-
Save Justasic/8065956 to your computer and use it in GitHub Desktop.
This is an XChat/HexChat plugin to announce Google Voice SMS notifications locally on your IRC client. If you're like me and always looking at your IRC client not your phone, this is much more useful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__module_name__ = "Google Voice" | |
__module_version__ = "1.0" | |
__module_description__ = "Receive Google Voice SMS notifications in IRC" | |
# This google voice plugin ONLY receives SMS messages and displays them in | |
# the current context. You are NOT able to reply to these people through | |
# this plugin (YET). | |
# | |
# This plugin requires the pygooglevoice library, unfortunately, it hasn't | |
# been updated in a few years and has since changed. You can view an updated | |
# version here: | |
# | |
# http://code.google.com/r/kkleidal-pygooglevoiceupdate/source/checkout | |
USERNAME = "<Google Username>" | |
PASSWORD = "<Google Password>" | |
############################################################################# | |
# END USER CONFIGURATION | |
from googlevoice import Voice | |
import sys, time, random | |
import BeautifulSoup | |
import pynotify | |
import xchat | |
import thread, threading | |
local_thread = None | |
def extractsms(htmlsms) : | |
""" | |
extractsms -- extract SMS messages from BeautifulSoup tree of Google Voice SMS HTML. | |
Output is a list of dictionaries, one per message. | |
""" | |
msgitems = [] # accum message items here | |
# Extract all conversations by searching for a DIV with an ID at top level. | |
tree = BeautifulSoup.BeautifulSoup(htmlsms) # parse HTML into tree | |
conversations = tree.findAll("div",attrs={"id" : True},recursive=False) | |
for conversation in conversations: | |
# For each conversation, extract each row, which is one SMS message. | |
rows = conversation.findAll(attrs={"class" : "gc-message-sms-row"}) | |
for row in rows: # for all rows | |
# For each row, which is one message, extract all the fields. | |
msgitem = {"id" : conversation["id"]} # tag this message with conversation ID | |
spans = row.findAll("span",attrs={"class" : True}, recursive=False) | |
for span in spans: # for all spans in row | |
cl = span["class"].replace('gc-message-sms-', '') | |
msgitem[cl] = (" ".join(span.findAll(text=True))).strip() # put text in dict | |
msgitems.append(msgitem) # add msg dictionary to list | |
return msgitems | |
def ctx_print(event_name, *args): | |
ctx = xchat.get_context() | |
ctx.emit_print(event_name, *args) | |
class LoadThread(threading.Thread): | |
def __init__(self, voice): | |
self.voice = voice | |
self.items = {} | |
self.msgs = [] | |
threading.Thread.__init__(self) | |
self.running = True | |
def run(self): | |
while self.running: | |
self.voice.sms() | |
msgs = extractsms(self.voice.sms.html) | |
if not self.msgs: | |
self.msgs = msgs | |
continue | |
old_msgs = [] | |
new_msgs = [] | |
for msg in msgs: | |
new_msgs.append(msg['text']) | |
for msg in self.msgs: | |
old_msgs.append(msg['text']) | |
find_msgs = list(set(old_msgs) ^ set(new_msgs)) | |
if find_msgs: | |
new_msgs = [] | |
for m in find_msgs: | |
for o in msgs: | |
if m == o['text']: | |
new_msgs.append(o) | |
for msg in new_msgs: | |
user = msg['from'][:-1] | |
if user not in self.items: | |
self.items[user] = random.randint(0, 25) | |
if user == "Me": | |
continue | |
ctx_print("Channel Message", "\003%d%s\017" % (self.items[user], user), msg['text'], "$") | |
self.msgs = msgs | |
time.sleep(10) | |
def on_unload(userdata): | |
local_thread.running = False | |
local_thread.join() | |
if __name__ == "__main__": | |
xchat.hook_unload(on_unload) | |
voice = Voice() | |
voice.login(USERNAME.encode('utf-8'), PASSWORD.encode('utf-8')) | |
local_thread = LoadThread(voice) | |
local_thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment