Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenarrow/291273 to your computer and use it in GitHub Desktop.
Save greenarrow/291273 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# LGPLv2 yadda yadda yadda, do whatever, yadda yadda yadda, bum it in the gob, yadda yadda yadda
import bluetooth, datetime, optparse, time, os, user, pynotify, tempfile, twitter, sys
# Install required packages on Ubuntu:
# sudo aptitude install python-twitter python-notify python-bluetooth festival
# Set these values if you wish to use Twitter (you probably won't, but then I didn't really want too but it happended anyway)
twitter_username = ''
twitter_password = ''
def speak_text(text):
"""Shell out to festival and mplayer to speak out text over sound system"""
tf = tempfile.NamedTemporaryFile(delete=False)
textfile = tf.name
tf.write(text)
tf.close()
wavefile = textfile + '.wav'
os.system( 'text2wave -o %s %s' % (wavefile, textfile) )
os.remove(textfile)
os.system('mplayer %s' % wavefile)
os.remove(wavefile)
if __name__ == '__main__':
# Have a gander at the options
parser = optparse.OptionParser()
parser.add_option('-p', '--period', dest='period', default=10, help='Pause between scans')
parser.add_option('-t', '--notification-timeout', dest='notification_timeout', default=10, help='Timeout for desktop notifications')
parser.add_option('-s', '--speak', dest='speak', action='store_true', default=False, help='Speak names using festival and mplayer')
parser.add_option('-w', '--twitter', dest='twitter', action='store_true', default=False, help='Upload names to twitter')
options, args = parser.parse_args()
# Initialise the bluetooth module
if not pynotify.init("bluesearch"):
parser.error("something went a bit wrong with initialising notify, sorry")
sys.exit()
# We keep a list of the last found MAC addresses here so we know when we have found new ones.
last_macs = set( [] )
print "starting bluesearch"
# authenticate twitter if required
if options.twitter:
twit = twitter.Api(username=twitter_username, password=twitter_password)
# Loop until terminated
while True:
print "searching for devices..."
try:
# Search for devices
devices = bluetooth.discover_devices(lookup_names=True)
except:
print "could not connect to bluetooth (or no devices found)"
else:
if len(devices):
# Get a set of mac current addresses
macs = set( [ mac for mac, name in devices ] )
# See which ones have appeared since the last scan
new_macs = list(macs - last_macs)
if len(new_macs):
print "new devices found:"
# Display a notification for each
for mac in new_macs:
name = dict(devices)[mac]
print '\t%s' % name
# Make a desktop notification
n = pynotify.Notification(name, mac)
n.set_timeout(options.notification_timeout)
n.show()
# Speak the text if required
if options.speak:
speak_text(name)
# Post to twitter if required
if options.twitter:
try:
twit.PostUpdate(name)
except:
twit = twitter.Api(username=twitter_username, password=twitter_password)
try:
twit.PostUpdate(name)
except:
print "twitter error"
last_macs = macs
# Wait a bit
print "waiting %d seconds..." % options.period
time.sleep(options.period)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment