#!/usr/bin/python import optparse import os import re import string import subprocess import sys import urllib2 __author__ = "nosmo@netsoc.tcd.ie (nosmo)" clients = ["o2sms", "meteorsms", "vodasms"] CLIENTCHOICE = 0 def GetTrack(user): """Gets a user's last listened to track. Uses the handy dandy audioscrobbler feed and parses out the track. En-dashes are for losers. Args: user: the last.fm username Returns: track: the last listened to track """ f = urllib2.urlopen("http://ws.audioscrobbler.com/1.0/user/%s/recenttracks.txt" % user) track = re.compile("\n").split(f.read())[0] # get rid of that filthy en-dash track = track.decode( 'utf-8') track = track.replace( u'\u2013', '-') track = re.compile(",").split(track)[1] return track def GetNumber(person): """Gets the number for a contact name in the o2sms config. This needs to be updated for people who use other silly networks. Args: person: a nickname for a number Returns: number: a string containing the person's number """ o2smsrc = open("%s/.%s/config" % (os.environ["HOME"], clients[CLIENTCHOICE]),"r") o2name = " %s " % person number = "" for line in o2smsrc.readlines(): if string.find(line, o2name) >= 0: line = line.rsplit(" ")[2] number = line.rstrip("\n") break return number def SendMessage(number, track, o2name): """Sends the message with the track affixed. Seriously, this is fucking stupid. Args: number: the number to send to track: the track name and artist o2name: the username of the person to send to """ try: lastline = raw_input("[ recipient : %s (%s) ]\n" % (o2name, number)) message = [] while lastline and lastline != ".": message.append(lastline) lastline = raw_input() except EOFError: None message.append(track) message = "\n".join(message) #Need to surpress output here or something text = subprocess.Popen("echo \"%s\" | %s %s" % (message, clients[CLIENTCHOICE], number), shell=True) text.wait() #Need to check exit code def main(): parser = optparse.OptionParser(usage="Usage: -l -n ") parser.add_option("-l", "--lastfm",action="store", type="string", dest="lastfm", help=("The last.fm username for the song to be grabbed from")) parser.add_option("-n", "--number", action="store", type="string", dest="o2num", help="The number to which you wish to text with this filth") parser.add_option("-c", "--client", action="store", type="int", dest="client", help="0 - o2sms (default), 1 - meteorsms, 2 - vodasms", default=0) (optparse.options, optparse.args) = parser.parse_args() global CLIENTCHOICE CLIENTCHOICE = optparse.options.client if optparse.options.o2num and optparse.options.lastfm: song = GetTrack(optparse.options.lastfm) number = GetNumber(optparse.options.o2num) SendMessage(number, song, optparse.options.o2num) elif not optparse.options.o2num and not optparse.options.lastfm: sys.stderr.write("No o2sms target specified!\n") sys.stderr.write("No last.fm username specified!\n") sys.exit(-1) elif not optparse.options.o2num: sys.stderr.write("No o2sms target specified!\n") sys.exit(-1) elif not optparse.options.lastfm: sys.stderr.write("No last.fm username specified!\n") sys.exit(-1) if __name__ == "__main__": main()